从前置摄像头捕获图像后如何消除镜面效果?[Android]

Mr *_*mar 1 camera android android-camera android-camera-intent android-studio

基本上我创建了一个不添加视频录制的简单android相机应用程序,我点击了此链接 android相机应用程序教程链接

问题是当我从前置摄像头捕获图像时,单击捕获按钮后,显示镜像的图像就像我们站在镜子前面一样。例如我有右侧箭头图像,但是当我捕获时,它会预览左侧箭头。对不起英语。

小智 8

val outputOptions = ImageCapture
        .OutputFileOptions
        .Builder(photoFile)
        .setMetadata(ImageCapture.Metadata().also {
            it.isReversedHorizontal = CameraSelector.LENS_FACING_FRONT == lensFacing
        }).build()
Run Code Online (Sandbox Code Playgroud)


skb*_*hmn 5

显示位图时,可以在imageView中使用比例尺属性

android:scaleX="-1" //To flip horizontally or
android:scaleY="-1" //To flip verticallyenter code here
Run Code Online (Sandbox Code Playgroud)

您可能还需要研究这篇文章和本教程

更新1:您可以类似的方式在预览中翻转图像。您还可以创建一个辅助函数来翻转位图,然后将其设置为imageView。

可以在此处找到翻转图像的辅助功能。对于您的代码,您可能只需要以下内容:

public static Bitmap flip(Bitmap src, int type) {
            // create new matrix for transformation
            Matrix matrix = new Matrix();

            matrix.preScale(-1.0f, 1.0f);

            // return transformed image
            return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
        }
Run Code Online (Sandbox Code Playgroud)

然后在您的PreviewCaptureImage方法(而不是this)中imgPreview.setImageBitmap(bitmap);,使用flip方法,然后将其设置为imageView,如下所示

imgPreview.setImageBitmap(flip(bitmap));
Run Code Online (Sandbox Code Playgroud)