How to autmatically capture and save image without press capture button in Android?

945 Views Asked by At

I am using Android 5.0 to implement an application which allows to automatically capture and save an image into my phone. Currently, I am using bellow code but it requires press the capture button in Capture UI of my phone. Is it possible to capture and save the image without press the capture button? For example, I just call the function myCaptureandSave(), then the phone will display Capture UI and intermediately capture the image and save, I do not need doing more step.

        public void myCaptureandSave() {
        String image_path = Environment.getExternalStorageDirectory() +"/"+System.currentTimeMillis()+".jpg";
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        //File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        File output = new File(image_path);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(output));
        startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
        }
3

There are 3 best solutions below

0
On

You have to use Camera Api. Create Service, running in background, which hold reference to this api and current SurfaceTexture, it is necessary, camera will be 'previewing to nowhere'. after that you'll be able to take pictures even if device is locaked

5
On

Yes it is possible, but I believe you can't do it using the internal camera (calling the intent). You have to use Camera2 API.

I made a library to use Camera2 API, you can take a look at it if you want:

https://github.com/omaflak/Android-Camera2-Library

In your case, simply pass a dummy SurfaceTexture for the preview and call takePicture().

Hope it will help

0
On

how to capture an image in background without using the camera application

i tried with this time ago and with little bit changes i got it. Hope this helps..