我使用JourneyApp's BarCode Scanner Library
设置了条形码扫描仪我添加了依赖项,就像在库的自述文件中一样。
然后,我在我的主要活动中设置了一个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仅显示黑屏。我在这里做错了吗?
答案 0 :(得分:4)
我知道自问题被问到已经过去了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) {
}
});
}
希望这有帮助!
答案 1 :(得分:4)
查看journeyapp的存储库here中的示例代码。
您必须分别在resume
和pause
内{1}}和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()
答案 2 :(得分:2)
您需要在AndroidManifesl.xml中添加以下权限
从Android的第6版开始,您需要在运行时请求权限。遵循文档以在运行时请求权限。 https://tools.ietf.org/html/rfc6638#section-5
答案 3 :(得分:0)
我遇到了同样的问题。但就我而言,我从日志中得到了以下信息
W / TextureView:TextureView或子类只能与 启用了硬件加速。
因此,我只在清单文件中设置活动的硬件加速。而且有效。!
android:hardwareAccelerated="true"