如何提高相机的对焦速度?

时间:2019-01-02 08:14:24

标签: android zxing barcode-scanner

我使用dm77/barcodescanner库扫描QrCode。但是在我的应用程序中使用This时,相机对焦时间为1000L,这并不是所有手机的最佳参数。

如何提高相机的对焦速度?

1 个答案:

答案 0 :(得分:3)

我在github中的@TeunVR帮助下找到了这个问题的答案。 您必须创建一个类并从ZXingScannerView和Override覆盖 setupCameraPreview setAutoFocus

public class ZXingAutofocusScannerView extends ZXingScannerView {

private boolean callbackFocus = false ;

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

@Override     公共无效setupCameraPreview(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)
}
}