2012-04-23 186 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