简体   繁体   English

从相机自动捕获图像?

[英]Auto capture image from camera?

I have qrReader app so in ActivityResult i wrote to intent into the camera if I have response what I want that is it possible to take the picture automatically after the intent into the camera and after autofocus it ??(what should I have to add for autocapture)我有 qrReader 应用程序,所以在 ActivityResult 中,如果我有我想要的响应,我写到相机中的意图是否可以在意图进入相机并自动对焦后自动拍摄照片??(我应该添加什么自动捕获)

        button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            IntentIntegrator intentIntegrator = new IntentIntegrator(activity);
            intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
            intentIntegrator.setPrompt("Scan");
            intentIntegrator.setCameraId(0);
            intentIntegrator.setBeepEnabled(false);
            intentIntegrator.setBarcodeImageEnabled(false);
            intentIntegrator.initiateScan();

        }
    });
}

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    final IntentResult result = IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
    if(result!= null){
                if(result.getContents()==null){
                    Toast.makeText(getApplicationContext(),"you canceled the scanning",Toast.LENGTH_LONG).show();
                }
                else {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    textView.setText(result.getContents());
                    startActivityForResult(intent, TAKE_PICTURE);



                }



    }

    super.onActivityResult(requestCode, resultCode, data);
}

Add the following code:添加以下代码:

Write a method写一个方法

public void capturePhoto() throws Exception { 
        mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
        Thread.sleep(WAIT_GENERIC);
        mCamera.stopPreview();
        mCamera.release();
    } 

And call it in your onResult else as并在你的 onResult else 中调用它

button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            IntentIntegrator intentIntegrator = new IntentIntegrator(activity);
            intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.QR_CODE_TYPES);
            intentIntegrator.setPrompt("Scan");
            intentIntegrator.setCameraId(0);
            intentIntegrator.setBeepEnabled(false);
            intentIntegrator.setBarcodeImageEnabled(false);
            intentIntegrator.initiateScan();

        }
    });
}

@Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
    final IntentResult result = IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
    if(result!= null){
                if(result.getContents()==null){
                    Toast.makeText(getApplicationContext(),"you canceled the scanning",Toast.LENGTH_LONG).show();
                }
                else {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    capturePhoto();
                    textView.setText(result.getContents());
                    startActivityForResult(intent, TAKE_PICTURE);



                }



    }

To answer your question in your camera previewclass add:要在您的相机预览类中回答您的问题,请添加:

ShutterCallback shutterCallback = new ShutterCallback() {
        public void onShutter() {
            //           Log.d(TAG, "onShutter'd");
        }
    };

    PictureCallback rawCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            //           Log.d(TAG, "onPictureTaken - raw");
        }
    };

    PictureCallback jpegCallback = new PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            new SaveImageTask().execute(data);
            resetCam();
            Log.d(TAG, "onPictureTaken - jpeg");
        }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM