如何使用扫描二维码与布局内的相机(如Whatsapp网络扫描仪)
问题描述:
我是开发人员,但在Android中是新的,我需要知道如何在活动布局中使用相机。如何使用扫描二维码与布局内的相机(如Whatsapp网络扫描仪)
我知道我需要使用Surface View在活动中插入相机,目前我的应用程序正在使用默认相机(使用默认相机阅读QR码)(一个按钮打开相机,用户拍照并且我的应用程序感知活动结果)。
但我真的需要在应用程序中实现与实时扫描仪功能。
有人可以指导我吗?
答
这里是我是如何实现类似于你所需要的东西。
- 首先,我使用Zxing库来实现这一点。所以,你需要下面的依赖添加到您的gradle产出:
compile 'com.journeyapps:zxing-android-embedded:3.5.0'
- 下面是一个直接链接到journeyapps扫描仪项目的Github上的链接:
https://github.com/journeyapps/zxing-android-embedded
- 以下是我如何制作我的布局文件:
<!-- language: lang-xml -->
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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/view_scanner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/view_footer"
android:soundEffectsEnabled="true" />
<LinearLayout
android:id="@+id/view_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/view_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|top"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/_10sdp"
android:layout_marginTop="@dimen/_10sdp"
android:src="@drawable/ic_back_light" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/_50sdp"
android:gravity="right|top">
<ImageView
android:id="@+id/iv_flash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="@dimen/_10sdp"
android:layout_marginTop="@dimen/_10sdp"
android:src="@drawable/ic_flash_inactive" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/view_footer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:gravity="center"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="@dimen/_10sdp"
android:layout_marginRight="@dimen/_10sdp"
android:background="@android:color/darker_gray" />
<TextView
android:id="@+id/code_info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@string/font_sans_serif_light"
android:padding="@dimen/_15sdp"
android:text="@string/app_name"
android:textColor="@android:color/white"
android:textSize="@dimen/_15sdp" />
</LinearLayout>
</FrameLayout>
所以现在DecoratedBarcodeView作为您的布局中的主扫描区域。
- 如下初始化条形码的看法: - :
private BarcodeCallback callback = new BarcodeCallback() {
@Override
public void barcodeResult(BarcodeResult result) {
//Process your scan result here
String resultString = result.getText();
}
@Override
public void possibleResultPoints(List<ResultPoint> resultPoints) {
}
};
希望这
private DecoratedBarcodeView barcodeView;
barcodeView = (DecoratedBarcodeView) findViewById(R.id.view_scanner);
barcodeView.setStatusText("");
barcodeView.decodeContinuous(callback);
在您的活动,您可以通过这种方式在你的BarcodeCallback获取扫描结果帮助。