最后我让它在我身边工作。我想分享我现在在我的应用程序中实现二维码扫描的过程和代码。我实际上并不是在回答你的问题。但是,我没有从 StackOverflow 找到任何关于如何使用 Google Vision API 实现二维码扫描的良好帮助来源。我查看了您在问题中指出的教程。然而,该教程对我也没有多大帮助。因此,我在我的应用程序中记录了实现二维码扫描的类和步骤。
首先,您需要一些 gradle 依赖项。所以在你的build.gradle
文件中,添加以下依赖项。
dependencies {
compile 'com.android.support:design:25.3.1'
compile 'com.google.android.gms:play-services-vision:10.2.1'
}
然后,您的项目中需要有以下五个类。我在这里添加课程。如有必要,请导入缺少的类。
- 相机源.java https://gist.github.com/masudias/f8f37e8cf5e78c2c704cb4b6cbc5ddc8
- CameraSourcePreview.java https://gist.github.com/masudias/4810ccf3ae0c577e5da18d728afd1bb0
- BarcodeCaptureActivity.java https://gist.github.com/masudias/87cb9e525dc846d7b02fd8b5c5186ead
- BarcodeGraphicTracker.java https://gist.github.com/masudias/15063d72dc9dde69858055cff0527798
- BarcodeTrackerFactory.java https://gist.github.com/masudias/3f3b142b041b2dd2b33c6f114af57575
Now BarcodeCaptureActivity
有一个布局,您需要将其放入您的layout
文件夹也是如此。
这是barcode_capture.xml
您需要的布局。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/topLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:orientation="vertical">
<!-- Do not forget to replace with your package name where the class is located -->
<com.example.yourpackage.camera.CameraSourcePreview
android:id="@+id/preview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
您将需要一些字符串和清单中的活动条目BarcodeCaptureActivity
。这是您需要放入的字符串strings.xml
.
<!-- QR Code related strings -->
<string name="permission_camera_rationale">Access to the camera is needed for detection</string>
<string name="no_camera_permission">This application cannot run because it does not have the camera permission. The application will now exit.</string>
<string name="low_storage_error">Face detector dependencies cannot be downloaded due to low device storage</string>
<string name="ok">OK</string>
And the AndroidManifest.xml
应该有一个新条目BarcodeCaptureActivity
像下面这样。
<activity
android:name=".util.scanner.BarcodeCaptureActivity"
android:theme="@style/Theme.AppCompat" />
现在您的设置已完成,您可以打开相机来扫描条形码或二维码。只需拨打以下电话initiateScan
必要时发挥作用。
public static final int RC_BARCODE_CAPTURE = 9001;
public void initiateScan() {
Intent intent = new Intent(YourActivity.this, BarcodeCaptureActivity.class);
startActivityForResult(intent, RC_BARCODE_CAPTURE);
}
请注意,您在拨打电话之前需要向用户请求相机许可initiateScan
功能。授予相机许可后,您将调用initiateScan
功能。
The initiateScan
函数将打开扫描仪,扫描成功后,将返回调用Activity
or Fragment
。所以你需要有一个onActivityResult
调用中的函数Activity
or Fragment
.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == CommonStatusCodes.SUCCESS && requestCode == RC_BARCODE_CAPTURE) {
if (data == null) return;
Barcode barcode = data.getParcelableExtra(BarcodeCaptureActivity.BarcodeObject);
final String scanResult = barcode.displayValue;
if (scanResult == null) return;
doSomethingWithTheScanResult(scanResult);
}
}
不要忘记添加CAMERA
您的许可AndroidManifest.xml
文件也是如此。
<uses-permission android:name="android.permission.CAMERA" />
希望这有助于使用 Google Vision API 更轻松地集成二维码扫描仪。我添加了一个github项目 https://github.com/masudias/QRScanner获取示例 QR 码扫描仪应用程序。请看一看。