Pro-test to solve: the self-defined camera set to take pictures before and after Camera2 photo flip, rotate and other display problems

The beginning of my problems is that the front camera to get pictures of all kinds rotate, flip, is not normal, front and rear camera after settlement get the picture are normal, the effect is shown below (see photo middle bottom of the display):

        

Custom implementations of the camera, there are many online code, I've not posted all of the code posted to resolve how to deal with front and rear camera photos from normal (remember to the relevant authority), the layout is used TextureView control.

 

1. SparseIntArray set of front and rear (the default is the front, a start calling front ())

 private static final SparseIntArray ORIENTATIONS = new SparseIntArray();

    private void front() {
        //前置时,照片竖直显示
        ORIENTATIONS.append(Surface.ROTATION_0, 270);
        ORIENTATIONS.append(Surface.ROTATION_90, 0);
        ORIENTATIONS.append(Surface.ROTATION_180, 90);
        ORIENTATIONS.append(Surface.ROTATION_270, 180);
    }

    private void rear() {
        //后置时,照片竖直显示
        ORIENTATIONS.append(Surface.ROTATION_0, 90);
        ORIENTATIONS.append(Surface.ROTATION_90, 0);
        ORIENTATIONS.append(Surface.ROTATION_180, 270);
        ORIENTATIONS.append(Surface.ROTATION_270, 180);
    }

2. The switching process when doing front and rear camera

private String mCameraId = "1";//摄像头id(通常0代表后置摄像头,1代表前置摄像头)(应该有个按钮可以改变前后置的)
private CameraDevice cameraDevice;
private CameraCaptureSession mPreviewSession;
private CameraManager manager;

//stateCallback是摄像头状态
private void switch(){
     if ("1".equals(mCameraId)) {//前置变后置
                mCameraId = "0";
                rear();
            }else {//其它全部变成前置(默认的是前置)
                mCameraId = "1";
                front();
            }
            if (mPreviewSession != null) {
                mPreviewSession.close();
                mPreviewSession = null;
            }
            if (cameraDevice != null) {
                cameraDevice.close();
                cameraDevice = null;
            }
            try {
                manager.openCamera(mCameraId, stateCallback, null);
            } catch (CameraAccessException e) {
                e.printStackTrace();
            }

}

 3. Get a picture taken, and process, it is normal, in which ImageReader.OnImageAvailableListener of onImageAvailable method, processing code is as follows:

          // 获取捕获的照片数据
            Image image = reader.acquireNextImage();
            ByteBuffer buffer = image.getPlanes()[0].getBuffer();
            byte[] data = new byte[buffer.remaining()];
            buffer.get(data);

           Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            //前置时左右翻转时处理,后置是正常的,不需要处理了
            if ("1".equals(mCameraId)){
                Matrix m = new Matrix();
                m.postScale(-1, 1); // 镜像水平翻转
                 bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), m, true);
            }
            ivImage.setImageBitmap(bitmap);

                image.close();//一定要,不能会……,你试试看

 

If a better solution, please advise, thank you!

Published 70 original articles · won praise 11 · views 50000 +

Guess you like

Origin blog.csdn.net/weixin_40420578/article/details/104612819