Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Camera picture to Bitmap results in messed up image

This is my code that I use to save the image to a Bitmap. This piece of code is based on the code from CyanogenMod's camera app so I would assume it would work properly but nope. The most important thing about this issue is that when tested on a Nexus 4 the Bitmap was being created properly for pictures taken with the back facing camera but using the front facing camera resulted in what you can see below.

The code that I'm using to create the Bitmap:

private class XyzPictureCallback implements Camera.PictureCallback {

    @Override
    public void onPictureTaken (byte [] data, Camera camera) {
        Options options = new Options();
        options.inDither = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;

        Bitmap image = BitmapFactory.decodeByteArray(data, 0, data.length, options);
    }
}

I tried using different Options (and none at all) but it did not help. It might be something with the pixel format returned by the two different cameras but when I ran getSupportedPictureFormats() they both returned ImageFormat.JPEG...

I'm running out of ideas...

I should probably also mention that saving the data directly using a FileOutputStream was creating a proper JPEG image. So the problem must be with the BitmapFactory and the way I create the Bitmap.

This is the bitmap that this code produces:

Broken image

EDIT (24.03.2013):

After spending multiple hours trying to fix this I still have no real solution to this. All i've found out is that the problem only occurs when I set the picture size (using Camera.Parameters.setPictureSize(int width, int height)) to the highest possible resolution that is available to the front facing camera which I've obtained by calling Camera.Parameters.getSupportedPictureSizes().

The resolution that is causing the problem is 1280x960. As I've mentioned earlier it's the highest resolution. The second highest is 1280x720 and when I use this one the output picture is fine. I did check the format that the camera spits out and it's ImageFormat.JPEG all the time so i don't think the pixel format is the issue here...

EDIT (08.03.2013): Call to takePicture:

private class XyzAutoFocusCallback implements Camera.AutoFocusCallback {

            @Override
            public void onAutoFocus(boolean success, Camera camera) {
                    if (takingPicture) {
                            camera.takePicture(null, null, myPictureCallback);
                    } else {
                    ...
            }

}
like image 923
Paul Avatar asked Mar 14 '13 08:03

Paul


1 Answers

The height and width aren't supported for that camera. That's why it looks like that and why the 1280x720 setting works. It's the same as if you set your starcraft to run at a resolution not supported by your monitor.

like image 166
Brian Griffey Avatar answered Sep 17 '22 12:09

Brian Griffey