2012-04-23 189 views
15

我有这个应用程序在肖像模式下运行,并作为一项活动的一部分,我有一个摄像头对象作为片段运行。Android前置摄像头拍摄倒影照片

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

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

回答

34

好。我似乎有点照顾它。我从这次的建议: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; 
} 

这似乎是工作和做的伎俩。虽然我不确定这是否是最好的方式,但我很满意。现在我所要做的就是弄清楚为什么在使用前置摄像头时没有考虑到正确的宽高比。

+0

谢谢!您是否有关于如何在“所有”设备上使用此解决方案的信息? – marino 2014-08-26 23:54:45

+1

解决了我的前置摄像头图像旋转问题(y) – 2014-09-29 10:23:33

+0

这是最好的一个..谢谢 – tsanzol 2015-08-19 08:34:12

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。我也没有前置功能。

祝你好运!

+0

谢谢,我已经看过他们,试图按照他们的建议实施onOrientationChanged监听器,但我一直在崩溃。我编辑了我的帖子以显示我的代码 – Valentin 2012-04-25 08:18:10