zxing android 自定义,android – ZXing条码扫描器在片段中的自定义布局

作者在Android Studio上开发了一个使用ZXing库的条码扫描仪,但在构建应用时遇到了问题:CompoundBarcodeView只显示黑色视图并提示用户将条码放入视场中进行扫描。在尝试了ZXing-minimal库后,发现该库不支持在portrait模式下工作。通过在Fragment的onResume和onPause方法中添加对barcodeView的暂停和恢复操作,解决了问题。现在,当应用进入和退出前台时,扫描功能可以正常工作了。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我在Android Studio上开发了this page的ZXing条码连续扫描仪.

我的应用build.gradle包括:

repositories {

mavenCentral()

maven {

url "https://raw.github.com/embarkmobile/zxing-android-minimal/mvn-repo/maven-repository/"

}

}

dependencies {

compile fileTree(include: ['*.jar'], dir: 'libs')

compile 'com.android.support:appcompat-v7:23.0.1'

compile files('src/main/jniLibs/scanditsdk-android-4.7.5.jar')

compile files('src/main/jniLibs/httpclient-4.0.jar')

compile 'com.journeyapps:zxing-android-embedded:3.0.3@aar'

compile 'com.google.zxing:core:3.2.0'

}

我的Fragment.xml的布局:

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#00CC00"

android:orientation="vertical"

android:weightSum="100">

android:id="@+id/barcode_scanner"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="40"

>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_marginTop="5dp"

android:gravity="center"

android:orientation="horizontal"

android:weightSum="100"

style="?android:attr/buttonBarStyle"

>

android:id="@+id/btnStartScan"

android:layout_width="0dp"

android:layout_height="50dp"

android:layout_weight="25"

android:text="Start"

android:background="@drawable/buttonstyle"

style="@style/button_style"/>

android:id="@+id/btnStopScan"

android:layout_width="0dp"

android:layout_height="50dp"

android:layout_weight="25"

android:text="Stop"

android:background="@drawable/buttonstyle"

style="@style/button_style"/>

android:id="@+id/btnPauseScan"

android:layout_width="0dp"

android:layout_height="50dp"

android:layout_weight="25"

android:text="Pause"

android:background="@drawable/buttonstyle"

style="@style/button_style"/>

android:id="@+id/btnResumeScan"

android:layout_width="0dp"

android:layout_height="50dp"

android:layout_weight="25"

android:text="Resume"

android:background="@drawable/buttonstyle"

style="@style/button_style"/>

然后,我的Fragment代码如下所示:

public class CMCSMOFragment extends Fragment implements View.OnClickListener {

private CompoundBarcodeView barcodeView;

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

if (container == null) {

return null;

}

View v;

v = inflater.inflate(R.layout.cmcsmo_layout, container, false);

barcodeView = (CompoundBarcodeView) v.findViewById(R.id.barcode_scanner);

barcodeView.decodeContinuous(callback);

return v;

}

private BarcodeCallback callback = new BarcodeCallback() {

@Override

public void barcodeResult(BarcodeResult result) {

if (result.getText() != null) {

barcodeView.setStatusText(result.getText());

}

//Do something with code result

}

@Override

public void possibleResultPoints(List resultPoints) {

}

};

}

当我构建我的应用程序时,CompoundBarcodeView只显示带有ZXing文本的黑色视图:

Place a barcode inside the viewfinder rectangle to scan it.

编辑:

遵循列侬的建议,我曾使用zxing-minimum,但它不允许在肖像模式下工作:(.

我该怎么做才能解决这个问题?谢谢大家的帮助!

解决方法:

这很简单,ZXing的老板说只在onResume和onPause覆盖方法中添加以下代码:

@Override

public void onResume() {

barcodeView.resume();

super.onResume();

}

@Override

public void onPause() {

barcodeView.pause();

super.onPause();

}

标签:barcode-scanner,android,android-fragments,zxing

来源: https://codeday.me/bug/20190930/1835636.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值