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


Last Updated:

  1. ophanin

    ophanin Member This Topic's Starter

    Joined:
    Jun 14, 2010
    Messages:
    20
    Likes Received:
    0
    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.
     

    Advertisement
  2. jonbonazza

    jonbonazza Well-Known Member

    Joined:
    Jul 13, 2010
    Messages:
    1,934
    Likes Received:
    458
    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?
     
  3. ophanin

    ophanin Member This Topic's Starter

    Joined:
    Jun 14, 2010
    Messages:
    20
    Likes Received:
    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?
     
  4. IanGClifton

    IanGClifton Well-Known Member

    Joined:
    Feb 7, 2010
    Messages:
    111
    Likes Received:
    19
    Where is "is" defined? -- Bitmap bit = BitmapFactory.decodeStream(is); -- I'd suggest looking at what bit is at this point.
     
  5. ophanin

    ophanin Member This Topic's Starter

    Joined:
    Jun 14, 2010
    Messages:
    20
    Likes Received:
    0
    Hah, I missed (or accidently deleted) part of my code there. is is defined right before bit there, as:

    ByteArrayInputStream is = new ByteArrayInputStream(outStream.toByteArray());

    Apologies for the omission
     

Share This Page

Loading...