【问题标题】:Android Front Facing Camera Taking Inverted PhotosAndroid 前置摄像头拍摄倒置照片
【发布时间】:2012-04-23 15:24:58
【问题描述】:

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

我可以选择从前置摄像头切换到后置摄像头,使用后置摄像头拍照时一切正常。

不过,当我使用前置摄像头拍照时,它们会反转 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;
}

【问题讨论】:

    标签: android camera


    【解决方案1】:

    好吧好吧。看来我有点照顾它了。我使用了以下建议:Android, front and back camera Orientation , Landscape

    并将我的 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;
    }
    

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

    【讨论】:

    • 谢谢你!您有关于如何在“所有”设备上运行此解决方案的任何信息吗?
    • 解决了我的前置摄像头图像旋转问题(y)
    • 我看不出这怎么可能工作。某些设备(例如 Nexus 6P)显示反转的图像,而其他设备(例如 Galaxy Tab Pro 8.4)则不会。你的代码总是在翻转图像,这会在三星设备上中断,除非我误解了你的代码。
    • 太棒了,将图像旋转成纵向并且也不翻转x轴,谢谢。
    【解决方案2】:

    试试这些链接:

    http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation(int)

    http://developer.android.com/reference/android/hardware/Camera.Parameters.html#setRotation(int)

    具体来说,这里有一些来自 setRotation 链接的废话。

    "如果应用程序想要旋转图片以匹配用户看到的方向,应用程序应该使用 OrientationEventListener 和 Camera.CameraInfo。OrientationEventListener 的值是相对于设备的自然方向。CameraInfo.orientation 是两者之间的角度摄像头方向和自然设备方向。两者之和是后置摄像头的旋转角度。两者之差是前置摄像头的旋转角度。注意JPEG图片的前置摄像头不像预览显示那样镜像。”

    我按原样使用此代码,根本没有修改它。我的相机现在好多了,不过仍然需要一些 TLC。我还没有前置功能。

    祝你好运!

    【讨论】:

    • 谢谢,我已经看过他们并尝试按照他们建议的方式实现 onOrientationChanged 侦听器,但我不断崩溃。我编辑了我的帖子以显示我的代码
    猜你喜欢
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多