2

I'm new in android.

I found previous questions, but are quite old, actually I'm using API 23 or higher.

I'm interested in a way to obtaining a picture from a camera, without displaying the preview and without any touch or interaction of the user.

I used an intent to access to a camera app but don't let me to take a picture automatically in the way I need. This only let me to use camera app.

       Intent intentTakePic = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if(intentTakePic.resolveActivity(getPackageManager()) != null){
                startActivityForResult(intentTakePic, GET_THE_PICTURE);
            }

In future I probably need also to record the audio in the same way (without interaction).

Does anyone has suggestion for me ?

Gustavo Pagani
  • 6,583
  • 5
  • 40
  • 71
Jeek
  • 39
  • 1
  • 6
  • It is for University project, I would like to use it to take a picture of lightning or something. – Jeek Jun 28 '19 at 16:59
  • I don't know of any way to use the camera without invoking the camera app, but even if there is a way, from API 24 and up, the app must ask the user for permission to access the camera and/or photo library. So, at least sometime (maybe the first time the app is run), you'll need to get permission from the user. – Michael Dougan Jun 28 '19 at 17:22
  • Thank you for the answer, I know that the app need the user's permission to access the camera, I want to automate the shooting process because men's reaction time is too long. – Jeek Jun 28 '19 at 22:04

2 Answers2

1

You need to use the CameraAPI to take pictures without opening another camera app. https://developer.android.com/guide/topics/media/camera

You'll basically make a camera app.

// in the activity onCreate, but doesn't have to be there

        // needs explicit permission
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[] {Manifest.permission.CAMERA}, 1);
            }
        }

        final Camera camera = Camera.open();
        CameraPreview cameraPreview = new CameraPreview(this, camera);

        // preview is required. But you can just cover it up in the layout.
        FrameLayout previewFL = findViewById(R.id.preview_layout);
        previewFL.addView(cameraPreview);
        camera.startPreview();

        // take picture button
        findViewById(R.id.take_picture_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                camera.takePicture(null, null, new Camera.PictureCallback() {
                    @Override
                    public void onPictureTaken(byte[] data, Camera camera) {
                        // path of where you want to save it
                        File pictureFile = new File(getFilesDir() + "/images/pic0");

                        try {
                            FileOutputStream fos = new FileOutputStream(pictureFile);
                            fos.write(data);
                            fos.close();
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });

CameraPreview class

import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.io.IOException;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private SurfaceHolder mHolder;
    private Camera mCamera;

    public CameraPreview(Context context, Camera camera) {
        super(context);
        mCamera = camera;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null){
            // preview surface does not exist
            return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e){
            // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
Israel dela Cruz
  • 794
  • 1
  • 5
  • 11