如何获得正常的相机视图在Surfaceview android? [英] How to get normal Camera view in Surfaceview android?

查看:222
本文介绍了如何获得正常的相机视图在Surfaceview android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我做一个示例应用程序,我必须在surfaceview中使用摄像头。我已经成功地设置在surfaceview相机,但我不能得到正常的相机视图中,宽度和高度改变。

Right now, i am doing an sample app where i have to use camera in surfaceview. I have successfully set the camera in surfaceview but i can't get the normal camera view in it, width and height gets changed. Below is my code, i am pleased to get any ideas from anyone.

相机类别:

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类:

SurfaceHolder class:

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.xml

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

</FrameLayout>

推荐答案

如果你想要显示相机没有失真, c $ c> layout_height 用于预览框。如果您知道相机支持176x144预览大小,这是您真正想要使用的,那么只需设置预览的布局参数,

If you want your to display the camera without distortion, you should override layout_height for preview frame. If you know that the camera supports 176x144 preview size, and this is what you really want to use, then simply set the layout parameters of preview,

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

这篇关于如何获得正常的相机视图在Surfaceview android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆