在安卓系统中如何使前置摄像头翻转镜像

10

在Android中,当你使用前置摄像头拍照时,预览会沿着Y轴反射,使得看到的图像好像用户正在照镜子一样。我想撤销这个效果(应用第二次反射),或者停止自动完成的反射。

我考虑使用这个:

Camera mCamera;
....
mCamera.setPreviewCallback(...);

但我真的不知道该如何处理覆盖

onPreviewFrame(byte[] data, Camera camera){...}

我如何最好地实现我所描述的内容?

注意,我正在尝试将此效果应用于实时预览,而不是已经拍摄的图像。


重复:https://dev59.com/wGkw5IYBdhLWcg3wn8CO#17569915 - Morrison Chang
@clairharrison,你得到解决方案了吗? - Aswathy
@Aswathy 不幸的是,所有答案都是针对保存的图像而不是实时预览的。 - Eduardo
2个回答

8

首先,当您通过Camera.open()打开相机实例时,应该使用Camera.open(getSpecialFacingCamera())来打开前置摄像头。

private int getSpecialFacingCamera() {
    int cameraId = -1;
    // Search for the front facing camera
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) {
        Camera.CameraInfo info = new Camera.CameraInfo();
        Camera.getCameraInfo(i, info);
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            cameraId = i;
            break;
        }
    }
    return cameraId;
}

然后,在将相机数据转换为图像的回调方法中,您可以使用以下代码来保持其正常。

public void onPictureTaken(byte[] data, Camera camera){
            Bitmap newImage = null;
            Bitmap cameraBitmap;
            if (data != null) {
                cameraBitmap = BitmapFactory.decodeByteArray(data, 0, (data != null) ? data.length : 0);
                if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
                    // use matrix to reverse image data and keep it normal
                    Matrix mtx = new Matrix();
                    //this will prevent mirror effect
                    mtx.preScale(-1.0f, 1.0f);
                    // Setting post rotate to 90 because image will be possibly in landscape
                    mtx.postRotate(90.f);
                    // Rotating Bitmap , create real image that we want
                    newImage = Bitmap.createBitmap(cameraBitmap, 0, 0, cameraBitmap.getWidth(), cameraBitmap.getHeight(), mtx, true);
                }else{// LANDSCAPE MODE
                    //No need to reverse width and height
                    newImage = Bitmap.createScaledBitmap(cameraBitmap, screenWidth, screenHeight, true);
                    cameraBitmap = newImage;
                }
            }
        }

您可以将newImage传递到画布中,创建jpeg图像并保存在设备上。 不要忘记相机已在21级API中弃用...


onPictureTaken()方法的完美结果,救了我的一天。谢谢。 - ND1010_

2
您可以使用矩阵来翻转图像数据,例如:
byte[] baImage = null;
Size size = camera.getParameters().getPreviewSize();
ByteArrayOutputStream os = new ByteArrayOutputStream();
YuvImage yuv = new YuvImage(data, ImageFormat.NV21, size.width, size.height, null);
yuv.compressToJpeg(new Rect(0, 0, size.width, size.height), 100, os);
baImage = os.toByteArray();
Bitmap bitmap = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length);
Matrix matrix = new Matrix(); 
matrix.preScale(-1.0f, 1.0f); 
Bitmap mirroredBitmap = Bitmap.createBitmap(bitmap, 0, 0, size.width, size.height, matrix, false);

而且...我错过了你的最后一条留言,回答了错误的问题 ;) - vitriolix

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接