1

我正在开发相机 API。它在 4.0 和 4.1 中完美运行,但是当我在 Android 2.3 或更低版本的设备上尝试它时,它在 surfacechanged 功能中崩溃。我正在使用前置摄像头拍照。这是我的日志:

11-03 03:37:11.756: E/AndroidRuntime(11316): FATAL EXCEPTION: main
11-03 03:37:11.756: E/AndroidRuntime(11316): java.lang.RuntimeException: startPreview failed
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.hardware.Camera.startPreview(Native Method)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at com.test.CameraSurfaceView.surfaceChanged(CameraSurfaceView.java:42)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.view.SurfaceView.updateWindow(SurfaceView.java:557)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.view.SurfaceView.dispatchDraw(SurfaceView.java:348)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.view.View.draw(View.java:6988)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.view.ViewGroup.drawChild(ViewGroup.java:1732)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.view.View.draw(View.java:6988)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.view.ViewGroup.drawChild(ViewGroup.java:1732)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.view.ViewGroup.drawChild(ViewGroup.java:1730)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1459)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.view.View.draw(View.java:6988)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1993)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.view.ViewRoot.draw(ViewRoot.java:1614)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.view.ViewRoot.performTraversals(ViewRoot.java:1335)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1973)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.os.Looper.loop(Looper.java:150)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at android.app.ActivityThread.main(ActivityThread.java:4293)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at java.lang.reflect.Method.invokeNative(Native Method)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at java.lang.reflect.Method.invoke(Method.java:507)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-03 03:37:11.756: E/AndroidRuntime(11316):    at dalvik.system.NativeStart.main(Native Method)

这是我的代码:

class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

    Camera camera;
    private int cameraId = 0;

    CameraSurfaceView(Context context) {
        super(context);

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        SurfaceHolder holder = this.getHolder();
        holder.addCallback(this);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {

        // The default orientation is landscape, so for a portrait app like this
        // one we need to rotate the view 90 degrees.
//      camera.setDisplayOrientation(90);

        // IMPORTANT: We must call startPreview() on the camera before we take
        // any pictures

//      camera.startPreview();


//      camera.setParameters(parameters);
        camera.startPreview();

    }

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

    }

    @Override
    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;
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        // TODO Auto-generated method stub
    }

    public void takePicture(PictureCallback imageCallback) {
        camera.takePicture(null, null, imageCallback);
    }

    private int findFrontFacingCamera() {
        int cameraId = -1;
        // Search for the front facing camera
        int numberOfCameras = Camera.getNumberOfCameras();
        for (int i = 0; i < numberOfCameras; i++) {
            CameraInfo info = new CameraInfo();
            Camera.getCameraInfo(i, info);
            if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {

            Log.e("Found", "Camera found");
            cameraId = i;
            break;
        }
    }
    return cameraId;
}

}

4

1 回答 1

-1

在前面加上这个

             if (camera != null) {
        try {
            if (previewing) {
                camera.stopPreview();
                previewing = false;
                Log.d("", "previewing :");
            }

            camera.setPreviewDisplay(holder);
            camera.startPreview();

            previewing = true;
            } catch (IOException e) {
            e.printStackTrace();
        }
    }
于 2013-05-18T10:00:35.750 回答