10

我想拍一张没有任何预览的照片。我使用了这段代码,但出现错误:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Camera.Parameters parameters = camera.getParameters();
    parameters.setPictureFormat(PixelFormat.JPEG);
    camera.setParameters(parameters);
    SurfaceView mview = new SurfaceView(getBaseContext());
    try {
        camera.setPreviewDisplay(mview.getHolder());
        camera.startPreview();
        camera.takePicture(null,null,photoCallback);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }




}

 Camera.PictureCallback photoCallback=new Camera.PictureCallback() {
    public void onPictureTaken(byte[] data, Camera camera) {

        Uri uriTarget = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, new ContentValues());
        OutputStream imageFileOS;

        try {

            imageFileOS = getContentResolver().openOutputStream(uriTarget);
            imageFileOS.write(data);
            imageFileOS.flush();
            imageFileOS.close();

            Toast.makeText(AndroidTestJNIActivity.this, "Image saved: " + uriTarget.toString(), Toast.LENGTH_LONG).show();

        } catch (FileNotFoundException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();

        }
        finish();

    }
};

当我尝试访问相机时收到 NullPointerException。我不知道如何初始化它。我认为问题在于相机从未初始化。

 05-29 14:40:20.330: E/AndroidRuntime(15571): FATAL EXCEPTION: main
 05-29 14:40:20.330: E/AndroidRuntime(15571): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.achillessecurity.androidtestjni/com.achillessecurity.androidtestjni.AndroidTestJNIActivity}: java.lang.RuntimeException: Fail to connect to camera service
 05-29 14:40:20.330: E/AndroidRuntime(15571):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
 05-29 14:40:20.330: E/AndroidRuntime(15571):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
05-29 14:40:20.330: E/AndroidRuntime(15571):    at android.app.ActivityThread.access$1500(ActivityThread.java:123)
 05-29 14:40:20.330: E/AndroidRuntime(15571):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
 05-29 14:40:20.330: E/AndroidRuntime(15571):   at android.os.Handler.dispatchMessage(Handler.java:99)
 05-29 14:40:20.330: E/AndroidRuntime(15571):   at android.os.Looper.loop(Looper.java:130)
 05-29 14:40:20.330: E/AndroidRuntime(15571):   at android.app.ActivityThread.main(ActivityThread.java:3835)
 05-29 14:40:20.330: E/AndroidRuntime(15571):   at java.lang.reflect.Method.invokeNative(Native Method)
 05-29 14:40:20.330: E/AndroidRuntime(15571):   at java.lang.reflect.Method.invoke(Method.java:507)
 05-29 14:40:20.330: E/AndroidRuntime(15571):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
 05-29 14:40:20.330: E/AndroidRuntime(15571):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
 05-29 14:40:20.330: E/AndroidRuntime(15571):   at dalvik.system.NativeStart.main(Native Method)
 05-29 14:40:20.330: E/AndroidRuntime(15571): Caused by: java.lang.RuntimeException: Fail to connect to camera service
 05-29 14:40:20.330: E/AndroidRuntime(15571):   at android.hardware.Camera.native_setup(Native Method)
 05-29 14:40:20.330: E/AndroidRuntime(15571):   at android.hardware.Camera.<init>(Camera.java:258)
 05-29 14:40:20.330: E/AndroidRuntime(15571):   at android.hardware.Camera.open(Camera.java:235)
 05-29 14:40:20.330: E/AndroidRuntime(15571):   at com.achillessecurity.androidtestjni.AndroidTestJNIActivity.onCreate(AndroidTestJNIActivity.java:42)
 05-29 14:40:20.330: E/AndroidRuntime(15571):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 05-29 14:40:20.330: E/AndroidRuntime(15571):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)

我通过以下方式初始化相机: camera= Camera.open();

4

4 回答 4

10

使用此权限:

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

你可以使用这个实例化一个相机对象

Camera mycamera = Camera.open();

如果您很简单,可以尝试这里描述的方法非常简单:

http://developer.android.com/training/camera/photobasics.html

于 2012-05-29T13:19:31.487 回答
0

Android不能在没有预览的情况下使用相机......但是,您可以将预览大小设置为1x1 px,然后您将看不到它,您仍然可以使用相机。

于 2014-06-12T10:04:19.350 回答
0

关于您的Fail to initialize camera错误,另一种可能性是相机正在被其他东西使用。

于 2014-11-22T23:27:50.953 回答
0

这是因为相机不能立即工作,而且经常不同步。startPreview ()某些设备需要和之间的显着延迟takePicture ()

例如:三星设备需要至少 100 毫秒才能启动相机。

您可以通过延迟takePicture()方法来解决此错误。

mview.postDelayed(new Runnable() {
    @Override
    public void run() {
        try {
            camera.takePicture(null, null, photoCallback);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}, 100);
于 2021-09-18T05:13:25.263 回答