How to capture photo automatically in android phone?

后端 未结 3 1905
刺人心
刺人心 2021-01-07 05:04

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 butto

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-07 05:58

        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();
            }
        }
    

提交回复
热议问题