-5

I am looking for a way for taking a picture automatic in android without user interaction , I can open and take a picture but i can't take automatic picutre?

public void capturePhoto() {
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(G.DIR_APP + "/tmp")));
        startActivityForResult(intent, TAKE_PICTURE);
    }
Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
Saeed Parand
  • 33
  • 1
  • 1
  • 9
  • 1
    Possible duplicate of [Take a photo automatically without user interaction](http://stackoverflow.com/questions/9752730/take-a-photo-automatically-without-user-interaction) – Netero Jan 08 '16 at 16:32
  • Capturing Image without user interaction(without user action) and capture an image in background using android service http://chandandroid.blogspot.in/2014/04/capturing-image-without-user-action.html – chand becse Feb 09 '18 at 13:27

1 Answers1

0

You can use an approach like this

private Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
    @Override
    public void onPictureTaken(final byte[] bytes,final Camera camera) {

        // Do something here ... save, display ...
    }
};
public void takePictureBack(ControllerState state){

    Camera camera = null;
    int cameraCount = Camera.getNumberOfCameras();
    for (int cameraId = 0; cameraId < cameraCount; cameraId++) {
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                camera = Camera.open(cameraId);
                break;
            }
        }




    if (camera!= null) {


        Camera.Parameters params = camera.getParameters();

        // Check what resolutions are supported by your camera
        List<Camera.Size> sizes = params.getSupportedPictureSizes();


        // Iterate through all available resolutions and choose one.    
        for (Camera.Size size : sizes) {
            ... 
        }




        params.setPictureSize( ... );
        camera.setParameters(params);

        camera.setPreviewDisplay(mCameraSourcePreview.getSurfaceView().getHolder());
        camera.startPreview();      
        camera.takePicture(null,null,mPictureCallback)

    }
}

However be careful since Android does not allow this kind of behaviour when the user is not aware of a picture being taken. The SurfaceView you use for the preview needs to have visibility visible.

Radu Ionescu
  • 3,462
  • 5
  • 24
  • 43