在前置摄像头2 API中录制后,视频反转180

时间:2018-08-17 09:11:10

标签: android android-camera2

我正在使用video camera2 API在应用中进行视频录制。我正在关注https://github.com/googlesamples/android-Camera2Basic此演示。

录制视频后,预览反转180度。在前置摄像头和后置摄像头的情况下如何进行管理?

1 个答案:

答案 0 :(得分:0)

我做了以下事情:

在科特林

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()
    }
}

在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();
        }
    }
};