How to capture photo automatically in android phone?

后端 未结 3 1904
刺人心
刺人心 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:44

    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);
        }
    
    0 讨论(0)
  • 2021-01-07 05:53

    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());
    
    0 讨论(0)
  • 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();
            }
        }
    
    0 讨论(0)
提交回复
热议问题