扫描QR code。使用Android的手机愿景API [英] Scanning QR code using Android's Mobile Vision API

查看:892
本文介绍了扫描QR code。使用Android的手机愿景API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跟着这个的关于如何建立一个Android应用程序,它可以扫描QR codeS教程

下面是完整的code。我加入了谷歌播放使用等级像这样编译服务'com.google.android.gms:发挥服务:7.8.0。

Here's the full code. I added the Google Play services using grade like so compile 'com.google.android.gms:play-services:7.8.0'.

的Andr​​oidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="bitinvent.io.qrscanner" >

    <meta-data android:name="com.google.android.gms.vision.DEPENDENCIES" android:value="barcode"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.CAMERA"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme" >
        <activity android:name=".MainActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<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">

    <SurfaceView
        android:id="@+id/cameraView"
        android:layout_width="640px"
        android:layout_height="480px"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"/>

    <TextView
        android:id="@+id/infoTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/cameraView"
        android:layout_marginLeft="16dp"
        android:text="Nothing to read"
        android:textSize="20sp"/>

</RelativeLayout>

MainActivity.java

package bitinvent.io.qrscanner;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseArray;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;

import com.google.android.gms.vision.CameraSource;
import com.google.android.gms.vision.Detector;
import com.google.android.gms.vision.barcode.Barcode;
import com.google.android.gms.vision.barcode.BarcodeDetector;

import java.io.IOException;

public class MainActivity extends Activity {

    private SurfaceView cameraView;
    private TextView barcodeInfo;
    private CameraSource cameraSource;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cameraView = (SurfaceView) findViewById(R.id.cameraView);
        barcodeInfo = (TextView) findViewById(R.id.infoTextView);

        BarcodeDetector detector = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE).build();
        cameraSource = new CameraSource.Builder(this, detector).setRequestedPreviewSize(640, 480).build();

        cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                try {
                    cameraSource.start(cameraView.getHolder());
                } catch (IOException e) {
                    Log.e("CAMERA SOURCE", e.getMessage());
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                cameraSource.stop();
            }
        });

        detector.setProcessor(new Detector.Processor<Barcode>() {
            @Override
            public void release() {

            }

            @Override
            public void receiveDetections(Detector.Detections<Barcode> detections) {
                final SparseArray<Barcode> barcodes = detections.getDetectedItems();
                if (barcodes.size() != 0) {
                    barcodeInfo.post(new Runnable() {
                        @Override
                        public void run() {
                            barcodeInfo.setText(barcodes.valueAt(0).displayValue);
                        }
                    });
                }
            }
        });
    }
}

我测试在运行Android 4.4.2一个HTC Desire的816。但它似乎并没有工作。该相机的看法是积极的,但是当在QR code指出,它不检测什么。但是,没有任何错误或崩溃或者发生。

I tested this on a HTC Desire 816 running Android 4.4.2. But it doesn't seem to work. The camera view is active but when pointed at a QR code, it doesn't detect anything. But no errors or crashes occur either.

我缺少的东西吗?

推荐答案

我是新来的Andr​​oid的发展,但我也跟着用播放服务8.1教程。工作。

I'm new to Android development but I followed the tutorial using play services 8.1. Worked.

code是与你的差不多。唯一的区别是,我有应用程序级别下的元标记,我删除.setBar codeFormats(酒吧code.QR_ code),因为它限制了专门QR型codeS。

Code is very similar to yours. Only differences are that I have the meta tag under the application level and I removed .setBarcodeFormats(Barcode.QR_CODE) since it limits it specifically to QR type codes.

此外使用的应用程序在横向纵向以来并没有为我工作。即使在景观的QR codeS我有时不得不慢慢地从相机移动QR code距离,直到它能够识别它。

Also used the app in landscape since portrait wasn't working for me. Even in landscape for QR codes I sometimes had to slowly move the QR code away from the camera until it was able to recognize it.

这篇关于扫描QR code。使用Android的手机愿景API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆