我正在使用Zxing扫描仪实现条形码扫描仪,此部分一切正常,但是现在我想在布局上添加一个按钮(以打开/关闭手电筒)。我在互联网上到处检查,但一无所获。
所以,这是我的问题:
是否有可能发现或修改.xml以添加按钮?如果是,如何找到该文件?
是否已经在某处实现了在布局中添加一些元素的功能?
这就是我叫ZXingScannerView的方式:
scannerView = new ZXingScannerView(this);
setContentView(scannerView);
使用ZXing库自定义QR /条形码扫描仪非常有用
https://github.com/journeyapps/zxing-android-embedded
您可以在此处看到示例应用程序项目,并自己进行自定义(添加按钮,设置闪光灯开/关)。
还有另一个库,但它没有使用ZXing。
https://github.com/googlesamples/android-vision
已弃用,现在是ML Kit的一部分,但仍然可以使用。
希望它对您有所帮助。谢谢。
[更新]
请将此库导入您的项目。 (您可以从下面的链接中看到如何导入它。)
https://github.com/journeyapps/zxing-android-embedded#adding-aar-dependency-with-gradle
导入后,您可以更新条形码扫描仪活动的layout.xml。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/topLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true">
<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="@+id/barcode_scanner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
<Button
android:id="@+id/btn_flash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:text="Flash"/>
</FrameLayout>
com.journeyapps.barcodescanner.DecoratedBarcodeView是BarcodeScanner视图。按钮只是打开/关闭闪光灯。
这是BarcodeScanner的活动。
public class ScanQRActivity extends BaseActivity {
private static final String TAG = "ScanQRActivity";
private DecoratedBarcodeView barcodeView;
private boolean isFlashOn;
/**
* Initializes the UI and creates the detector pipeline.
*/
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_scan_qr);
isFlashOn = false;
barcodeView = findViewById(R.id.barcode_scanner);
Collection<BarcodeFormat> formats = Arrays.asList(BarcodeFormat.CODE_39); // Set barcode type
barcodeView.getBarcodeView().setDecoderFactory(new DefaultDecoderFactory(formats));
barcodeView.initializeFromIntent(getIntent());
barcodeView.decodeContinuous(callback);
Button btnFlash = findViewById(R.id.btn_flash);
if (!hasFlash()) {
btnFlash.setVisibility(View.GONE);
}
btnFlash.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switchFlashlight();
}
});
}
private BarcodeCallback callback = new BarcodeCallback() {
@Override
public void barcodeResult(BarcodeResult result) {
Log.e(TAG, result.getText()); // QR/Barcode result
}
@Override
public void possibleResultPoints(List<ResultPoint> resultPoints) {
}
};
/**
* 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);
}
public void switchFlashlight() {
if (isFlashOn) {
isFlashOn = false;
barcodeView.setTorchOff();
} else {
isFlashOn = true;
barcodeView.setTorchOn();
}
}
@Override
protected void onResume() {
super.onResume();
barcodeView.resume();
}
@Override
protected void onPause() {
super.onPause();
barcodeView.pause();
}
}
扫描QR /条形码时,可以从BarcodeCallback.barcodeResult函数获得结果。
希望对您有所帮助。如有任何疑问,请在此处评论。