Thursday, December 2, 2010

Zooming Bitmap images in blackberry Mobile Development

In our projects we may need zoom in or out images before they display.so this method will do it for you.simple but useful. you must have access to signed api's in blackberry
   private Bitmap ZoomImage(String FilePath, int zoomFactor) {

    Bitmap pictureDisplayed = null;

    try {
        // open the file connection to retrieve the picture from the saved
        // place
        FileConnection fconnForZoom = (FileConnection) Connector
        .open("file://" + FilePath);
        if (fconnForZoom.exists()) {
        InputStream input = fconnForZoom.openInputStream();
        int available = (int) fconnForZoom.fileSize();
        byte[] data = new byte[available];
        input.read(data, 0, available);
        EncodedImage ePictureOriginal = EncodedImage
        .createEncodedImage(data, 0, data.length);
        ePictureOriginal.setScale(20);
        if (zoomFactor == 100) {
            int scaleFP = Fixed32.tenThouToFP((int) 1 * 10000);
            EncodedImage ePictureDisplayed = ePictureOriginal
            .scaleImage32(scaleFP, scaleFP);
            pictureDisplayed = ePictureDisplayed.getBitmap();
        } else if (zoomFactor == 200) {
            int scaleFP = Fixed32.tenThouToFP((int) 2 * 10000);
            EncodedImage ePictureDisplayed = ePictureOriginal
            .scaleImage32(scaleFP, scaleFP);
            pictureDisplayed = ePictureDisplayed.getBitmap();
        }
        // I keep in memory the bitmap to get a smooth picture scrolling

        input.close();
        } else {
        pictureDisplayed = new Bitmap(0, 0);
        }

        fconnForZoom.close();

    } catch (Exception ioe) {

    }

    return pictureDisplayed;

    }

No comments:

Post a Comment