Emma22
Emma22

Reputation: 23

Take picture with front camera in android service

I need to take a picture with the back camera in an android service but after reading the docs it seems you need a SurfaceView is it possible to take a picture without showing anything to the user?

Edit: Will this work?

SurfaceTexture surfaceTexture = new SurfaceTexture(10);
Camera camera = Camera.open();
camera.getParameters().setPreviewSize(1, 1);
camera.setPreviewTexture(surfaceTexture);
camera.startPreview();
camera.takePicture(null, pictureCallback, null);

Upvotes: 1

Views: 1635

Answers (3)

iamkdblue
iamkdblue

Reputation: 3622

100% working . click picture from front camera using service .

public class MyService extends Service {

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        CapturePhoto();
    }

    private void CapturePhoto() {

        Log.d("kkkk","Preparing to take photo");
        Camera camera = null;

        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();

            int frontCamera = 1;
            //int backCamera=0;

            Camera.getCameraInfo(frontCamera, cameraInfo);

            try {
                camera = Camera.open(frontCamera);
            } catch (RuntimeException e) {
                Log.d("kkkk","Camera not available: " + 1);
                camera = null;
                //e.printStackTrace();
            }
            try {
                if (null == camera) {
                    Log.d("kkkk","Could not get camera instance");
                } else {
                    Log.d("kkkk","Got the camera, creating the dummy surface texture");
                     try {
                         camera.setPreviewTexture(new SurfaceTexture(0));
                        camera.startPreview();
                    } catch (Exception e) {
                        Log.d("kkkk","Could not set the surface preview texture");
                        e.printStackTrace();
                    }
                    camera.takePicture(null, null, new Camera.PictureCallback() {

                        @Override
                        public void onPictureTaken(byte[] data, Camera camera) {
                            File pictureFileDir=new File("/sdcard/CaptureByService");

                            if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
                                pictureFileDir.mkdirs();
                            }
                            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
                            String date = dateFormat.format(new Date());
                            String photoFile = "ServiceClickedPic_" + "_" + date + ".jpg";
                            String filename = pictureFileDir.getPath() + File.separator + photoFile;
                            File mainPicture = new File(filename);

                            try {
                                FileOutputStream fos = new FileOutputStream(mainPicture);
                                fos.write(data);
                                fos.close();
                                Log.d("kkkk","image saved");
                            } catch (Exception error) {
                                Log.d("kkkk","Image could not be saved");
                            }
                            camera.release();
                        }
                    });
                }
            } catch (Exception e) {
                camera.release();
            }
    }
}

Upvotes: 2

Rajan Kali
Rajan Kali

Reputation: 12953

You can set Width and Height of SurfaceView to 1dp and margintop to -10,so that it wont display it on screen but it functions as normal and you can Take picture without displaying Surface View to User

<SurfaceView
android:layout_width="1dp"
android:layout_height="1dp"
android:layout_marginTop="-10dp"
 ...
/>

in that case you can just use SurfaceTexture

   SurfaceTexture surfaceTexture = new SurfaceTexture(10);
    Camera camera = Camera.open();
    camera.getParameters().setPreviewSize(1, 1);
    camera.setPreviewTexture(surfaceTexture);

Upvotes: 1

Ashish Shukla
Ashish Shukla

Reputation: 1047

Yes it is possible. call your Camera Callback method to take picture from camera and Dont specify the size for preview and just start the preview it will work .

Omit this Step :

  param.setPreviewSize(122,133);   

or use

  param.setPreviewSize(1, 1);

Upvotes: 0

Related Questions