问题
I am using video camera2 API for video recording in app. I am following https://github.com/googlesamples/android-Camera2Basic this demo.
After recording a video is preview is inverted 180 degrees. How can manage this in both cases front and back camera?
回答1:
I done the following :
in Kotlin
setUpCameraOutputs()
// Find out if we need to swap dimension to get the preview size relative to sensor
// coordinate.
val displayRotation = activity.windowManager.defaultDisplay.rotation
mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION)!!
var swappedDimensions = false
when (displayRotation) {
Surface.ROTATION_0, Surface.ROTATION_180 -> if (mSensorOrientation == 90 || mSensorOrientation == 270) {
swappedDimensions = true
}
Surface.ROTATION_90, Surface.ROTATION_270 -> if (mSensorOrientation == 0 || mSensorOrientation == 180) {
swappedDimensions = true
}
else -> Log.e(TAG, "Display rotation is invalid: $displayRotation")
}
val displaySize = Point()
activity.windowManager.defaultDisplay.getSize(displaySize)
var rotatedPreviewWidth = width
var rotatedPreviewHeight = height
var maxPreviewWidth = displaySize.x
var maxPreviewHeight = displaySize.y
if (swappedDimensions) {
rotatedPreviewWidth = height
rotatedPreviewHeight = width
maxPreviewWidth = displaySize.y
maxPreviewHeight = displaySize.x
}
onImageAvailableListener
private val mOnImageAvailableListener = ImageReader.OnImageAvailableListener { reader ->
if (mCameraListener != null) {
val image = reader.acquireLatestImage()
val buffer = image.planes[0].buffer
var bytes = ByteArray(buffer.capacity())
buffer.get(bytes)
val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size, null)
var resultBitmap = bitmap
if (mCameraFacing == CameraCharacteristics.LENS_FACING_FRONT) {
val resultBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.width, bitmap.height, if (isPortrait) HORIZONTAL_FLIP_MATRIX else VERTICAL_FLIP_MATRIX, true)
}
mCameraListener!!.onImageAvailable(resultBitmap)
image.close()
}
}
in Java
setUpCameraOutputs()
// Find out if we need to swap dimension to get the preview size relative to sensor
// coordinate.
int displayRotation = activity.getWindowManager().getDefaultDisplay().getRotation();
mSensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
boolean swappedDimensions = false;
switch (displayRotation) {
case Surface.ROTATION_0:
case Surface.ROTATION_180:
if (mSensorOrientation == 90 || mSensorOrientation == 270) {
swappedDimensions = true;
}
break;
case Surface.ROTATION_90:
case Surface.ROTATION_270:
if (mSensorOrientation == 0 || mSensorOrientation == 180) {
swappedDimensions = true;
}
break;
default:
Log.e(TAG, "Display rotation is invalid: " + displayRotation);
}
Point displaySize = new Point();
activity.getWindowManager().getDefaultDisplay().getSize(displaySize);
int rotatedPreviewWidth = width;
int rotatedPreviewHeight = height;
int maxPreviewWidth = displaySize.x;
int maxPreviewHeight = displaySize.y;
if (swappedDimensions) {
rotatedPreviewWidth = height;
rotatedPreviewHeight = width;
maxPreviewWidth = displaySize.y;
maxPreviewHeight = displaySize.x;
}
onImageAvailableListener
private final ImageReader.OnImageAvailableListener mOnImageAvailableListener
= new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
if (mCameraListener != null) {
final Image image = reader.acquireNextImage();
final ByteBuffer buffer = image.getPlanes()[0].getBuffer();
final byte[] bytes = new byte[buffer.capacity()];
buffer.get(bytes);
final Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);
Bitmap resultBitmap = bitmap;
if (mCameraFacing == CameraCharacteristics.LENS_FACING_FRONT) {
resultBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), isPortrait() ? HORIZONTAL_FLIP_MATRIX : VERTICAL_FLIP_MATRIX, true);
}
mCameraListener.onImageAvailable(resultBitmap);
image.close();
}
}
};
来源:https://stackoverflow.com/questions/51892029/video-inverts-180-after-recording-in-front-camera-2-api