如何在Surfaceview android中获得普通的Camera视图?

时间:2014-10-01 06:32:39

标签: android camera surfaceview

现在,我正在做一个示例应用程序,我必须在surfaceview中使用相机。我已成功将相机设置在surfaceview中,但我无法获得正常的相机视图,宽度和高度会发生变化。以下是我的代码,我很高兴收到任何人的意见。

相机类:

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

1 个答案:

答案 0 :(得分:0)

如果您希望自己无失真地显示相机,则应覆盖{strong>预览框架的layout_height。如果你知道相机支持176x144预览尺寸,这就是你真正想要使用的,那么只需设置预览的布局参数,

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