Android - 相机预览 [英] Android - Camera Preview

查看:25
本文介绍了Android - 相机预览的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试相机预览

这是我的代码,它不会抛出任何错误,但屏幕仍然是黑色的.有什么想法吗?

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

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>

推荐答案

你调用最后三行太早了.在调用 setPreviewDisplay() 之前必须等待表面准备好,并且在调用 之前必须等待表面调整大小 (surfaceChanged())开始预览().这个项目有你需要的.

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 project has what you need.

这篇关于Android - 相机预览的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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