Android - 相机预览
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2456802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - Camera Preview
提问by david
i'm trying the camera preview
我正在尝试相机预览
This is my code and it doesn't throw any error, but the screen is still black. Any ideas?
这是我的代码,它没有抛出任何错误,但屏幕仍然是黑色的。有任何想法吗?
this.setContentView(R.layout.camerapreview);
SurfaceView cameraSurface = (SurfaceView)findViewById(R.id.cpPreview);
SurfaceHolder holder = cameraSurface.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
this.camera = Camera.open();
this.camera.setPreviewDisplay(holder);
this.camera.startPreview();
camerapreview.xml
相机预览文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<SurfaceView
android:id="@+id/cpPreview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
</SurfaceView>
</LinearLayout>
回答by CommonsWare
You are calling the last three lines too early. You have to wait for the surface to be prepared before calling setPreviewDisplay()
and you have to wait for the surface to be sized (surfaceChanged()
) before calling startPreview()
. This projecthas what you need.
你太早调用最后三行了。在调用之前必须等待表面准备好,setPreviewDisplay()
并且在调用之前必须等待表面调整大小 ( surfaceChanged()
) startPreview()
。这个项目有你需要的。