【问题标题】:Orientation image image after use Front or Back camera使用前置或后置摄像头后的方位图像图像
【发布时间】:2017-08-31 06:06:40
【问题描述】:

我在我的应用程序中使用相机来捕获图像,当切换到前置摄像头时,图片是 mirrorrd 。我用这个代码拍照(旋转和补偿镜子) 使用此代码后置摄像头即可(无反光镜和旋转)! 但前置摄像头效果与这张照片相似:

获取前置摄像头 id 的方法: (我使用 CameraInfo 获取前置摄像头 id 并将 id 发送到结果活动)

    public int getFrontFacingCameraId() {
        int numCameras = getNumberOfCameras();
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        for (int cameraId = 0; cameraId < numCameras; cameraId++) {
            Camera.getCameraInfo(cameraId, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                return cameraId;
            }
        }
        return 0;
    }

这个旋转代码:(使用矩阵)

private Bitmap rotateImage(Bitmap source, float angle) {
            Matrix matrix = new Matrix();
            matrix.postRotate(angle);
            return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
            return source;
        }
    }

此代码用于活动结果:

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case PICTURE_RESULT:
                if (resultCode == Activity.RESULT_OK) {
                    try {
                        Bitmap thumbnail = MediaStore.Images.Media.getBitmap(getContentResolver(), IMG_URI);
                        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
                        int rotation = this.getWindowManager().getDefaultDisplay().getRotation();
                        int degrees = 0;
                        switch (rotation) {
                            case Surface.ROTATION_0:
                                degrees = 90;
                                break;
                            case Surface.ROTATION_90:
                                degrees = 180;
                                break;
                            case Surface.ROTATION_180:
                                degrees = 270;
                                break;
                            case Surface.ROTATION_270:
                                degrees = 360;
                                break;
                        }
                        int displayOrientation;
                        if (cameraInfo.facing == getFrontFacingCameraId()) {
                            displayOrientation = (cameraInfo.orientation + degrees) % 360;
                            displayOrientation = (360 - displayOrientation) % 360;
                        } else {
                            displayOrientation = (cameraInfo.orientation - degrees + 360) % 360;
                        }
                        USER_CIRCLE_PHOTO.setImageBitmap(Bitmap.createScaledBitmap(rotateImage(thumbnail, displayOrientation), 480, 800, false));
                        getContentResolver().delete(IMG_URI, null, null);
                    } catch (Exception e) {
                        displayToast(this, "خطای گرفتن عکس:" + "\n" + e.toString());
                    }
                }
        }
    }

我该如何解决这个问题 谢谢

什么时候改成这个:

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;

结果:

后退和相机不行!

【问题讨论】:

    标签: android camera image-capture


    【解决方案1】:
    /***Inside your Picture Taken Call back try to change**/
    
    private PictureCallback mPicture = new PictureCallback()
    {
    
    @Override
    public void onPictureTaken(byte[] data, Camera camera)
    {
    
        pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
    
        if (pictureFile == null)
        {
            return;
        }
    
        try
        {
            path = pictureFile.getPath();
            FileOutputStream fos = new FileOutputStream(pictureFile);
    
            fos.write(data);
            fos.close();
            if (pictureFile.exists())
            {
    
                Bitmap largeIcon = BitmapFactory.decodeFile(pictureFile.getAbsolutePath());
    
    
                if(cameraId == CameraInfo.CAMERA_FACING_BACK)
                {
                    imageRotation(largeIcon);
    
    
    
                }
                else
                {
    
                     rotateFrontImage(largeIcon);
    
    
    
                }
    
    
    
    
    
    
            }
    
    
    
            /* * Make the callback to the calling activity to handle picture
             * clicked*/
    
            mCallback.imageClicked(pictureFile);
    
    
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
    
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
    };
    
    /***********************************************/
    
    public Bitmap rotateFrontImage(Bitmap source)
    {
    
    Bitmap rImg;
    
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT)
    {
    
        Bitmap imgBitmap = source;
    
        ExifInterface ei = null;
        try
        {
    
            ei = new ExifInterface(path );
    
            int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    
    
            switch(orientation)
            {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    imgBitmap = rotateImage(source, 90);
    
    
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    imgBitmap = rotateImage(source, 180);
    
    
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    imgBitmap = rotateImage(source, 270);
    
    
                    break;
    
    
            }
    
        } catch (IOException e)
        {
            e.printStackTrace();
    
                       }
    
        Matrix rotateRight = new Matrix();
        rotateRight.preRotate(270);
    
        if(android.os.Build.VERSION.SDK_INT>13 &&  cameraId == CameraInfo.CAMERA_FACING_FRONT)
        {
    
            float[] mirrorY = { -1, 0, 0, 0, 1, 0, 0, 0, 1};
            //rotateRight = new Matrix();
            Matrix matrixMirrorY = new Matrix();
            matrixMirrorY.setValues(mirrorY);
    
            rotateRight.postConcat(matrixMirrorY);
    
            Log.i(TAG, "rotateFrontImage if mirrorY: "+mirrorY);
            Log.i(TAG, "rotateFrontImage if matrixMirrorY: " + matrixMirrorY);
    
    
        }
    
        rotateRight.preRotate(90);
    
         rImg= Bitmap.createBitmap(imgBitmap, 0, 0, imgBitmap.getWidth(), imgBitmap.getHeight(), rotateRight, true);
    
    
    }
    
    else
    {
    
    
    
        Matrix rotateRight = new Matrix();
        rotateRight.preRotate(270);
    
        if(android.os.Build.VERSION.SDK_INT>13 &&  cameraId == CameraInfo.CAMERA_FACING_FRONT)
        {
    
    
            float[] mirrorY = { -1, 0, 0, 0, 1, 0, 0, 0, 1};
            rotateRight = new Matrix();
            Matrix matrixMirrorY = new Matrix();
            matrixMirrorY.setValues(mirrorY);
    
            rotateRight.postConcat(matrixMirrorY);
    
            Log.i(TAG, "rotateFrontImage else mirrorY: "+mirrorY);
            Log.i(TAG,"rotateFrontImage else matrixMirrorY: "+matrixMirrorY);
    
            rotateRight.preRotate(270);
    
        }
    
        rImg= Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), rotateRight, true);
    
    
    }
    
     return rImg;
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-14
      • 1970-01-01
      • 2016-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多