2014-05-15 37 views
2

我正在開發自定義相機應用程序,並遇到方向問題。設置正確的方向以預覽相機和最終結果圖像

當手機處於縱向的預覽畫面,並導致圖像顯示在景觀

enter image description here

當手機在左側橫向預覽畫面和結果正確的圖像顯示

enter image description here

當手機處於正確的橫向位置時,預​​覽屏幕和結果圖像顯示爲橫向模式,但是垂直翻轉

enter image description here

我讀過有關改變與setDisplayorientation方向,但我沒有設法解決它。

有人可以幫助我嗎?

感謝您的幫助

這是相機的活動啓動預覽代碼

@Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
     // TODO Auto-generated method stub 
     System.out.println("!!!!!!!CAMBIO!!!!!!!!"); 
     initPreview(width, height); 
     startPreview(); 
    } 


    private void initPreview(int width, int height) { 
     if (myCamera!=null && mySurfaceHolder.getSurface()!=null) { 
      try { 
       myCamera.setPreviewDisplay(mySurfaceHolder); 
      } 
      catch (Throwable t) { 
       Log.e("PreviewDemo-surfaceCallback", 
         "Exception in setPreviewDisplay()", t); 
       Toast 
       .makeText(CameraHandler.this, t.getMessage(), Toast.LENGTH_LONG) 
       .show(); 
      } 

      if (!cameraConfigured) { 
       Camera.Parameters parameters=myCamera.getParameters(); 
       Camera.Size size=getBestPreviewSize(width, height, 
         parameters); 

       if (size!=null) { 

        parameters.setPreviewSize(size.width, size.height); 

        myCamera.setParameters(parameters); 
        cameraConfigured=true; 
       } 
      } 
     } 
    } 

    private void startPreview() { 
     if (cameraConfigured && myCamera!=null) { 
      myCamera.startPreview(); 
      inPreview=true; 
     } 
    } 

private Camera.Size getBestPreviewSize(int width, int height, Camera.Parameters parameters) { 

     Camera.Size result=null; 

     for (Camera.Size size : parameters.getSupportedPreviewSizes()) { 
      System.out.println("Soportados " + size.width + "x" + size.height); 
      if (size.width<=width && size.height<=height) { 
       if (result==null) { 
        result=size; 
       } 
       else { 
        int resultArea=result.width*result.height; 
        int newArea=size.width*size.height; 

        if (newArea>resultArea) { 
         result=size; 
        } 
       } 
      } 
     } 

     return(result); 
    } 
+0

對不起,語法錯誤。 – Hanzo

+0

同樣的問題與我的方向我張貼我的解決方案在我的文章請檢查它會幫助你設置方向http://stackoverflow.com/questions/23657694/impementation-of-tuch-to-focus-on-camera-in -android – Simpalm

回答

2

setDisplayorientation()的代碼示例是主文檔中:

如果你想要使相機圖像以與顯示屏 相同的方向顯示,可以使用以下代碼。

public static void setCameraDisplayOrientation(Activity activity, 
     int cameraId, android.hardware.Camera camera) { 
    android.hardware.Camera.CameraInfo info = 
      new android.hardware.Camera.CameraInfo(); 
    android.hardware.Camera.getCameraInfo(cameraId, info); 
    int rotation = activity.getWindowManager().getDefaultDisplay() 
      .getRotation(); 
    int degrees = 0; 
    switch (rotation) { 
     case Surface.ROTATION_0: degrees = 0; break; 
     case Surface.ROTATION_90: degrees = 90; break; 
     case Surface.ROTATION_180: degrees = 180; break; 
     case Surface.ROTATION_270: degrees = 270; break; 
    } 

    int result; 
    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) { 
     result = (info.orientation + degrees) % 360; 
     result = (360 - result) % 360; // compensate the mirror 
    } else { // back-facing 
     result = (info.orientation - degrees + 360) % 360; 
    } 
    camera.setDisplayOrientation(result); 
} 

從API級14開始,該方法可以在預覽 是活性被調用。

這不適用於用Camera.takePicture()拍攝的圖片。爲此,您可以使用Camera.Parameters.setRotation()。不幸的是,有些設備根據此設置EXIF旋轉標誌,更不幸的是,一些流行的圖像瀏覽器忽略了這個標誌。在這種情況下,您必須自己旋轉生成的JPEG。爲此,您可以使用Java MediaUtil庫,請參閱https://stackoverflow.com/a/15302674/192373

+1

我解決了它,setRisplayOrientation後必須執行'setRotation'。謝謝 – Hanzo

相關問題