aag*_*ios 3 java xml layout android zxing
我正在使用 Zxing Scanner 实现条形码扫描仪,这部分的一切都正常,但现在我想在布局上添加一个按钮(用于打开/关闭手电筒)。我在互联网上到处查了一下,但一无所获。
所以,这是我的问题:
是否可以修改 .xml 以添加按钮?如果是,如何找到这个文件?
是否已经在某处实现了在布局中添加一些元素的功能?
这就是我调用 ZXingScannerView 的方式:
scannerView = new ZXingScannerView(this);
setContentView(scannerView);
Run Code Online (Sandbox Code Playgroud)
小智 11
使用 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>
Run Code Online (Sandbox Code Playgroud)
com.journeyapps.barcodescanner.DecolatedBarcodeView 是 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();
}
}
Run Code Online (Sandbox Code Playgroud)
当扫描 QR/Barcode 时,您可以从 BarcodeCallback.barcodeResult 函数获取结果。