在Android中尝试takePicture时失败

时间:2014-06-18 07:30:36

标签: java android camera

您好我正在使用API​​来控制Android中的相机。我正在做一个getImages函数,但是当我尝试takePicture时,我在logcat中出现了这个错误:

06-18 09:25:44.319: E/AndroidRuntime(2918): FATAL EXCEPTION: main
06-18 09:25:44.319: E/AndroidRuntime(2918): Process: org.gradiant.democamera, PID: 2918
06-18 09:25:44.319: E/AndroidRuntime(2918): java.lang.IllegalStateException: Could not execute method of the activity
06-18 09:25:44.319: E/AndroidRuntime(2918):     at android.view.View$1.onClick(View.java:3823)
06-18 09:25:44.319: E/AndroidRuntime(2918):     at android.view.View.performClick(View.java:4438)
06-18 09:25:44.319: E/AndroidRuntime(2918):     at android.view.View$PerformClick.run(View.java:18422)
06-18 09:25:44.319: E/AndroidRuntime(2918):     at android.os.Handler.handleCallback(Handler.java:733)
06-18 09:25:44.319: E/AndroidRuntime(2918):     at android.os.Handler.dispatchMessage(Handler.java:95)
06-18 09:25:44.319: E/AndroidRuntime(2918):     at android.os.Looper.loop(Looper.java:136)
06-18 09:25:44.319: E/AndroidRuntime(2918):     at android.app.ActivityThread.main(ActivityThread.java:5001)
06-18 09:25:44.319: E/AndroidRuntime(2918):     at java.lang.reflect.Method.invokeNative(Native Method)
06-18 09:25:44.319: E/AndroidRuntime(2918):     at java.lang.reflect.Method.invoke(Method.java:515)
06-18 09:25:44.319: E/AndroidRuntime(2918):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-18 09:25:44.319: E/AndroidRuntime(2918):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-18 09:25:44.319: E/AndroidRuntime(2918):     at dalvik.system.NativeStart.main(Native Method)
06-18 09:25:44.319: E/AndroidRuntime(2918): Caused by: java.lang.reflect.InvocationTargetException
06-18 09:25:44.319: E/AndroidRuntime(2918):     at java.lang.reflect.Method.invokeNative(Native Method)
06-18 09:25:44.319: E/AndroidRuntime(2918):     at java.lang.reflect.Method.invoke(Method.java:515)
06-18 09:25:44.319: E/AndroidRuntime(2918):     at android.view.View$1.onClick(View.java:3818)
06-18 09:25:44.319: E/AndroidRuntime(2918):     ... 11 more
06-18 09:25:44.319: E/AndroidRuntime(2918): Caused by: java.lang.RuntimeException: takePicture failed
06-18 09:25:44.319: E/AndroidRuntime(2918):     at android.hardware.Camera.native_takePicture(Native Method)
06-18 09:25:44.319: E/AndroidRuntime(2918):     at android.hardware.Camera.takePicture(Camera.java:1245)
06-18 09:25:44.319: E/AndroidRuntime(2918):     at android.hardware.Camera.takePicture(Camera.java:1190)
06-18 09:25:44.319: E/AndroidRuntime(2918):     at org.gradiant.camara.CameraAcquisition.GetImage(CameraAcquisition.java:146)
06-18 09:25:44.319: E/AndroidRuntime(2918):     at org.gradiant.democamera.MainActivity.getFrame(MainActivity.java:30)
06-18 09:25:44.319: E/AndroidRuntime(2918):     ... 14 more

这是我的代码:

public void GetImage() {

        mCamera = StartCapture();
        mCamera.startPreview();
        mCamera.takePicture(null, null, new PictureCallback() {

            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
                // TODO Auto-generated method stub

            }
        });

    }

    public Camera StartCapture() {

        int cameraCount = 0;
        Camera cam = null;
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        cameraCount = Camera.getNumberOfCameras();

        for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
            Camera.getCameraInfo(camIdx, cameraInfo);
            if (cameraInfo.facing == camera_used) {
                try {
                    cam = Camera.open(camIdx);
                    android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
                    android.hardware.Camera.getCameraInfo(camIdx, info);
                    int rotation = ((Activity) context_class)
                            .getWindowManager().getDefaultDisplay()
                            .getRotation();
                    int degrees = 0;
                    switch (rotation) {
                    case Surface.ROTATION_0:
                        degrees = 0;
                        break;
                    case Surface.ROTATION_90:
                        degrees = 90;
                        break;
                    case Surface.ROTATION_180:
                        degrees = 180;
                        break;
                    case Surface.ROTATION_270:
                        degrees = 270;
                        break;
                    }
                    int result;
                    if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                        result = (info.orientation + degrees) % 360;
                        result = (360 - result) % 360; // compensate the mirror
                    } else { // back-facing
                        result = (info.orientation - degrees + 360) % 360;
                    }
                    cam.setDisplayOrientation(result);

                    // other options, as resolution, format, etc
                    Camera.Parameters params = mCamera.getParameters();
                    mCamera.setParameters(params);

                } catch (RuntimeException e) {
                }
            }
        }

        return cam;

    }

问题出在哪里?我做错了什么?

由于

编辑:

根据评论意见,这些是两条相互矛盾的界限:

在MainActivity中:

camera.GetImage();

在调用方法时,在方法中:

mCamera.takePicture(null, null, new PictureCallback()

0 个答案:

没有答案