Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
abhi007tyagi
Posts: 225
Registered: ‎09-29-2010
My Device: Limited Edition Z10, Playbook
My Carrier: Airtel
Accepted Solution

How to fetch image from device to bitmap

Hi,

 

how can I open an image file on device to a bitmap?

I have the path of the image file (///file:.........../abc.png,jpeg,..) and I want to create a bitmap object from it.

 

should I open the image as file and convert it into byte[] and then to bitmap or there is other simpler way of doing this?

 

Thanks

Abhinav Tyagi

Blackberry Z10-Limited Edition
Blackberry Playbook (won in free playbook offer)
My Apps : pAinT, HealthMeter, Shoot
Please use plain text.
Developer
simon_hain
Posts: 16,113
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to fetch image from device to bitmap

you open it with a fileconnection and use http://www.blackberry.com/developers/docs/6.0.0api/net/rim/device/api/system/EncodedImage.html#creat... int, int) to create the picture.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
Blanc
Posts: 638
Registered: ‎07-02-2009
My Device: Not Specified

Re: How to fetch image from device to bitmap

Look at the EncodedImage class. It takes byte[] as input and returns you a bitmap. You can fetch the byte[] using the FileConnection API

Thanks and Regards,
PraveenGoparaju.
Please use plain text.
Developer
abhi007tyagi
Posts: 225
Registered: ‎09-29-2010
My Device: Limited Edition Z10, Playbook
My Carrier: Airtel

Re: How to fetch image from device to bitmap

Hi,

 

I am getting the image on screen.

But the portrait images are coming in landscape and stretched like this

bb.PNG

 

 

How can I have image in portrait or landscape depending on how it is taken?

I want them to fit into the bitmapfield.

 

here is code I am using now.

 

EncodedImage ei = null;
			FileConnection fconn = null;
			InputStream is = null;
			try {
				fconn = (FileConnection) Connector.open(str, Connector.READ);
				is = fconn.openInputStream();
				ei = EncodedImage.createEncodedImage(IOUtilities.streamToBytes(is), 0,-1);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (is != null) {
					try {
						is.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (fconn != null) {
					try {
						fconn.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}

			Dialog.alert("You selected " + str);
			bitgraphic.clear();
			bitgraphic.drawBitmap(0, 0, bitmap.getWidth(), bitmap.getHeight(),bitmap, 0, 0);

 

 

Thanks

Abhinav Tyagi

Blackberry Z10-Limited Edition
Blackberry Playbook (won in free playbook offer)
My Apps : pAinT, HealthMeter, Shoot
Please use plain text.
Developer
simon_hain
Posts: 16,113
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to fetch image from device to bitmap

as a general note: blocking operations, as I/O, should be done on a separate thread.

you can use http://www.blackberry.com/developers/docs/6.0.0api/net/rim/device/api/system/EncodedImage.html#scale... int) to resize the image.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
arsenk01
Posts: 91
Registered: ‎06-21-2011
My Device: Blackberry Torch 9800
My Carrier: developer

Re: How to fetch image from device to bitmap

Do you using simulator or real device?

However here is the code which may help you:

 

public static EncodedImage resizeImage(EncodedImage ei, int requiredWidth,
            int requiredHeight) {
        int currentWidthFixed32 = Fixed32.toFP(ei.getWidth());
        int currentHeightFixed32 = Fixed32.toFP(ei.getHeight());

        int requiredWidthFixed32 = Fixed32.toFP(requiredWidth - 4);
        int requiredHeightFixed32 = Fixed32.toFP(requiredHeight);

        int scaleXFixed32 = Fixed32.div(currentWidthFixed32,
                requiredWidthFixed32);
        int scaleYFixed32 = Fixed32.div(currentHeightFixed32,
                requiredHeightFixed32);

        EncodedImage newEi = ei.scaleImage32(scaleXFixed32, scaleYFixed32);
        return newEi;
    }

    public static Bitmap resizeBitmap(Bitmap image, int width, int height) {
        int imageWidth = image.getWidth();
        int imageHeight = image.getHeight();

        // Need an array (for RGB, with the size of original image)
        int rgb[] = new int[imageWidth * imageHeight];

        // Get the RGB array of image into "rgb"
        image.getARGB(rgb, 0, imageWidth, 0, 0, imageWidth, imageHeight);

        // Call to our function and obtain rgb2
        int rgb2[] = rescaleArray(rgb, imageWidth, imageHeight, width, height);

        // Create an image with that RGB array
        Bitmap temp2 = new Bitmap(width, height);

        temp2.setARGB(rgb2, 0, width, 0, 0, width, height);

        return temp2;
    }


Regards,
Arsen
Please use plain text.
Developer
abhi007tyagi
Posts: 225
Registered: ‎09-29-2010
My Device: Limited Edition Z10, Playbook
My Carrier: Airtel

Re: How to fetch image from device to bitmap

Thanks for helping,

 

I am using both simulator and device Torch 9800
The image rotates when it gets loaded into my bitmapfield of the application as in image I posted earlier.

 

I will try your code and check the working again.

 

Thanks

Abhinav Tyagi

Blackberry Z10-Limited Edition
Blackberry Playbook (won in free playbook offer)
My Apps : pAinT, HealthMeter, Shoot
Please use plain text.
Developer
arsenk01
Posts: 91
Registered: ‎06-21-2011
My Device: Blackberry Torch 9800
My Carrier: developer

Re: How to fetch image from device to bitmap

Please use plain text.
Developer
abhi007tyagi
Posts: 225
Registered: ‎09-29-2010
My Device: Limited Edition Z10, Playbook
My Carrier: Airtel

Re: How to fetch image from device to bitmap

thanks, I was trying that Orientation method... but testing that on simulator

It seems issue is with simulator for orientation approach

Will try your code and this one today on device and update you

 

Thanks

Abhinav Tyagi

Blackberry Z10-Limited Edition
Blackberry Playbook (won in free playbook offer)
My Apps : pAinT, HealthMeter, Shoot
Please use plain text.
Developer
abhi007tyagi
Posts: 225
Registered: ‎09-29-2010
My Device: Limited Edition Z10, Playbook
My Carrier: Airtel

Re: How to fetch image from device to bitmap

Hi,

 

I used the orientation code mentioned in the link

I am getting "6" for my image on device.

 

but how can I have correct orientation???

 

Thanks

Abhinav Tyagi

Blackberry Z10-Limited Edition
Blackberry Playbook (won in free playbook offer)
My Apps : pAinT, HealthMeter, Shoot
Please use plain text.