How to capture photo automatically in android phone?

14,172

Solution 1

Follow the steps outlined in the Android Developer reference pages. There's no requirement to have a 'shutter button'. You can create a dummy SurfaceHolder if you don't want to show the image on the screen, e.g.

SurfaceView surface = new SurfaceView(context);
cam.setPreviewDisplay(surface.getHolder());

Solution 2

write this code in on create to auto capture image

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custom__camera_activity);
    mCamera = getCameraInstance();
    mCameraPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mCameraPreview);


    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

   {



                mCamera.takePicture(null, null, mPicture);

            }


    }
    }, 5500);
    }

Solution 3

    public int intPicTaken;


    // setPreviewCallback on the camera, wait intil intPicTaken increments to 10, then take the picture
    cam.setPreviewCallback(prevCallBack);

    public Camera.PreviewCallback prevCallBack = new Camera.PreviewCallback() {
        @Override
        public void onPreviewFrame(byte[] data, Camera camera) {
            intPicTaken++;
            try {
                if(intPicTaken == 10) {
                doTakePicture();
                }
            } catch (Exception e) {
                System.out.println("onPreviewFrame: " + e.toString());
            }
        }
    };

    public Camera.PictureCallback mPicture = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            System.out.println("PictureCallback onPictureTaken");
            try {

                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 1;
                Bitmap picture = BitmapFactory.decodeByteArray(data, 0, data.length, options);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                picture.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                baos.close();
                System.out.println("PictureCallback onPictureTaken done");
                cam.release();
                saveFile(picture);
            } catch (Exception e) {
                System.out.println("onPictureTaken: " + e.toString());
            }
        }
    };

    // take the picture
    public void doTakePicture() {
        try {

            cam.stopPreview();
            cam.takePicture(null, null, mPicture, mPicture);
        } catch(Exception e){
            System.out.println("doTakePicture: " + e.toString());
        }
    }

    // saving the file to gallery 
    public void saveFile(Bitmap bitmap) {
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaStorageDir = Environment.getExternalStorageDirectory();
        if (! mediaStorageDir.exists()){
            if (! mediaStorageDir.mkdirs()){
                System.out.println("saveFile: failed to create directory");
                return;
            }
        }
        try {
            String saved = MediaStore.Images.Media.insertImage(this.getContentResolver(), bitmap, "title", "description");
            Uri sdCardUri = Uri.parse("file://" + Environment.getExternalStorageDirectory());
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, sdCardUri));
            System.out.println("file saved");
        } catch (Exception e) {
            System.out.println("saveFile: " + e.toString());
            e.printStackTrace();
        }
    }
Share:
14,172
Selva
Author by

Selva

Updated on June 04, 2022

Comments

  • Selva
    Selva almost 2 years

    I have developed an android application. In that i have used front facing camera functionality. Its working fine but I need to auto capture. i.e. without click shutter button sound, I want to capture photocode..

    my camera activity code is

     private Camera openFrontFacingCameraGingerbread() { 
        int cameraCount = 0; 
        Camera cam = null; 
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo(); 
        cameraCount = Camera.getNumberOfCameras(); 
        for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) { 
            Camera.getCameraInfo( camIdx, cameraInfo ); 
            if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT  ) { 
                try { 
                    cam = Camera.open( camIdx ); 
                } catch (RuntimeException e) { 
                    Log.i("Camera failed to open: ",e.getLocalizedMessage()); 
                } 
            } 
        } 
        return cam; 
    } 
    

    Thanks in advance..

  • Selva
    Selva over 12 years
    i want to capture photo.without any click or any event.my device is fixed one area.first it scan QR code and next take person photo and next process do.user can't do nothing.. I want result same as barcode scan or QR scan
  • Glitch
    Glitch over 12 years
    Perhaps I'm not understanding your problem, but it should just be a matter of calling takePicture() and passing in callbacks (which implement the logic to store/manipulate the image).