• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Trying to get a sub-section of a bitmap from CreateBitmap

Hi Everyone!

I'm trying to grab a subsection of a bitmap with CreateBitmap(orig, x,y,width,height) but always getting a width and height of -1 in Android 2.2. Anyone have any idea? I figured if it failed, I'd get null back, so I'm a bit confused.

First off, the reason you are getting -1 back instead of null is because the method is defined to return an int value, thus it cannot return null, so -1 is used instead.

As for your actual problem, from what little you have given us, I don't see why it would not work. The error is obviously somewhere else in your code. Could you post a bit more for us to analyze?
 
Upvote 0
Sure, here's the code as I am currently testing it, which is inside of the preview frame callback:

Camera.Parameters params = mCamera.getParameters();
Size size = params.getPreviewSize();
int he = size.height;
int wi = size.width;

YuvImage im = new YuvImage(_data, ImageFormat.NV21, wi,he,null);

Rect r = new Rect(0,0,wi,he);

ByteArrayOutputStream outStream = new ByteArrayOutputStream();

im.compressToJpeg(r, 75, outStream);

Bitmap bit = BitmapFactory.decodeStream(is);

Bitmap facePic = Bitmap.createBitmap(bit, 0, 0, 50, 50);


And facePic is what returns the -1 on width and height. Any ideas?
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones