【问题标题】:Camera doesn't open with Mobile Vision API相机无法使用 Mobile Vision API 打开
【发布时间】:2016-10-21 07:48:19
【问题描述】:

我正在尝试开发一个简单的 QR 扫描仪应用程序。我已经按照教程here 进行了操作,但它似乎不起作用。

以下是我的build.gradle(应用级别)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.3"
    defaultConfig {
        applicationId "qrcode.auro.com.qrreaderexample"
        minSdkVersion 16
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.google.android.gms:play-services-vision:9.6.1'
    compile fileTree(dir: 'libs', include: ['*.jar'])

    testCompile 'junit:junit:4.12'
}

AndroidManifest.xml

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

    <uses-permission android:name="android.permission.CAMERA"/>
    <uses-feature
        android:name="android.hardware.camera2"
        android:required="true"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

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

        <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:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="qrcode.auro.com.qrreaderexample.MainActivity">

    <TextView
        android:id="@+id/barcode_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:text="READING BARCODE...."/>


    <SurfaceView
        android:id="@+id/surface_view"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerInParent="true"/>


    <Button
        android:id="@+id/scanner_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SCAN"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

MainActivity.java

package qrcode.auro.com.qrreaderexample;

import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Camera;
import android.support.v4.app.ActivityCompat;
import android.support.v4.util.SparseArrayCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.SparseArray;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
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 AppCompatActivity {

    private SurfaceView surfaceView;
    private Button button;
    private TextView barcodeText;

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

        surfaceView = (SurfaceView) findViewById(R.id.surface_view);
        button = (Button) findViewById(R.id.scanner_button);
        barcodeText = (TextView) findViewById(R.id.barcode_text);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createCameraSource();
            }
        });


    }

    private void createCameraSource() {

        BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this).build();
        final CameraSource cameraSource = new CameraSource.Builder(this, barcodeDetector)
                .setAutoFocusEnabled(true)
                .setRequestedPreviewSize(500, 500)
                .build();

        surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                try {
                    cameraSource.start(surfaceView.getHolder());

                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

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

            }

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

        barcodeDetector.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) {

                    barcodeText.setText(barcodes.valueAt(0).displayValue);

                } else
                    barcodeText.setText("NO BARCODES FOUND!!!!");

            }
        });

    }
}

我这里有一个按钮 (R.id.scanner_button),它应该打开相机并开始读取代码,但它没有。

每次单击按钮时,我的 logcat 中都会显示以下内容:

10-21 13:16:55.370 25722-25722/qrcode.auro.com.qrreaderexample W/DynamiteModule: Local module descriptor class for com.google.android.gms.vision.dynamite not found.
10-21 13:16:55.373 25722-25722/qrcode.auro.com.qrreaderexample I/DynamiteModule: Considering local module com.google.android.gms.vision.dynamite:0 and remote module com.google.android.gms.vision.dynamite:8
10-21 13:16:55.374 25722-25722/qrcode.auro.com.qrreaderexample I/DynamiteModule: Selected remote version of com.google.android.gms.vision.dynamite, version >= 8

我在这里错过了什么?

【问题讨论】:

    标签: java android android-camera


    【解决方案1】:

    这似乎是 Google Play 服务中的相同错误。例如,Firebase 的类似错误 - link。您在 logcat 输出中看到的日志具有警告级别,而不是错误。

    关于您的代码 - 未调用 surfaceCreated(),您应该正确初始化 SurfaceViewHolder,例如创建 MySurfaceViewextends SurfaceView,类似 this

    此外,您还可以看到官方谷歌example 用于条形码阅读器,其中包含使用BarcodeDetector 类。在该示例中,LogCat 也有警告消息,如您的示例所示,但条形码阅读器现在可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-30
      • 1970-01-01
      • 2016-07-12
      • 1970-01-01
      • 1970-01-01
      • 2017-12-24
      • 1970-01-01
      • 2019-01-07
      相关资源
      最近更新 更多