Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

camera pixels rotated

I want to do some image processing to the pixels gotten from the camera.

The problem is that the pixels from the camera are rotated 90 degrees.

Im getting the pixels inside the method onPreviewFrame(byte[] data, Camera camera)

I tried camera.setDisplayOrientation(90); and it displays the video in the correct orientation but I am still receiving the rotated pixels as stated in the documentation:

This does not affect the order of byte array passed in Android.Hardware.Camera.IPreviewCallback.OnPreviewFrame(Byte[], Android.Hardware.Camera), JPEG pictures, or recorded videos.

I also tried:

parameters.setRotation(90);
camera.setParameters(parameters);

but that did not work.

I'm using android 2.2

enter image description here

Top image shows the SurfaceView when using camera.setDisplayOrientation(90);

The second image is gotten inside onPreviewFrame(byte[] data, Camera camera) from the data array. As you can see the data array comes rotated.

like image 491
Enrique Avatar asked Jul 28 '11 02:07

Enrique


1 Answers

Maybe you can perform your image processing on the sideways frame, and only worry about rotation when you display it (which you have already done). If not, you need to rotate the frame the old fashioned way. I found some code somewhere (with a little modification) that might help:

// load the origial bitmap
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
       R.drawable.android);

int width = bitmap.width();
int height = bitmap.height();

// create a matrix for the manipulation
Matrix matrix = new Matrix();
// rotate the Bitmap 90 degrees (counterclockwise)
matrix.postRotate(90);

// recreate the new Bitmap, swap width and height and apply transform
Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                  width, height, matrix, true);

This is easy because the createBitmap method supports a matrix transform: http://developer.android.com/reference/android/graphics/Bitmap.html#createBitmap(android.graphics.Bitmap, int, int, int, int, android.graphics.Matrix, boolean)

like image 129
Matt Montag Avatar answered Oct 20 '22 17:10

Matt Montag