我已经为QR扫描仪集成了ZBar库。但是它在我的其中一台设备中无法正常运行。相机无法自动对焦在专门的Samsung Galaxy J7 +手机上。我们已经签入了其他电话,例如摩托罗拉,Xiomi,Oneplus,并且工作正常。 那么谁能建议我应该做些什么改变呢? 这是我的代码。
@Override
protected void onStart() {
super.onStart();
initCamera();
}
@Override
protected void onStop() {
super.onStop();
releaseCamera();
}
private void initCamera() {
autoFocusHandler = new Handler();
mCamera = getCameraInstance();
/* Instance barcode scanner */
scanner = new ImageScanner();
scanner.setConfig(0, Config.X_DENSITY, 1);
scanner.setConfig(0, Config.Y_DENSITY, 1);
scanner.setConfig(0, Config.ENABLE, 0);
// Only enable the codes your app requires
scanner.setConfig(Symbol.QRCODE, Config.ENABLE, 1);
CameraPreview mPreview = new CameraPreview(this, mCamera, previewCb, getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_AUTOFOCUS) ? autoFocusCB : null);
if (mBinding.preview.getChildCount() > 0) {
mBinding.preview.removeAllViews();
}
mBinding.preview.addView(mPreview);
}
/**
* A safe way to get an instance of the Camera object.
*/
public static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open();
} catch (Exception e) {
e.printStackTrace();
}
return c;
}
private void releaseCamera() {
if (mCamera != null) {
previewing = false;
mCamera.setPreviewCallback(null);
mCamera.release();
mCamera = null;
}
}
private Runnable doAutoFocus = new Runnable() {
public void run() {
if (previewing)
mCamera.autoFocus(autoFocusCB);
}
};
Camera.PreviewCallback previewCb = new Camera.PreviewCallback() {
public void onPreviewFrame(byte[] data, Camera camera) {
Camera.Parameters parameters = camera.getParameters();
Camera.Size size = parameters.getPreviewSize();
Image barcode = new Image(size.width, size.height, "Y800");
barcode.setData(data);
int result = scanner.scanImage(barcode);
if (result != 0) {
previewing = false;
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
SymbolSet syms = scanner.getResults();
for (Symbol sym : syms) {
final String code = sym.getData();
}
}
}
};
// Mimic continuous auto-focusing
Camera.AutoFocusCallback autoFocusCB = new Camera.AutoFocusCallback() {
public void onAutoFocus(boolean success, Camera camera) {
autoFocusHandler.postDelayed(doAutoFocus, 1000);
}
};