Monday, July 30, 2012

Re: [android-developers] Byte [] image

On Mon, Jul 30, 2012 at 07:12:52AM -0700, Numair Qadir wrote:
> I'm working on an app, in which i convert the byte[] image to bitmap, but
> it showed me some weird image, (in attachment), here is my code

No need to look at the attached image...I can already see the errors.


> int width = parameters.getPreviewSize (). width;
> int height = parameters.getPreviewSize (). height;

Ok, why are you bothering with the preview size? You aren't dealing with
the preview here---you're dealing with the PHOTO image now. But that's
not important, as you don't even NEED the width or height here. So
remove the above two lines.

> ByteArrayOutputStream outStr = new ByteArrayOutputStream ();
> Rect rect = new Rect (0, 0, width, height);
> YuvImage yuvimage = new YuvImage (arg0, ImageFormat.NV21, width,
> height, null);
> yuvimage.compressToJpeg (rect, 100, outStr);

Ok, what's all this junk for? Toss it all into the bin....

> Bitmap bmp = BitmapFactory.decodeByteArray (outstr.toByteArray (), 0,
> outstr.size ());

Ok, now we're where we should be...except what you have about should look
more like this:

public void onPictureTaken(final byte[] data, final Camera camera) {
.....
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length, options);
.....
}

And then you have your bitmap. Be careful, however...anything over about
3 MP is going to cause an Out Of Memory Error when you exceed the amount
of heap the Java side allows you to use (which may be higher for devices
with higher-resolution cameras). I suggest using try/catch to avoid
locking the camera and forcing the user to reboot to reset the camera.
Then you can at least exit gracefully.

Later,
--jim

--
THE SCORE: ME: 2 CANCER: 0
73 DE N5IAL (/4) MiSTie #49997 < Running Mac OS X Lion >
spooky130u@gmail.com ICBM/Hurr.: 30.44406N 86.59909W

Saw something on TV about Psych-os.
Hmmmm, Psych OS. Perhaps the next freeware OS.... --me

Android Apps Listing at http://www.jstrack.org/barcodes.html

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment