【问题标题】:How can mirror effect be solved on preview screen AndroidAndroid预览屏出现镜像效果如何解决
【发布时间】:2017-03-20 12:40:32
【问题描述】:

之前我的代码遇到了一个问题,当我使用前置摄像头拍摄照片时,将图片设置为 ImageView 时,旋转被翻转并且图像使用镜像效果。

经过一些研究后问题得到解决,但是我仍然遇到图像在预览屏幕上翻转的问题。

启动活动以获得结果..(用户看到相机)

用户拍照(显示预览屏幕,这是我看到镜像效果的地方)

用户单击确定.. ImageView 已设置.. 此处图像设置完美,没有镜像效果问题

下面是我的代码..

    private void takePhoto(){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        uri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        startActivityForResult(intent, 100);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 100) {
            if (resultCode == RESULT_OK) {

                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 8;

                Bitmap bitmap = BitmapFactory.decodeFile(uri.getPath(),
                        options);

                Matrix matrix = new Matrix();

                float[] mirrorY = { -1, 0, 0, 0, 1, 0, 0, 0, 1};
                Matrix matrixMirrorY = new Matrix();
                matrixMirrorY.setValues(mirrorY);

                matrix.postConcat(matrixMirrorY);

                ExifInterface exifInterface = null;
                try {
                    exifInterface = new ExifInterface(uri.getPath());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                assert exifInterface != null;
                int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

                switch (orientation) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        matrix.setRotate(90);
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        matrix.setRotate(180);
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        matrix.setRotate(270);
                        break;

                }
                rotatedBitmap = Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
                visitorImage.setImageBitmap(rotatedBitmap);
                visitorPreviewImage.setImageBitmap(rotatedBitmap);

            } else if (resultCode == RESULT_CANCELED) {
                // user cancelled Image capture
                Toast.makeText(getApplicationContext(),
                        "User cancelled image capture", Toast.LENGTH_SHORT)
                        .show();
            } else {
                // failed to capture image
                Toast.makeText(getApplicationContext(),
                        "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                        .show();
            }

        }
    }

我的问题是我目前不确定在哪里可以解决预览屏幕的镜像效果问题..

谢谢大家。

【问题讨论】:

    标签: android android-camera


    【解决方案1】:

    我的问题是我目前不确定在哪里可以解决预览屏幕的镜像效果问题

    预览屏幕位于另一个 Android 应用中,而不是您的。你没有什么可以“修复”的。相机应用会根据相机应用的需要显示其预览。

    【讨论】:

    • 好的,是否可以停止在我的应用中显示预览?因为目前它有点误导我想要实现的目标?
    • @chirag90:“所以可以停止在我的应用程序中显示预览吗?” -- 不,因为它不是您的应用程序。它是相机应用程序。用户的相机应用程序(无论是预先安装的还是由用户从 Play 商店或其他地方安装的)将执行用户相机应用程序想要的任何操作。如果您需要这种级别的控制,请编写自己的相机应用程序,或添加一个库,将相机功能添加到您自己的应用程序中(并为您提供更多控制权)。
    • 非常感谢您的回答。我将看看图书馆/创建我自己的应用程序。如果您将其与提供的答案一起提供,我将接受它作为答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    • 2014-01-27
    • 2020-03-10
    • 1970-01-01
    • 1970-01-01
    • 2017-03-08
    相关资源
    最近更新 更多