当前位置: 代码迷 >> java >> 旅程应用的条码视图仅显示黑色视图
  详细解决方案

旅程应用的条码视图仅显示黑色视图

热度:83   发布时间:2023-07-17 20:29:03.0

我已经使用设置了条形码扫描

我已经添加了依赖项,就像在库的自述文件中一样。

然后,我在我的主要活动中设置了一个 BarCode View,如下所示

main_activity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  android:paddingBottom="@dimen/activity_vertical_margin"
  tools:context=".MainActivity">

  <com.journeyapps.barcodescanner.BarcodeView
    android:id="@+id/barcode_scanner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

  </com.journeyapps.barcodescanner.BarcodeView>

</RelativeLayout>

我将 MainActivity.java 保留为默认值。 这是它的 OnCreate 方法

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }

我也在我的清单中添加了相机权限。

当我在手机上运行应用程序时。 BarCodeView 仅显示黑屏。 我在这里做错了什么吗?

在travelapp 存储库中的示例代码。

您必须分别在onResume()onPause() resumepause条形码视图。

因此,在onCreate()消息中,您必须获得BarcodeView视图的处理程序:

BarcodeView barcodeView = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.continuous_scan); 
   barcodeView = (DecoratedBarcodeView) findViewById(R.id.barcode_scanner);               
}

您还必须同时添加onResume()onPause()方法:

@Override
protected void onResume() {
    super.onResume();
    barcodeView.resume();
}

@Override
protected void onPause() {
    super.onPause();
    barcodeView.pause();
}

我知道自从提出这个问题已经 6 个月了,但也许其他人会遇到这个问题。 (我真的希望你找到解决方案)。

您的布局完全正确,您只是缺少了barcodeView 的初始化代码。

在您活动的 onResume 方法中添加:

protected void onResume () {
    BarcodeView v = (BarcodeView)findViewById(R.id.barcode_view);
    v.resume();
    v.decodeSingle(new BarcodeCallback() {
        @Override
        public void barcodeResult(BarcodeResult result) {
            // Do whatever you want with the result
        }

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

希望这可以帮助!

需要在AndroidManifesl.xml中添加如下权限

从 Android 版本 6 开始,您需要在运行时请求权限。 按照文档在运行时请求权限。

我遇到了同样的问题。 但就我而言,我从日志中得到以下信息

W/TextureView:TextureView 或子类只能在启用硬件加速的情况下使用。

所以我只是为清单文件中的活动设置了硬件加速。 它奏效了。!

android:hardwareAccelerated="true"
  相关解决方案