繁体   English   中英

Android前置摄像头拍摄倒置照片

[英]Android Front Facing Camera Taking Inverted Photos

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

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

当我用前置摄像头拍照时,它们会被倒置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,前后摄像头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;
}

这似乎有效并且可以解决问题。 虽然我不确定这是最好的方式,但我很满意。 现在,我所要做的就是弄清楚为什么在使用前置摄像头时没有采用适当的宽高比。

尝试这些链接:

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。 我还没有前面的功能。

祝好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM