我用dm77/barcodescanner库扫描qrcode。但在我的应用程序中使用此功能时,相机的焦距为1000L,这不是所有手机的最佳参数。
如何提高相机的对焦速度?

最佳答案

我在github的help@teunvr中找到了这个问题的答案。
必须创建一个类,并从zxingscannerview扩展并重写setupcamerapreview和setautofocus。

public class ZXingAutofocusScannerView extends ZXingScannerView {

private boolean callbackFocus = false ;

public ZXingAutofocusScannerView(Context context) {
    super(context);
}

@重写
公共void设置camerapreview(camerawrapper camerawrapper){
    Camera.Parameters parameters= cameraWrapper.mCamera.getParameters();
    if(parameters != null)
    {
      try {
          parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
          cameraWrapper.mCamera.setParameters(parameters);
      }catch (Exception e)
      {
          fallbackFocus = true ;
      }
       // cameraWrapper.mCamera.getParameters()

    }


    super.setupCameraPreview(cameraWrapper);
}
@Override
public void setAutoFocus(boolean state) {
    super.setAutoFocus(callbackFocus);
}
}

现在你必须用这个类代替zxingscannerview。
public class SimpleScannerActivity extends AppCompatActivity implements
ZXingAutofocusScannerView.ResultHandler {

private ZXingAutofocusScannerView mScannerView;

@Override
public void onCreate(Bundle state) {
    super.onCreate(state);
    mScannerView = new ZXingAutofocusScannerView(this);
    setContentView(mScannerView);

@Override
public void onResume() {
    super.onResume();
    mScannerView.setResultHandler(this);
    mScannerView.startCamera();
}

@Override
public void onPause() {
    super.onPause();
    mScannerView.stopCamera();
}

@Override
public void handleResult(Result rawResult) {
    Toast.makeText(this, ""+rawResult.getText(), Toast.LENGTH_SHORT).show();
    mScannerView.resumeCameraPreview(this);
}
}

如果使用koltin,请查看以下答案:
class ZXingAutofocusScannerView(context: Context) :
ZXingScannerView(context) {
private val TAG = ZXingAutofocusScannerView::class.qualifiedName

private var callbackFocus = false

override fun setupCameraPreview(cameraWrapper: CameraWrapper?) {
    cameraWrapper?.mCamera?.parameters?.let{parameters->
        try {
            parameters.focusMode =
 Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE
            cameraWrapper.mCamera.parameters = parameters
        }catch(ex:Exception){
            Log.e(TAG, "Failed to set CONTINOUS_PICTURE", ex)
            callbackFocus = true
        }
    }
    super.setupCameraPreview(cameraWrapper)
}

override fun setAutoFocus(state: Boolean) {
    super.setAutoFocus(callbackFocus)
}
}

10-08 03:43