2014-10-01 42 views
0

現在,我正在做一個示例應用程序,我必須在surfaceview中使用相機。我已經成功地在Surfaceview中設置了相機,但是我無法在其中獲得正常的相機視圖,寬度和高度也會發生變化。以下是我的代碼,我很高興得到任何人的任何想法。如何在Surfaceview android中獲得正常的攝像頭視圖?

Camera類:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.camera); 
      cameraObject = isCameraAvailiable(); 
    showCamera = new ShowCamera(this, cameraObject); 
    preview.addView(showCamera); 
      takePhotoButton.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
     cameraObject.takePicture(null, null, capturedIt); 
     cameraObject.stopPreview(); 
     preview.removeView(showCamera); 
     cameraObject.release(); 
     cameraObject = Camera.open(); 
     showCamera = new ShowCamera(Photo.this, cameraObject); 

     preview.addView(showCamera); 
     } 
    }); 
} 

SurfaceHolder類:

public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback { 

private SurfaceHolder holdMe; 
private Camera theCamera; 

public ShowCamera(Context context, Camera camera) { 
    super(context); 
    theCamera = camera; 
    holdMe = getHolder(); 
    holdMe.addCallback(this); 
} 

public void surfaceChanged(SurfaceHolder holder, int format, int width, 
     int height) { 
    theCamera.stopPreview(); 
    theCamera.setDisplayOrientation(90); 
    camWidth = width; 
    camHeight = height; 
    theCamera.startPreview(); 
    initPreview(width, height); 
} 

private void initPreview(int width, int height) { 
    // Log.i(TAG, "initPreview()starts"); 

    if (theCamera != null) { 
     try { 
      Camera.Parameters param; 
      param = camera.getParameters(); 
      param.setPreviewSize(176, 144); 
      camera.setParameters(param); 
      theCamera.setPreviewDisplay(holdMe); 
     } catch (Throwable t) { 
      Log.e("PreviewDemo-surfaceCallback", 
        "Exception in setPreviewDisplay()", t); 
     } 
    } 

    // Log.i(TAG, "initPreview() ends"); 
} 

@Override 
public void surfaceCreated(SurfaceHolder holder) { 
    try { 
     theCamera.setDisplayOrientation(90); 
     theCamera.setPreviewDisplay(holdme); 
     theCamera.startPreview(); 
    } catch (IOException e) { 
    } 
} 

public void surfaceDestroyed(SurfaceHolder arg0) { 
    this.getHolder().removeCallback(this); 
    theCamera.stopPreview(); 
    theCamera.release(); 
} 

}

Framelayout.xml

<FrameLayout 
    android:id="@+id/preview1" 
    android:layout_width="match_parent" 
    android:layout_height="200dp" 
    android:layout_alignParentLeft="true"> 

</FrameLayout> 

enter image description here

+0

的佈局參數在你** **相機類,有會員**預覽**。它是如何定義和初始化的? – 2014-10-01 08:55:58

+0

上面的代碼是一個,你可以看到它。 – Vicky 2014-10-01 09:30:24

+0

不,我只看到像'preview.addView()'或'preview.removeView()'這樣的語句,但沒有一個像'preview - new Preview(width,height);'。 – 2014-10-01 13:06:49

回答

0

如果您希望顯示相機而不失真,則應該覆蓋layout_height預覽幀。如果您知道該相機支持176×144預覽大小,這是你真的想用什麼,然後簡單地設置預覽

showCamera = new ShowCamera(this, cameraObject); 

ViewGroup.LayoutParams lp = preview.getLayoutParams(); 
lp.width = getWindowManager().getDefaultDisplay().getWidth(); 
//*** no, this is wrong!! *** lp.height = lp.width*144/176; 
lp.height = lp.width*176/144; // that's correct for Camera.setDisplayOrientation(90) !! 
preview.setLayoutParams(lp); 

preview.addView(showCamera); 
+0

我已經用上面的解決方案設置寬度和高度,但它仍然是一樣的。 – Vicky 2014-10-06 11:51:45

+0

如果我設置cameraview的高度爲200dp它顯示圖像在strech我怎麼能得到正常圖像 – Vicky 2014-10-06 11:54:49

+0

你應該改變預覽的佈局,然後再調用'addView(showCamera)'。但是,如果您不關心不同的屏幕尺寸,只需計算屏幕寬度* 144/176並將其設置爲您的XML中的layout_height。你應該使用** px **而不是** dp **,或者使用相關的縮放因子。如果你的屏幕截圖是1:1,並且屏幕寬度真的是** 200px **,那麼高度應該設置爲** 163px ** – 2014-10-06 12:02:04

相關問題