1
votes

I tried converting a URLImage to an EncodedImage to scale it but it turned the background black. Conversion works fine, the problem only occurs when I scale the image. In the guides it says that there are size restrictions to URLImages but I do not understand how to scale it properly. If somebody could write an example on how to scale URLImages to an arbitrary size I would greatly appreciate it.

Here is the code I used

    //create the form
    Form hi = new Form ("urlimage scaling");

    try
    {
        //create placeholder image
        EncodedImage placeholder = EncodedImage.create("/dog.jpg").scaledEncoded(492, 290);
        //get image from the web
        URLImage urlimg = URLImage.createToStorage(placeholder, "https://australianbananas.com.au/Images/Home/RipenessBlend.png", 
                "https://australianbananas.com.au/Images/Home/RipenessBlend.png");
        //try to convert and scale
        EncodedImage scaledImage = EncodedImage.createFromImage(urlimg,false).scaledEncoded(Display.getInstance().getDisplayWidth(), -1);
        //display image using a label
        Label lbl = new Label();
        lbl.setIcon(scaledImage);
        hi.add(lbl);
    } 
    catch(Exception e) {System.out.println(""+e);}

    hi.show();   
1

1 Answers

1
votes

Scaling a URLImage in this way is problematic because a URLImage's "data" will be its placeholder image until it is finished downloading. Therefore, calling things like EncodedImage.createFromImage(urlimg, false) will just create the URLImage's placeholder image as an encoded image.

Better to get the correct size for the placeholder image before loading the URLImage, and use the appropriate "scale" parameter in your call to createToStorage() so that the image is correctly mapped to the placeholder dimensions.