Android Camera Activity类 - 实现SurfaceView - 问题

时间:2012-08-28 12:27:23

标签: android android-camera

我正在尝试使用SurfaceView类创建Camera Activity类但它给了我错误。

这是我的错误日志猫

08-28 12:21:11.558: E/AndroidRuntime(989): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.cameraapp/com.example.cameraapp.CameraSurfaceView}: java.lang.InstantiationException: com.example.cameraapp.CameraSurfaceView
08-28 12:21:11.558: E/AndroidRuntime(989):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
08-28 12:21:11.558: E/AndroidRuntime(989):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-28 12:21:11.558: E/AndroidRuntime(989):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-28 12:21:11.558: E/AndroidRuntime(989):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-28 12:21:11.558: E/AndroidRuntime(989):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 12:21:11.558: E/AndroidRuntime(989):  at android.os.Looper.loop(Looper.java:123)
08-28 12:21:11.558: E/AndroidRuntime(989):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-28 12:21:11.558: E/AndroidRuntime(989):  at java.lang.reflect.Method.invokeNative(Native Method)
08-28 12:21:11.558: E/AndroidRuntime(989):  at java.lang.reflect.Method.invoke(Method.java:507)
08-28 12:21:11.558: E/AndroidRuntime(989):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-28 12:21:11.558: E/AndroidRuntime(989):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-28 12:21:11.558: E/AndroidRuntime(989):  at dalvik.system.NativeStart.main(Native Method)
08-28 12:21:11.558: E/AndroidRuntime(989): Caused by: java.lang.InstantiationException: com.example.cameraapp.CameraSurfaceView
08-28 12:21:11.558: E/AndroidRuntime(989):  at java.lang.Class.newInstanceImpl(Native Method)
08-28 12:21:11.558: E/AndroidRuntime(989):  at java.lang.Class.newInstance(Class.java:1409)
08-28 12:21:11.558: E/AndroidRuntime(989):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-28 12:21:11.558: E/AndroidRuntime(989):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
08-28 12:21:11.558: E/AndroidRuntime(989):  ... 11 more

这是我的CameraSurfaceActivity:

package com.example.cameraapp;

import java.io.IOException;

import android.content.Context;
import android.view.SurfaceView;
import android.view.SurfaceHolder;
import android.hardware.Camera;

public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback
{
        private SurfaceHolder holder;
        private Camera camera;

        public CameraSurfaceView(Context context) 
        {
                super(context);

                //Initiate the Surface Holder properly
                this.holder = this.getHolder();
                this.holder.addCallback(this);
                this.holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        }

        public void surfaceCreated(SurfaceHolder holder) 
        {
                try
                {
                        //Open the Camera in preview mode
                        this.camera = Camera.open();
                        this.camera.setPreviewDisplay(this.holder);
                }
                catch(IOException ioe)
                {
                        ioe.printStackTrace(System.out);
                }
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) 
        {
                // Now that the size is known, set up the camera parameters and begin
                // the preview.
                Camera.Parameters parameters = camera.getParameters();
                parameters.setPreviewSize(width, height);
                camera.setParameters(parameters);
                camera.startPreview();
        }



        public void surfaceDestroyed(SurfaceHolder holder) 
        {
                // Surface will be destroyed when replaced with a new screen
                //Always make sure to release the Camera instance
                camera.stopPreview();
                camera.release();
                camera = null;
        }

        public Camera getCamera()
        {
                return this.camera;
        }
}

在Manifest中,我也定义了权限,

    android:targetSdkVersion="15" />
    <uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />

现在,请指导我寻找可能的解决方案

由于

Shumaila

1 个答案:

答案 0 :(得分:0)

错误发生在camera.setParameters(width, height);

查看此主题:Camera crashes in Android 2.2

  

需要在获得最佳相机尺寸后设置相机的预览尺寸。以下是修复的详细信息和代码 -   http://code.google.com/p/android/issues/detail?id=7909