相机preVIEW仍是黑色的 [英] Camera preview still black

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

问题描述

我想在我的Andr​​oid应用程序的摄像头preVIEW。当我尝试在我的实际设备,它给了我黑色的屏幕。

I'm trying the camera preview on my Android applications. When I try on my real devices, it gives me black screen.

这是我的code,它不会引发任何错误,但屏幕仍然是黑色的。任何想法?

This is my code and it doesn't throw any error, but the screen is still black. Any ideas?

import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.FrameLayout;

public class ARrazerNav extends Activity{

public void onCreate(Bundle savedInstanceState){
    try{
        super.onCreate(savedInstanceState);
        CustomCameraView cv = new CustomCameraView(this.getApplicationContext());
        FrameLayout r1 = new FrameLayout(this.getApplicationContext());
        setContentView(r1);
        r1.addView(r1);
    }catch (Exception e) {
        // TODO: handle exception
    }
}

public class CustomCameraView extends SurfaceView{
    Camera camera;
    SurfaceHolder previewHolder;
    public CustomCameraView(Context ctx){
        super(ctx);
        previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        previewHolder.addCallback(surfaceHolderListener);
    }
    SurfaceHolder.Callback surfaceHolderListener = new SurfaceHolder.Callback(){

        public void surfaceDestroyed(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            camera.stopPreview();
            camera.release();
        }

        public void surfaceCreated(SurfaceHolder holder) {
            // TODO Auto-generated method stub
            camera = Camera.open();
            try{
                camera.setPreviewDisplay(previewHolder);
            }catch (Throwable th) {
                // TODO: handle exception
            }
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int width,
                int height) {
            // TODO Auto-generated method stub
            Parameters params = camera.getParameters();
            params.setPreviewSize(width, height);
            params.setPictureFormat(PixelFormat.JPEG);
            camera.setParameters(params);
            camera.startPreview();

        }
    };
}

}

有人可以给我某种解释,为什么它给了我黑色的屏幕?

Can somebody give me some sort of explanation why it gives me black screen?

推荐答案

也许你正在受到机器人的相机错误

Maybe you are being affected by this bug of android Camera bug.

解决方法到这里提出各地的工作。

The workaround to which is proposed here Work around.

我希望它能帮助。

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

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