Reputation: 456
I used setPreviewCallbackWithBuffer
and got video frame pixel data in onPreviewFrame()
callback. The byte data was YUV NV21 format and rotated 90 degrees. I did know how to convert the preview frame from YUV to RGB and rotate it back. But it took the time (around 20ms) that I didn't need because I had a lot of image processing tasks later. How should I config camera parameters or do anything else to get the right orientation video frame without my rotating & converting code?
Upvotes: 0
Views: 902
Reputation: 1997
Setup your camera orientation like this
private int compensateCameraRotation(int uiRotation) {
int currentDeviceOrientation = 0;
switch (uiRotation) {
case (Surface.ROTATION_0):
currentDeviceOrientation = 0;
break;
case (Surface.ROTATION_90):
currentDeviceOrientation = 270;
break;
case (Surface.ROTATION_180):
currentDeviceOrientation = 180;
break;
case (Surface.ROTATION_270):
currentDeviceOrientation = 90;
break;
default:
break;
}
And when you are initializing your camera you can set it like this
Camera.Parameters parameters = camera.getParameters();
parameters.setRotation(compensateCameraRotation(currentDisplay
.getRotation()));
Where currentDisplay is Object of Display you get it by calling
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
currentDisplay = windowManager.getDefaultDisplay();
Set your camera properties like this I had same problem recently I could solve it by setting properties like this.
Upvotes: 0
Reputation: 57203
You can fix camera live preview orientation on the screen with Camera.setDisplayOrientation() essentially for free, but does not affect JPEG pictures, or recorded videos.
Other relevant methods are setRotation(), which applies to captured JPEG picture, and setOrientationHint() for MediaRecorder.
But none of these methods affects the order of byte array passed to preview callbacks. Even the new camera2 API does not resolve this for you.
You don't need to go through RGB to rotate the NV21 frame, if that's what you need. This byte manipulation can be much faster than color conversion, but still takes time. If you have image processing (as hinted by your opencv tag), I would recommend to tune the image processing algorithms to accept rotated image. If changing the algorithmic part is too much of a trouble, but your image processing procedures are written in C++, you will see a performance gain if you code YUV rotation in C++.
Upvotes: 1