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
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With