使用前置摄像头拍摄的照片颠倒

时间:2016-03-30 20:25:05

标签: android android-camera

我的项目中有自定义相机实现。 当我使用该设备的后置摄像头时,它工作正常。 在前置摄像头以纵向模式或反向纵向模式拍摄图像时,图像会上下颠倒。在横向模式下,两台相机都能正常工作。

我已实施SensorEventListener并计算orientation回调中的SensorChanged值,以便在捕获的文件中设置ExifInterface.TAG_ORIENTATION

public void onSensorChanged(SensorEvent event) {
        synchronized (this) {
            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {


                if (event.values[0] < 4 && event.values[0] > -4) {
                    if (event.values[1] > 0 && orientation != ExifInterface.ORIENTATION_ROTATE_90) {
                        // UP
                        orientation = ExifInterface.ORIENTATION_ROTATE_90;
                    } else if (event.values[1] < 0 && orientation != ExifInterface.ORIENTATION_ROTATE_270) {
                        // UP SIDE DOWN
                        orientation = ExifInterface.ORIENTATION_ROTATE_270;
                    }
                } else if (event.values[1] < 4 && event.values[1] > -4) {
                    if (event.values[0] > 0 && orientation != ExifInterface.ORIENTATION_NORMAL) {
                        // LEFT
                        orientation = ExifInterface.ORIENTATION_NORMAL;
                    } else if (event.values[0] < 0 && orientation != ExifInterface.ORIENTATION_ROTATE_180) {
                        // RIGHT
                        orientation = ExifInterface.ORIENTATION_ROTATE_180;
                    }
                }

            }

        }
    }

我使用SensorEventListener的原因是因为我在用户旋转屏幕时向屏幕上的图标添加旋转动画。 这是我的PictureCallback

private Camera.PictureCallback mPictureCallBack = new Camera.PictureCallback() {

        public void onPictureTaken(byte[] data, Camera camera) {
            mCamera.stopPreview();

            File pictureFile = new File(fileName);

            try {
                FileOutputStream purge = new FileOutputStream(pictureFile);
                purge.write(data);
                purge.close();
            } catch (FileNotFoundException e) {

            } catch (IOException e) {

            }

            // Adding Exif data for the orientation.
            try {

                exif = new ExifInterface(fileName);
                exif.setAttribute(ExifInterface.TAG_ORIENTATION, "" + orientation);
                exif.saveAttributes();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    };

对于调试,当我exif.setAttribute() orientation值为ExifInterface.ORIENTATION_ROTATE_90以防ExifInterface.ORIENTATION_ROTATE_270Note :,反之亦然时,它正常工作。 我的问题是解决这个问题的最佳和最可靠的事情是什么,以便最适合所有设备。

data我没有在整个过程中使用Bitmaps。我将django-cors-headers写入文件并将其发送回另一个活动以显示捕获的图像。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

前几天我遇到了这个问题。我所做的是在使用正面镜头相机时旋转图像,照顾设备的负旋转。因此,要转换轮换,您必须执行以下操作:

  //check first in which camera we are
public boolean isFrontCamera(int cameraIndex) {
    try {
        if (mDeviceManager != null) {
            CameraCharacteristics characteristics = mDeviceManager.getCameraManager().getCameraCharacteristics(String.valueOf(cameraIndex));

            return characteristics.get(CameraCharacteristics.LENS_FACING)
                    == CameraCharacteristics.LENS_FACING_FRONT;
        }
    } catch (CameraAccessException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace();
    }

    return false;
}

  boolean mirror = isFrontCamera(Integer.parseInt(mCameraId));
                if (mirror) {
                    //Mirror image rotation is equal to the negative rotation of the device, but some external apps are not able to read negative numbers
                    //in image details, we do this to prevent the crash of this apps
                    rotation = 360 - rotation;
                    if (rotation == 360) {
                        rotation = 0;
                    }
                }

稍后您必须先旋转图像,然后再将其添加到视图中,您可以使用以下矩阵:

 public static Bitmap rotateImage(Bitmap imageToOrient, int degreesToRotate) {
    Bitmap result = imageToOrient;
    try {
        if (degreesToRotate != 0) {
            Matrix matrix = new Matrix();
            matrix.setRotate(degreesToRotate);
            result = Bitmap.createBitmap(
                    imageToOrient,
                    0,
                    0,
                    imageToOrient.getWidth(),
                    imageToOrient.getHeight(),
                    matrix,
                    true);
        }
    } catch (Exception e) {
        if (Log.isLoggable(TAG, Log.ERROR)) {
            Log.e(TAG, "Exception when trying to orient image", e);
        }
    }
    return result;
}

我知道您没有使用Bitmap,但您可以修改方法以将其发送到byteArray或您需要的任何内容。

希望它会对你有所帮助! 最好的问候!