【问题标题】:journeyapp's barcodeview showing black view onlyJourneyapp 的条码视图仅显示黑色视图
【发布时间】:2026-01-05 11:00:01
【问题描述】:

我已经使用JourneyApp's BarCode Scanner Library设置了条码扫描器

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

然后,我像这样在我的主要活动中设置了条形码视图

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 仅显示黑屏。我在这里做错了吗?

【问题讨论】:

    标签: java android zxing barcode-scanner


    【解决方案1】:

    查看 Journeyapp 存储库 here 中的示例代码。

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

    因此,在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();
    }
    

    【讨论】:

      【解决方案2】:

      我知道这个问题被问到已经 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) {
              }
          });
      }
      

      希望这能有所帮助!

      【讨论】:

        【解决方案3】:

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

        从 Android 版本 6 开始,您需要在运行时请求权限。按照文档在运行时请求权限。 https://developer.android.com/training/permissions/requesting.html?hl=en

        【讨论】:

        • 在 AndroidManifesl.xml 中添加&lt;uses-permission android:name="android.permission.CAMERA"/&gt; 权限后工作
        【解决方案4】:

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

        W/TextureView:TextureView 或子类只能与 启用硬件加速。

        所以我只是在清单文件中为活动设置了硬件加速。并且成功了。!

        android:hardwareAccelerated="true"
        

        【讨论】: