2013-12-13 68 views
1

每當我在風景模式下使用相機進行縱向模式時,我爲縱向和橫向模式準備了兩種不同的佈局。在風景模式下使用相機以縱向模式進行預覽

像仍然我在縱向模式下預覽無論我使用的相機在風景或肖像模式,但一旦我刪除此行:mCamera.setDisplayOrientation(90);我得到預覽在風景模式下,無論是我在橫向或縱向模式下使用相機

PreviewSurface.java:-

public class PreviewSurface extends SurfaceView implements 
    SurfaceHolder.Callback { 

     public static final String LOG_TAG = "CameraPreview"; 
     private SurfaceHolder mSurfaceHolder; 

     private Camera mCamera; 

     // Constructor that obtains context and camera 
     @SuppressWarnings("deprecation") 
     public PreviewSurface(Context context, Camera camera) { 
      super(context); 
      this.mCamera = camera; 

      this.mSurfaceHolder = this.getHolder(); 
      this.mSurfaceHolder.addCallback(this); 
      this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
      this.mSurfaceHolder.setFixedSize(100, 100); 
     } 

     @Override 
     public void surfaceCreated(SurfaceHolder surfaceHolder) { 
      try {  
       mCamera.setPreviewDisplay(surfaceHolder); 
       mCamera.startPreview(); 

      } catch (IOException e) { 
       // left blank for now 
      } 
     } 

     @Override 
     public void surfaceDestroyed(SurfaceHolder surfaceHolder) { 
      mCamera.stopPreview(); 
      mCamera.release(); 
     } 

     @Override 
     public void surfaceChanged(SurfaceHolder surfaceHolder, int format, 
       int width, int height) { 

       // start preview with new settings 
       try { 
       mCamera.setDisplayOrientation(90); 
       mCamera.setPreviewDisplay(surfaceHolder); 
       mCamera.startPreview(); 
       } catch (Exception e) { 
       // intentionally left blank for a test 
      } 
     } 

回答

6

我建議你使用:Configuration.ORIENTATION_LANDSCAPE支持景觀預覽

編輯答案:

@Override 
    public void surfaceCreated(SurfaceHolder surfaceHolder) { 
     try {  
      Camera.Parameters parameters = mCamera.getParameters(); 
      if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) { 
       parameters.set("orientation", "portrait"); 
       mCamera.setDisplayOrientation(90); 
       parameters.setRotation(90); 
      } 
       else { 
         // This is an undocumented although widely known feature 
         parameters.set("orientation", "landscape"); 
         // For Android 2.2 and above 
         mCamera.setDisplayOrientation(0); 
         // Uncomment for Android 2.0 and above 
         parameters.setRotation(0); 
      } 
      mCamera.setPreviewDisplay(surfaceHolder); 
      mCamera.startPreview(); 

     } catch (IOException e) { 
      // left blank for now 
     } 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) { 
     mCamera.stopPreview(); 
     mCamera.release(); 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder surfaceHolder, int format, 
      int width, int height) { 

     try {  
      Camera.Parameters parameters = mCamera.getParameters(); 
      if (this.getResources().getConfiguration().orientation != Configuration.ORIENTATION_LANDSCAPE) { 
       parameters.set("orientation", "portrait"); 
       mCamera.setDisplayOrientation(90); 
       parameters.setRotation(90); 
       mCamera.setPreviewDisplay(surfaceHolder); 
        mCamera.startPreview(); 
      } 
       else { 
         // This is an undocumented although widely known feature 
         parameters.set("orientation", "landscape"); 
         // For Android 2.2 and above 
         mCamera.setDisplayOrientation(0); 
         // Uncomment for Android 2.0 and above 
         parameters.setRotation(0); 
      } 
      mCamera.setPreviewDisplay(surfaceHolder); 
      mCamera.startPreview(); 

     } catch (IOException e) { 
      // left blank for now 
     }   
    } 
+0

確定但不知道在哪裏以及如何?我必須使用這 – Sun

+0

這不是很好,但不是一個問題,我可以理解你的挫敗感 – Android

相關問題