2011-03-22 48 views
1

我希望在手機旋轉時不要改變相機的方向,使相機能夠以任何方向拍攝垂直照片。目前,圖片在除一個方向以外的所有方向上旋轉不正確。測試HTC Incredible運行2.2:如何實現Android相機以任何屏幕方向拍照?

  • 肖像模式有圖像順時針旋轉90度它應該是什麼。
  • 設備從肖像逆時針旋轉產生旋轉180度的圖像。
  • 設備從肖像順時針旋轉產生正確方向的圖像。

下面是相機執行相關的代碼:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.camera); 

    // Set up camera's SurfaceView and SurfaceHolder 
    surfaceView = (SurfaceView) findViewById(R.id.camera_surface); 
    surfaceHolder = surfaceView.getHolder(); 
    surfaceHolder.addCallback(this); 
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

} 

public void surfaceChanged(SurfaceHolder holder, int format, int width, 
     int height) { 
    Camera.Parameters p = camera.getParameters(); 
    p.setPreviewSize(width,height); 
} 

public void surfaceCreated(SurfaceHolder holder) { 
    camera = Camera.open(); 
    try { 
     camera.setPreviewDisplay(surfaceHolder); 
    } catch (IOException e) { 
     camera.release(); 
    } 
    camera.startPreview(); 
} 

由於我想應用程序可與任何API級別等於或高於4兼容最大化的用戶羣,我如果答案不使用特定於更高API級別的任何內容,則更喜歡。謝謝!

回答

0

試試這個,但我認爲這是錯誤。我也有這個問題,但在我的情況下,我找不到任何東西。

此代碼可能是正確的,也許必須幫助你,當你使用相機預覽。

如果沒有 - 此代碼不幫你。我沒有使用相機,所以它不能幫助我。

public void surfaceCreated(SurfaceHolder holder) 
{ 
    try 
    { 
     camera.setPreviewDisplay(holder); 
     camera.setPreviewCallback(this); 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 
    Size previewSize = camera.getParameters().getPreviewSize(); 
    float aspect = (float) previewSize.width/previewSize.height; 

    int previewSurfaceWidth = preview.getWidth(); 
    int previewSurfaceHeight = preview.getHeight(); 

    LayoutParams lp = preview.getLayoutParams(); 


    if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) 
    { 
     // ïîðòðåòíûé âèä 
     camera.setDisplayOrientation(90); 
     lp.height = previewSurfaceHeight; 
     lp.width = (int) (previewSurfaceHeight/aspect); 
    } 
    else 
    { 
     camera.setDisplayOrientation(0); 
     lp.width = previewSurfaceWidth; 
     lp.height = (int) (previewSurfaceWidth/aspect); 
    } 

    preview.setLayoutParams(lp); 
    camera.startPreview(); 


} 
+0

不幸的是,setDisplayOrientation僅適用於API級別8以上:[link](http://developer.android.com/reference/android/hardware/Camera.html#setDisplayOrientation%28int%29) – 2011-03-24 04:24:15

+0

舊版API的setDisplayOrientation替代方法是Camera.Parameters parameters = mCamera .getParameters(); parameters.set(「rotation」,度); mCamera.setParameters(parameters); – 2012-06-01 12:36:20

2

如果您不想預覽也旋轉,那麼請在清單文件中將方向設置爲橫向或縱向模式。

要正確旋轉圖像,您需要在攝像頭參數上調用setRotation()(API level 5),然後在調用takePicture API之前設置該參數。你還需要使用orientationEventListener

示例代碼here

0

這應該照顧任何屏幕方向,包括不同的處理的正面和背面攝像頭:

// Set camera rotation (consider display orientation) 
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); 
int displayOrientation = display.getRotation(); 

int rotation = cameraInfo.orientation; 
if (Surface.ROTATION_0 != displayOrientation) 
{ 
    if (CameraInfo.CAMERA_FACING_BACK == cameraInfo.facing) 
    { 
     if (Surface.ROTATION_90 == displayOrientation) 
     { 
      rotation -= 90; 
     } 
     else if (Surface.ROTATION_180 == displayOrientation) 
     { 
      rotation -= 180; 
     } 
     if (Surface.ROTATION_270 == displayOrientation) 
     { 
      rotation -= 270; 
     } 

     if (rotation < 0) 
     { 
      rotation += 360; 
     } 
    } 
    else 
    { 
     if (Surface.ROTATION_90 == displayOrientation) 
     { 
      rotation += 90; 
     } 
     else if (Surface.ROTATION_180 == displayOrientation) 
     { 
      rotation += 180; 
     } 
     if (Surface.ROTATION_270 == displayOrientation) 
     { 
      rotation += 270; 
     } 

     rotation %= 360; 
    } 
} 

Log.d(TAG, "Camera orientation (" + cameraInfo.orientation + ", " + displayOrientation + ", " + rotation + ")"); 

cameraParams.setRotation(rotation); 
相關問題