Question

I want to make a png Image as the background image of a LWUIT Form. The problem is that the image is altered : there are stains in the image after setting it as the Form's background image. Here are codes :

public class DetailPhotoClient extends Form implements ActionListener {

    private Command options, delete, back, annuler, ok;
    private GaleriePhotos backForm;
    private FileConnection fcFile;
    private Image sourceImage, fullImage;
    private InputStream is;
    private PopupMenu popup;

    public DetailPhotoClient(GaleriePhotos prevForm, String absolutePathphotoName)
    {
        super();
        back = new Command("Retour");
        options = new Command("Options");
        this.addCommand(back);
        this.addCommand(options);
        this.addCommandListener(this);

        delete = new Command("Supprimer");
        annuler = new Command("Annuler");
        ok = new Command("Ok");

        backForm = prevForm;

        try {
            fcFile = (FileConnection) Connector.open(absolutePathphotoName, Connector.READ);
            is = fcFile.openInputStream();
            sourceImage = Image.createImage(is);
            fullImage = createThumbnail(sourceImage);
            setBgImage(fullImage);
            is.close();
            fcFile.close();
        } catch (IOException ex) {
            handleException();
        } catch (OutOfMemoryError oom) {
            handleOOM();
        }
    }
    private Image createThumbnail(Image image) {
        Image thumb = image.scaled(this.getPreferredW(), this.getPreferredH());
        return thumb;
    }
    ...
}

I noticed that when I open the photo manually , that is from the phone memory's photo folder , then the photo is not altered !

So how to make the image not altered when setting it as the Form's background image ?

Was it helpful?

Solution

LWUIT uses scaling for background images by default. It will always scale the images unless you explicitly ask the style for different behavior e.g. tiling, aligning etc. try:

myForm.getStyle().setBackgroundType(Style.BACKGROUND_IMAGE_ALIGNED_CENTER);

OTHER TIPS

Validate whether the image read from file, i.e. sourceImage, is as seen in the native phone device. If not so the problem lies here.

If you are able to see the sourceImage correct, than there are some things to be evaluated in getting a scaled image.

  1. In case your form contentPane's width / height is smaller than the images width / height than a better option would be use

    Image thumb = image.scaledSmallerRatio(this.getWidth(), this.getHeight()); NOTE: Yes, its getWidth() and not getPreferredW(). If you don't get desired result than try scaling with getPreferredW().

  2. In case your image's width / height is smaller than form contentPane's width / height, you might as well do not scale it since the image will fit the screen.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top