Android 安卓正面摄像头拍摄倒置照片

Android 安卓正面摄像头拍摄倒置照片,android,camera,Android,Camera,我有一个以纵向模式运行的应用程序,作为一个活动的一部分,我有一个相机对象作为片段在其中运行 我可以选择从前置相机切换到后置相机,用后置相机拍照时一切都很好 但当我用前向相机拍照时,它们会反转180度。 现在我知道,这可能与纵向模式下的方向有关,但将其置于横向模式只会扼杀我的应用程序的想法 是否有任何方法可以修复此问题,使拍摄的照片与预览中看到的照片相同 listener = new OrientationEventListener(this.getActivity(),Senso

我有一个以纵向模式运行的应用程序,作为一个活动的一部分,我有一个相机对象作为片段在其中运行

我可以选择从前置相机切换到后置相机,用后置相机拍照时一切都很好

但当我用前向相机拍照时,它们会反转180度。 现在我知道,这可能与纵向模式下的方向有关,但将其置于横向模式只会扼杀我的应用程序的想法

是否有任何方法可以修复此问题,使拍摄的照片与预览中看到的照片相同

        listener = new OrientationEventListener(this.getActivity(),SensorManager.SENSOR_DELAY_NORMAL){

        @Override
        public void onOrientationChanged(int orientation) {
            // TODO Auto-generated method stub
            if (orientation == ORIENTATION_UNKNOWN) return;
             android.hardware.Camera.CameraInfo info =
                    new android.hardware.Camera.CameraInfo();
             android.hardware.Camera.getCameraInfo(mCurrentCamera, info);
             orientation = (orientation + 45) / 90 * 90;
             int rotation = 0;
             if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
                 rotation = (info.orientation - orientation + 360) % 360;
             } else {  // back-facing camera
                 rotation = (info.orientation + orientation) % 360;
             }
             if (mCamera.getParameters() != null){
             Camera.Parameters mParameters = mCamera.getParameters();

             mParameters.setRotation(rotation);
             mCamera.setParameters(mParameters);
             }
        }

    };
    listener.enable();
    if (listener.canDetectOrientation())
        Log.d("Orientation Possible", "Orientation Possible");
问题是,在我尝试拍照后,这东西崩溃了。此外,如果它在预览模式下运行一段时间,它将再次崩溃。我可能还应该补充一点,在另一种方法中,我正在这样做

   public void switchCamera(Camera camera) {
    setCamera(camera);
    try {
        camera.setPreviewDisplay(mHolder);
    } catch (IOException exception) {
        Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
    }
    Camera.Parameters parameters = camera.getParameters();
    parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height);

    requestLayout();


    try{
        camera.setParameters(parameters);
    }
    catch (RuntimeException ex){
        Log.d("Preview", "Failure to set proper parameters");
        //need to improve this method
        if (mSupportedPreviewSizes != null) {
            Camera.Size cs = mSupportedPreviewSizes.get(0);
            parameters.setPreviewSize(cs.width, cs.height);
            camera.setParameters(parameters);
            requestLayout();
            //int tempheight = mPreviewSize.height;
            //mPreviewSize.height = mPreviewSize.width;
            //mPreviewSize.width = tempheight;

        }
    }
在摄影机之间切换时(从后向前,反之亦然)会调用此选项。这会干扰定向事件侦听器吗

另外,当保存拍摄的照片时,这就是我所说的。以前它只是把数据作为一个参数,但我试着让它也接受屏幕方向。问题是,无论发生什么情况,屏幕方向始终为90。因此,位图将始终旋转90度(这对于使用后置相机拍照非常有用),但在使用前置相机拍照时,位图会反转。是否可以在此函数中实施修复

  public static Bitmap MakeSquare(byte[] data, int orientation) {
    int width;
    int height;
    // Rotate photo
    Matrix matrix = new Matrix();
    matrix.postRotate(orientation);
    // Convert ByteArray to Bitmap
    Bitmap bitPic = BitmapFactory.decodeByteArray(data, 0, data.length);
    width = bitPic.getWidth();
    height = bitPic.getHeight();


    // Create new Bitmap out of the old one
    Bitmap bitPicFinal = Bitmap.createBitmap(bitPic, 0, 0, width, height,matrix, true);
    bitPic.recycle();
    int desWidth;
    int desHeight;
    desWidth = bitPicFinal.getWidth();
    desHeight = desWidth;
    Bitmap croppedBitmap = Bitmap.createBitmap(bitPicFinal, 0,bitPicFinal.getHeight() / 2 - bitPicFinal.getWidth() / 2,desWidth, desHeight);
    croppedBitmap = Bitmap.createScaledBitmap(croppedBitmap, 528, 528, true);
    return croppedBitmap;
}

请尝试以下链接:

)

)

具体来说,这里有一段来自setRotation链接的详细信息

“如果应用程序希望旋转图片以匹配用户看到的方向,应用程序应使用OrientationEventListener和Camera.CameraInfo。来自OrientationEventListener的值与设备的自然方向相关。CameraInfo.orientation是摄影机方向和自然设备方向之间的角度。这两者之和是后向摄影机的旋转角度。两者的区别在于前向摄像头的旋转角度。请注意,前向摄像头的JPEG图片不会像预览显示中那样镜像。”

我按原样使用了这段代码,根本没有修改。我的相机现在更好了,但仍然需要一些TLC。我也没有正面功能


祝你好运!

好吧。看来我已经处理好了。我用了下面的建议:

并将我的MakeSquare函数更改为:

public static Bitmap MakeSquare(byte[] data, int cameraID) {
    int width;
    int height;
    Matrix matrix = new Matrix();
    Camera.CameraInfo info = new Camera.CameraInfo();
    android.hardware.Camera.getCameraInfo(cameraID, info);
    // Convert ByteArray to Bitmap
    Bitmap bitPic = BitmapFactory.decodeByteArray(data, 0, data.length);
    width = bitPic.getWidth();
    height = bitPic.getHeight();

    // Perform matrix rotations/mirrors depending on camera that took the photo
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT)
    {
        float[] mirrorY = { -1, 0, 0, 0, 1, 0, 0, 0, 1};
        Matrix matrixMirrorY = new Matrix();
        matrixMirrorY.setValues(mirrorY);

        matrix.postConcat(matrixMirrorY);
    }

    matrix.postRotate(90);


    // Create new Bitmap out of the old one
    Bitmap bitPicFinal = Bitmap.createBitmap(bitPic, 0, 0, width, height,matrix, true);
    bitPic.recycle();
    int desWidth;
    int desHeight;
    desWidth = bitPicFinal.getWidth();
    desHeight = desWidth;
    Bitmap croppedBitmap = Bitmap.createBitmap(bitPicFinal, 0,bitPicFinal.getHeight() / 2 - bitPicFinal.getWidth() / 2,desWidth, desHeight);
    croppedBitmap = Bitmap.createScaledBitmap(croppedBitmap, 528, 528, true);
    return croppedBitmap;
}

这似乎奏效了。虽然我不确定这是最好的方法,但我很满意。现在我所要做的就是找出为什么在使用前置摄像头时它没有采用正确的高宽比。

谢谢,我已经看过了他们,并尝试按照他们建议的方式实现了一个onOrientationChanged listener,但我坚持崩溃。我已经编辑了我的帖子来显示我的代码谢谢!你有任何关于这个解决方案在“所有”设备上如何工作的信息吗?解决了我的前摄像头图像旋转问题(y)我看不出这可能如何工作。一些设备(如Nexus 6P)显示图像反转,而其他设备(如Galaxy Tab Pro 8.4)不要这样做。你的代码总是翻转图像,这会在三星设备上中断,除非我误解了你的代码。太好了,将图像旋转为纵向,也不会翻转x轴,谢谢。