虽然声明了权限,但无法连接到相机服务

时间:2016-05-15 11:07:19

标签: android google-play-services android-vision

由于某些原因我得到了这个错误evan虽然我在清单中添加了正确的权限,我正在尝试使用相机并使用谷歌Android移动视觉api扫描条形码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.barcodescanner">
<uses-permission android:name="android.permission.INTERNET">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE/> <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
</uses-permission>
<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>

java代码:

package com.app.barcodescanner;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    SurfaceView cameraView;
    TextView barCodeInfo;
    CameraSource cameraSource;
    ArrayList<String> results = new ArrayList<String>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cameraView = (SurfaceView) findViewById(R.id.camera_view);
        barCodeInfo = (TextView) findViewById(R.id.code_info);

        final BarcodeDetector barcodeDetector = new BarcodeDetector.Builder(this).setBarcodeFormats(Barcode.QR_CODE)
                .build();

        cameraSource = new CameraSource.Builder(this, barcodeDetector).
                setRequestedPreviewSize(640,480).build();

        cameraView.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                try {
                    cameraSource.start(cameraView.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) {
                    barCodeInfo.post(new Runnable() {    // Use the post method of the TextView
                        public void run() {
                            barCodeInfo.setText(    // Update the TextView
                                    barcodes.valueAt(0).displayValue
                            );
                        }
                    });
                }
            }
        });
    }
}

这是错误日志:

com.app.barcodescanner D/libEGL: eglInitialize EGLDisplay = 0x96f457c4
com.app.barcodescanner I/OpenGLRenderer: Initialized EGL, version 1.4
com.app.barcodescanner W/CameraBase: An error occurred while connecting to camera: 0 /com.app.barcodescanner D/AndroidRuntime: Shutting down VM
   /com.app.barcodescanner E/AndroidRuntime: FATAL EXCEPTION: main                                                                         Process: com.app.barcodescanner, PID: 19767                                                                           java.lang.RuntimeException: Fail to connect to camera service
                                                                                at android.hardware.Camera.<init>(Camera.java:568)
                                                                                at android.hardware.Camera.open(Camera.java:405)
                                                                                at com.google.android.gms.vision.CameraSource.zzBZ(Unknown Source)
                                                                                at com.google.android.gms.vision.CameraSource.start(Unknown Source)
                                                                                at com.app.barcodescanner.MainActivity$1.surfaceCreated(MainActivity.java:41)
                                                                                at android.view.SurfaceView.updateWindow(SurfaceView.java:712)
                                                                                at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:209)
                                                                                at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:1014)
                                                                                at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2510)
                                                                                at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
                                                                                at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397)
                                                                                at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
                                                                                at android.view.Choreographer.doCallbacks(Choreographer.java:695)
                                                                                at android.view.Choreographer.doFrame(Choreographer.java:631)
                                                                                at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
                                                                                at android.os.Handler.handleCallback(Handler.java:739)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:158)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
    05-15 13:48:49.549 19767-21028/com.app.barcodescanner I/Vision: Connection error: null

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题并通过重新启动我的nexus-6几次来修复它。还要尝试查看您的相机原生应用是否可以打开相机。在我的情况下,甚至相机原生应用程序都无法打开相机硬件。我重新启动了,它又开始工作了。