【问题标题】:Auto capture image from camera?从相机自动捕捉图像?
【发布时间】:2017-06-19 03:34:23
【问题描述】:

我有 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);
}

【问题讨论】:

  • 发布您的代码,以便我们提出更改建议
  • 添加了请检查,否则应该添加什么来自动捕获图片

标签: android camera


【解决方案1】:

添加以下代码:

写一个方法

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

然后在你的 onResult 中调用它

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



                }



    }

要在您的相机预览课中回答您的问题,请添加:

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

【讨论】:

  • 什么是相机...?
  • 假设您之前已经初始化了相机,我已经给了您这段代码。
  • 不,这是我的代码,我只是在相机中添加了意图,即使我没有照片处理程序类
  • 什么是shutterCallback、rawCallback、jpegCallback
  • 查看编辑了解shutterCallback、rawCallback、jpegCallback
猜你喜欢
  • 1970-01-01
  • 2016-01-09
  • 2020-03-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多