本文介绍了在前置摄像头2 API中录制后,视频反转180的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using video camera2 API for video recording in app. I am following https://github.com/googlesamples/android-Camera2Basic this demo.

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

After recording a video is preview is inverted 180 degrees. How can manage this in both cases front and back camera?

推荐答案

我完成了以下操作:

在科特林

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

这篇关于在前置摄像头2 API中录制后,视频反转180的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 17:47