0

I have a class that takes a String parameter and performs a google search, then it gets the ten images and puts them in an array, that is then handled by another method in the same class. Using Javafx.scene.image would probably allow me to implement the buffering progress easily, but there is a bug with JavaFX Image, that misinterprets the color encoding of normal Images, and saves a weird looking image to the hard drive, so I just decided to use awt.Image.

This is the image search class:

public class GoogleCustomSearch {

    static String key = //custom google id;
    static String cx = // also a custom google id;
    static String searchType = "image";
    static java.awt.Image[] publicImageArray;


    public static java.awt.Image[] Search(String searchParameter,int start) throws IOException, URISyntaxException{

        String formatedText = URLEncoder.encode(searchParameter,"UTF-8");

        URL url = new URL("https://www.googleapis.com/customsearch/v1?" + "key=" +key + "&cx=" +cx + "&q=" +formatedText + "&searchType=" +searchType +"&imgSize=medium" + "&start=" + start + "&num=10");
        System.out.println(url);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Accept", "application/json");
        BufferedReader br = new BufferedReader(new InputStreamReader( ( conn.getInputStream() ) ) );
        GResults results = new Gson().fromJson(br, GResults.class);

        java.awt.Image [] imageArray = new java.awt.Image[10];
        //JProgressBar prb = new JProgressBar();
        //MediaTracker loadTracker = new MediaTracker(prb);
        for(int i = 0; i<10; i++){
            try {
                imageArray[i] = ImageIO.read(new URL(results.getLink(i)));
            }catch (java.io.IOException e){
                imageArray[i] = ImageIO.read(new File("C:\\Users\\FILIP.D\\IdeaProjects\\Manual_Artwork\\src\\MAT - NoImage.jpg"));
            }

        }
        conn.disconnect();

        return imageArray;

    }

    public static BufferedImage getImage(String searchPar, int index, boolean newSearch) throws IOException, URISyntaxException {
        int adaptedIndex;
        int start;
        BufferedImage bimage;
        if(index<10){
            adaptedIndex = index;
            start = 1;
        }else if (index<20){
            start = 11;
            adaptedIndex = index % 10;
            if(index == 10){
                publicImageArray = new java.awt.Image[10];
                publicImageArray = Search(searchPar,start);
            }
        }else if(index < 30){
            start = 21;
            adaptedIndex = index % 10;
            if (index == 20) {
                publicImageArray = new java.awt.Image[10];
                publicImageArray = Search(searchPar,start);
            }
        }else{
            adaptedIndex = index % 10;
            start = 21; //ovo ce posle 30 da ga vrti u loop prvih 10
        }
        if(newSearch){
            publicImageArray = new java.awt.Image[10];
            publicImageArray = Search(searchPar,start);
            return bimage = (BufferedImage) publicImageArray[adaptedIndex];
        }else{
            return bimage = (BufferedImage) publicImageArray[adaptedIndex];
        }
    }

    public static RenderedImage getLiveImage (int index){
        return (RenderedImage) publicImageArray[index % 10];
    }
}

And this is the snippet of the main GUI class that just handles opening the new image in the array

private void nextImageResult() throws IOException, URISyntaxException {
        if(imgNr == -1){
            imgNr++;
            changeImage(SwingFXUtils.toFXImage(GoogleCustomSearch.getImage(oppenedTrack.getArtistName() + "+" + oppenedTrack.getTrackName(),imgNr,true),null));
        }else{
            imgNr++;
            changeImage(SwingFXUtils.toFXImage(GoogleCustomSearch.getImage(oppenedTrack.getArtistName() + "+" + oppenedTrack.getTrackName(),imgNr,false),null));
        }

    }

To summarise, I need a proper way to show a progress bar in the place of the image before it loads, and it needs not to hang the UI, for which I can use Task. I can optimise the loading of the array with MediaTracker, so it can prioritize loading the first few images first.

Dzeri96
  • 196
  • 2
  • 13
  • "there is a bug with JavaFX Image, that misinterprets the color encoding of normal Images" -> what bug? Is it [JDK-8114609 Incorrect display of JPEG images](https://bugs.openjdk.java.net/browse/JDK-8114609)? (which would seem to be fixed in recent JavaFX builds based upon the bug status). For JavaFX applications, I recommend using JavaFX images rather than AWT images, if this is possible for you. – jewelsea Mar 11 '16 at 18:43
  • It is that bug, but I had it appear two weeks ago, before I implemented awt. How could I check if the version of Java I'm using has the fixed bug? My IDE is Idea. – Dzeri96 Mar 11 '16 at 18:50
  • And the JavaFX image was displaying correctly in the app, but as soon as I used ImageIO to turn it into a file, it misinterpreted the colors. – Dzeri96 Mar 11 '16 at 18:51
  • Could be a bug in ImageIO, not JavaFX. You could run `java -version` to check what Java version you are using from the command line (it should be the [latest from Oracle](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html)). To check the Java version used by the IDE, you will need to consult the IDE documentation. – jewelsea Mar 11 '16 at 18:58
  • So would it be better to make awt work instead of searching for the bug and trying to fix it? – Dzeri96 Mar 11 '16 at 19:02
  • I think it would be better to have it work without awt, but how you choose to proceed is up to you. There is a possibility the bug is already fixed and you are just running with an outdated JDK. I had a similar issue (see answer to [Display RTP MJPEG](http://stackoverflow.com/questions/16721917/display-rtp-mjpeg)) and a JDK update fixed it. If it actually ended up that you need to patch the JDK yourself to fix the bug, that would be inadvisable and probably best to go the integrated awt route at that stage, however that may not be the case. – jewelsea Mar 11 '16 at 19:04
  • I should be using 8u73, and all mentions of this bug should long have been fixed in this version, but obviously its still there so it seems like it's still well documented. – Dzeri96 Mar 11 '16 at 19:17
  • Can you supply a link to the image you are testing with so that this can be independently verified? – jewelsea Mar 11 '16 at 19:19
  • It is literally any image searched on google, but definately one of the images I tested was this one: http://blogdailyherald.com/wp-content/uploads/2015/10/drake-future-what-a-time-to-be-alive-cover-full.jpg To reproduce the bug, write a JPG file with ImageIO and SwingFXUtils.ToFxImage – Dzeri96 Mar 11 '16 at 19:22
  • `final Image image = new Image(IMAGE_LOC); System.out.println("Exception value: " + image.getException());` outputs `java.io.IOException: Server returned HTTP response code: 403`, so the server is rejecting the image fetching request from the JavaFX app, even though it works in a browser (probably something to do with accepted media type content headers in the request and a poorly coded server or something like that). I'm not sure why you would write a jpg file with ImageIO in this case. – jewelsea Mar 11 '16 at 19:32
  • @jewelsea yeah I might have mislead you, this can happen from time to time with some Images, just pick literally any image from google results that doesn't give you this error – Dzeri96 Mar 11 '16 at 19:34
  • Well if I use just any image, it works fine, for example: `Image image = new Image("http://www.personal.psu.edu/jul229/mini.jpg"); ImageView imageView = new ImageView(image); stage.setScene(new Scene(new StackPane(imageView))); stage.show();` – jewelsea Mar 11 '16 at 19:38
  • Now use new file and write and Image to it with ImageIO and SwingFxUtils.ToFxImage(imageView.getImage(), null); The file on the hard drive should be messed up, but when oppened again in the gui it should still look normal. – Dzeri96 Mar 11 '16 at 19:42

0 Answers0