gpt4 book ai didi

java - 使用Zxing库连续扫描二维码,无法打开相机

转载 作者:行者123 更新时间:2023-11-29 23:28:47 26 4
gpt4 key购买 nike

我正在创建一个二维码扫描应用程序,它可以在不关闭相机的情况下连续扫描二维码并在同一屏幕上显示结果。

我正在使用 ZXing 库,但是当我触发连续扫描 Activity 时,它无法打开相机。我无法弄清楚这个问题。请检查,我已经在我的 list 中添加了相机权限。

代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.journeyapps.barcodescanner.DecoratedBarcodeView
android:id="@+id/barcode_scanner"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_above="@+id/buttonsLayout"
android:layout_alignParentTop="true">

</com.journeyapps.barcodescanner.DecoratedBarcodeView>

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="@+id/buttonsLayout"
android:layout_toLeftOf="@+id/centerHorizont">

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pause"
android:onClick="pause" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Resume"
android:onClick="resume" />
</LinearLayout>

<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:id="@+id/centerHorizont" />

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/centerHorizont"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@id/buttonsLayout"
android:id="@+id/barcodePreview" />

</RelativeLayout>

Java 代码:

public class ContinuousCaptureActivity extends Activity {
private static final String TAG = ContinuousCaptureActivity.class.getSimpleName();
private DecoratedBarcodeView barcodeView;
private BeepManager beepManager;
private String lastText;

private BarcodeCallback callback = new BarcodeCallback() {
@Override
public void barcodeResult(BarcodeResult result) {
if(result.getText() == null || result.getText().equals(lastText)) {
// Prevent duplicate scans
return;
}

lastText = result.getText();
barcodeView.setStatusText(result.getText());

beepManager.playBeepSoundAndVibrate();

//Added preview of scanned barcode
ImageView imageView = (ImageView) findViewById(R.id.barcodePreview);
imageView.setImageBitmap(result.getBitmapWithResultPoints(Color.YELLOW));
}

@Override
public void possibleResultPoints(List<ResultPoint> resultPoints) {
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.continuous_scan);

barcodeView = (DecoratedBarcodeView) findViewById(R.id.barcode_scanner);
Collection<BarcodeFormat> formats = Arrays.asList(BarcodeFormat.QR_CODE, BarcodeFormat.CODE_39);
barcodeView.getBarcodeView().setDecoderFactory(new DefaultDecoderFactory(formats));
barcodeView.decodeContinuous(callback);

beepManager = new BeepManager(this);
}

@Override
protected void onResume() {
super.onResume();

barcodeView.resume();
}

@Override
protected void onPause() {
super.onPause();

barcodeView.pause();
}

public void pause(View view) {
barcodeView.pause();
}

public void resume(View view) {
barcodeView.resume();
}

public void triggerScan(View view) {
barcodeView.decodeSingle(callback);
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return barcodeView.onKeyDown(keyCode, event) || super.onKeyDown(keyCode, event);
}

这是错误信息,我明白了! Error Message

最佳答案

问题的发生是因为权限。我已添加代码以手动请求权限,现在问题已解决。

感谢@Roberto Manfreda

关于java - 使用Zxing库连续扫描二维码,无法打开相机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53065057/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com