Android相机显示黑屏

时间:2014-03-03 02:23:07

标签: android camera

我正在尝试创建基于Android的基本相机应用。我已经介绍了基础知识,SurfaceView + SurfaceHolder实现在一个旨在拥有全屏SurfaceView的活动中。在surfaceCreated中,我在try catch中调用camera = Camera.open(),在运行时没有异常被抛出。我在SurfaceView上设置了一个带有onClick功能的ImageView,基本上充当了我的“拍照”按钮。所有这一切都很好,除了我加载表面视图活动时唯一看到的是一个黑屏,ImageView处于正确的位置。然后,当我在ImageView上执行单击时,应用程序崩溃并显示附加的logcat错误。 stacktrace指向imageView的onClick

中的行
  

camera.takePicture(...)

有关图像预览为何黑色且takePicture功能失败的任何想法? 谢谢!

logcat的

03-02 17:22:29.002: E/AndroidRuntime(24787): FATAL EXCEPTION: main
03-02 17:22:29.002: E/AndroidRuntime(24787): java.lang.RuntimeException: takePicture     failed
03-02 17:22:29.002: E/AndroidRuntime(24787):    at     android.hardware.Camera.native_takePicture(Native Method)
03-02 17:22:29.002: E/AndroidRuntime(24787):    at  android.hardware.Camera.takePicture(Camera.java:1194)
03-02 17:22:29.002: E/AndroidRuntime(24787):    at    android.hardware.Camera.takePicture(Camera.java:1139)
03-02 17:22:29.002: E/AndroidRuntime(24787):    at charles.michaels.getinsideyourface.CaptureActivity$4.onClick(CaptureActivity.java:58)
03-02 17:22:29.002: E/AndroidRuntime(24787):    at android.view.View.performClick(View.java:4489)
03-02 17:22:29.002: E/AndroidRuntime(24787):    at android.view.View$PerformClick.run(View.java:18803)
03-02 17:22:29.002: E/AndroidRuntime(24787):    at android.os.Handler.handleCallback(Handler.java:730)
03-02 17:22:29.002: E/AndroidRuntime(24787):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-02 17:22:29.002: E/AndroidRuntime(24787):    at android.os.Looper.loop(Looper.java:137)
03-02 17:22:29.002: E/AndroidRuntime(24787):    at android.app.ActivityThread.main(ActivityThread.java:5455)
03-02 17:22:29.002: E/AndroidRuntime(24787):    at java.lang.reflect.Method.invokeNative(Native Method)
03-02 17:22:29.002: E/AndroidRuntime(24787):    at java.lang.reflect.Method.invoke(Method.java:525)
03-02 17:22:29.002: E/AndroidRuntime(24787):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
03-02 17:22:29.002: E/AndroidRuntime(24787):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
03-02 17:22:29.002: E/AndroidRuntime(24787):    at dalvik.system.NativeStart.main(Native Method)

surfaceHolder覆盖

public void surfaceCreated(SurfaceHolder holder)
{
    Log.e(TAG, "surfaceCreated");
    try{
        camera = Camera.open();
        Log.d(TAG,"You opened that camera yo");
    }
    catch(Exception e){
        Log.e(TAG,e.toString());
    }
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
{
    Log.e(TAG, "surfaceChanged");
    try
    {
        if (previewRunning) {
            camera.stopPreview();
        }

        Camera.Parameters p = camera.getParameters();
        p.setPreviewSize(w, h);
        camera.setParameters(p);
        camera.setPreviewDisplay(holder);
        camera.startPreview();
        previewRunning = true;
    }
    catch(Exception e)
    {
        Log.d(TAG,e.toString());
    }
}

public void surfaceDestroyed(SurfaceHolder holder)
{
    Log.e(TAG, "surfaceDestroyed");
    camera.stopPreview();
    previewRunning = false;
    camera.release();
    camera=null;
}

onClick功能

    iv.setOnClickListener(new OnClickListener(){
        public void onClick(View view){         
            {

                camera.takePicture(mShutterCallback, mPictureCallback,mjpeg);
            }
        }

    });    

0 个答案:

没有答案