GLSurfaceView 自定义相机 拍照颠倒

近期根据项目需求用GLSurfaceView 自定义相机 发现拍照后图片颠倒 网上查找了资料 终于解决啦 ,记录一下

预览的镜像上下颠倒

设置一下相机方向

public void setCameraDisplayOrientation(Activity activity, int cameraId) {
        int result;
        try {
            Camera camera = mCamera.getCamera();
            Camera.CameraInfo info = new Camera.CameraInfo();
            Camera.getCameraInfo(cameraId, info);
            int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
            int degrees = 0;
            switch (rotation) {
                case Surface.ROTATION_0:
                    degrees = 0;
                    break;
                case Surface.ROTATION_90:
                    degrees = 90;
                    break;
                case Surface.ROTATION_180:
                    degrees = 180;
                    break;
                case Surface.ROTATION_270:
                    degrees = 270;
                    break;
            }

            if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                result = (info.orientation + degrees) % 360;
                result = (360 - result) % 360;
            } else {
                result = (info.orientation - degrees + 360) % 360;
            }
            camera.setDisplayOrientation(result);
        } catch (Exception e) {
            LogUtils.e("setCameraDisplayOrientation", e.toString());
        }

    }

拍完照后在旋转图片

                    //将字节数组
                    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                    //拍完照后旋转图片
                    Matrix matrix = new Matrix();
                    matrix.postRotate(getCameraDisplayOrientation((Activity) mContext, getCameraId(), mCamera.getCamera()));
                    matrix.postScale(1, isCameraFront() ? -1 : 1);
                    //获取正确方向bitmap
                    Bitmap cropRotateScaled = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
 private int getCameraDisplayOrientation(Activity activity, int cameraId) {
        int result = 90;
        try {
            Camera.CameraInfo info = new Camera.CameraInfo();
            Camera.getCameraInfo(cameraId, info);
            int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
            int degrees = 0;
            switch (rotation) {
                case Surface.ROTATION_0:
                    degrees = 0;
                    break;
                case Surface.ROTATION_90:
                    degrees = 90;
                    break;
                case Surface.ROTATION_180:
                    degrees = 180;
                    break;
                case Surface.ROTATION_270:
                    degrees = 270;
                    break;
            }

            if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                result = (info.orientation + degrees) % 360;
                result = (360 - result) % 360;
            } else {
                result = (info.orientation - degrees + 360) % 360;
            }
        } catch (Exception e) {
            LogUtils.e("getCameraDisplayOrientation", e.toString());
        }
        return result;

    }
 private boolean isCameraFront() {
        Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(cameraId, info);
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            //前置摄像头
            return true;
        }

        return false;
    }
这样问题就解决了




阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭