我正在使用ZXing Library和its port of the Android Application通过Intent从Android应用中扫描条形码和QR码。我在Gradle依赖项中添加了以下两行,以便在不进行修改的情况下使用android-integration代码:
compile 'com.journeyapps:zxing-android-embedded:3.2.0@aar'
compile 'com.google.zxing:core:3.2.1'
我在我的活动中使用IntentIntegrator
来扫描onCreate()
中的条形码,如下所示:
integrator = new IntentIntegrator(this);
integrator.setOrientationLocked(false);
integrator.setPrompt(getString(R.string.scanner_text)); // Set the text of the scanner
integrator.setCameraId(0); // Use a specific camera of the device
integrator.setBeepEnabled(true); // Enable beep in the scanner
integrator.setBarcodeImageEnabled(false); // Do not fetch image from the camera
integrator.initiateScan();
一切正常,我得到正确的扫描结果,但我想在扫描仪的右下角有一个闪光按钮,如下所示:
我已经可以使用音量增大和减小键来控制闪光灯了,因为我覆盖了CaptureActivity
。
是否有像上面那样的闪光按钮,条码扫描器可以在AUTO,ON和OFF模式之间切换?如果有,我可以使用addExtra()
的{{1}}方法来激活它吗?或者实现这一点的唯一方法是根据我的需要修改整个代码?
答案 0 :(得分:1)
我忽略了this page on Embedding BarcodeView和these sample activities,它们展示了如何根据您的需要自定义条形码扫描仪。帮助我的示例活动是CustomScannerActivity
。
IntentIntegrator
类中没有选项可以本地实现Flash按钮。相反,我应该为条形码扫描仪制作自定义布局,在自定义活动中使用它并从IntentIntegrator
调用此活动。
我有两项活动。一个是ScannerActivity
,另一个是CallingActivity
。困扰我一段时间的错误是我在IntentIntegrator
onCreate()
方法中创建了ScannerActivity
的实例。它应该在CallingActivity
中。
在给定的示例中,使用Button
并根据闪存更改Button
的文本。我创建了一个名为activity_custom_scanner
的新Android布局,我在其中用ToggleButton替换了Button,并使用按钮的图像来获取我想要的Flash开/关按钮。
所以我的ScannerActivity看起来像这样:
public class CustomScannerActivity extends Activity implements
CompoundBarcodeView.TorchListener {
private static final int BarCodeScannerViewControllerUserCanceledErrorCode = 99991;
private static final String TAG = CustomScannerActivity.class.getSimpleName();
private CaptureManager capture;
private CompoundBarcodeView barcodeScannerView;
private ToggleButton switchFlashlightButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_scanner);
barcodeScannerView = (CompoundBarcodeView)findViewById(R.id.zxing_barcode_scanner);
barcodeScannerView.setTorchListener(this);
switchFlashlightButton = (ToggleButton)findViewById(R.id.switch_flashlight);
switchFlashlightButton.setText(null);
switchFlashlightButton.setTextOn(null);
switchFlashlightButton.setTextOff(null);
// if the device does not have flashlight in its camera,
// then remove the switch flashlight button...
if (!hasFlash()) {
switchFlashlightButton.setVisibility(View.GONE);
}
switchFlashlightButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// Save the state here
if (isChecked) {
barcodeScannerView.setTorchOn();
} else {
barcodeScannerView.setTorchOff();
}
}
});
capture = new CaptureManager(this, barcodeScannerView);
capture.initializeFromIntent(getIntent(), savedInstanceState);
capture.decode();
}
@Override
protected void onResume() {
super.onResume();
capture.onResume();
}
@Override
protected void onPause() {
super.onPause();
capture.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
capture.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
capture.onSaveInstanceState(outState);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return barcodeScannerView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}
/**
* Check if the device's camera has a Flashlight.
* @return true if there is Flashlight, otherwise false.
*/
private boolean hasFlash() {
return getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
}
@Override
public void onTorchOn() {
// necessary override..
}
@Override
public void onTorchOff() {
// necessary override..
}
}
CallingActivity
看起来像这样:
public class CallingActivity extends Activity {
private static final String TAG = CallingActivity.class.getSimpleName();
private static final int BarCodeScannerViewControllerUserCanceledErrorCode = 99991;
String uuid;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uuid = getIntent().getStringExtra("uuid");
new IntentIntegrator(this).setOrientationLocked(false).setCaptureActivity(CustomScannerActivity.class).initiateScan();
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
if (scanResult != null) {
// handle scan result
Log.i(TAG, "Text from Barcode Scanner: " + scanResult.getContents());
getIntent().putExtra("data", scanResult.getContents());
getIntent().putExtra("uuid", uuid);
}
}
else if (resultCode == RESULT_CANCELED) {
getIntent().putExtra("error", "User canceled");
getIntent().putExtra("error_code", BarCodeScannerViewControllerUserCanceledErrorCode);
}
else
{
getIntent().putExtra("error", getString(R.string.scanner_error));
getIntent().putExtra("error_code", BarCodeScannerViewControllerUserCanceledErrorCode);
}
setResult(resultCode, this.getIntent());
this.finish();
}
}
我不确定它是否是完美的方式,但这就是我如何做到的。
希望它有所帮助!
答案 1 :(得分:0)
CompoundBarcodeView中有一个setTorchOn方法,因此您可以检查该方法并尝试根据您的需要实现它。希望它有所帮助。
答案 2 :(得分:0)
这个问题可能太老了,但您可以在扫描时使用音量按钮打开/关闭手电筒。