Take Picture without preview Android
Asked Answered
P

4

10

I would like to take a picture without any preview. I used this code but I'm getting an error:

 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();

    }
};

I get a NullPointerException when I try to access camera. I don't know how to initialize it. I think the problem is that camera is never initialized.

 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)

I initialize camera by: camera= Camera.open();

Pul answered 29/5, 2012 at 13:16 Comment(4)
@PadmaKumar i update my post with the stacktracePul
@Pul may you help me ? I need an app that can capture picture with front and back camera . I try your app but it not work in some device !Spiritualism
@محمد This is a thread old of 2 years, a better approach would be to open a new one or put the link herePul
can you show me a better approach !? I cant find a good sample for take picture in background ! :(Spiritualism
M
10

Use this permissions :

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

And you can instantiate a Camera object using this

Camera mycamera = Camera.open();

You can try the method described here is very simple if you are into simplicity:

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

Maddox answered 29/5, 2012 at 13:19 Comment(4)
thanks for the response. I initialize camera and now i get a new error see my editPul
@HamzaKarmouda have you set the permissions in your manifest file ?Maddox
yes of course. i don't know why it can't connect camera to servicePul
you're right, to initialize the camera we have to do Camera.open()and don't forget to close the camera to avoid Fail to connect service ErrorPul
P
0

Android can't use camera without a preview... but, you can set the preview size like 1x1 px, then you won't see it and you can use the camera anyway.

Plant answered 12/6, 2014 at 10:4 Comment(0)
C
0

Regarding your Fail to initialize camera error, another possibility is the camera is in use by something else.

Childress answered 22/11, 2014 at 23:27 Comment(0)
C
0

This is because the camera does not work immediately and often not synchronously. Some devices require significant latency between startPreview () and takePicture ().

For Eg: The Samsung devices require atleast 100ms to start the camera.

You can resolve this error by putting a delay for takePicture() method.

mview.postDelayed(new Runnable() {
    @Override
    public void run() {
        try {
            camera.takePicture(null, null, photoCallback);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}, 100);
Collettecolletti answered 18/9, 2021 at 5:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.