2013-12-16 57 views
6

重發起定製相機,而在做預覽模式變化,從橫向到縱向縱向到橫向,我的面類的代碼如下所示:禁用取向變化

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 {  
      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 
     } 
    } 

    @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); 

      } 
       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

您還需要在配置更改時管理預覽的高度和寬度。 – TheLittleNaruto

回答

12

您必須爲您的應用程序處理配置更改。

將此行添加到您的AndroidManifest.xml中。

android:configChanges="keyboardHidden|orientation|screenSize" 

這告訴系統哪些配置更改你要自己處理 - 無爲而在這種情況下。

希望它可以幫助ツ

+1

感謝它的工作... – PankajAndroid

+0

很棒..它讓你感到高興:) –

0

這不是從你的問題,你抱怨什麼明確的,但你似乎只操縱相機參數,調用從未mCamera.setParameters(parameters)。僅這一點可能會導致混淆。

在不支持此參數的設備上使用parameters.set("orientation", "landscape");可能導致RuntimeExceptionmCamera.setParameters(parameters)中。因此,我們通常會使用單獨的嘗試...來捕捉此設置。