无法连接到摄像头服务

无法连接到摄像头服务

我在stackoverflow和其他网站上查看了许多与我类似的问题,但未能找到解决我问题的方法。我已请求正确的权限,但仍然出现此错误。起初我以为是因为我在使用AVD,但是即使在使用电话时,我仍然会收到此错误。

主要活动:

  package com.example.vishal.textfacialprocessing;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.qualcomm.snapdragon.sdk.face.FaceData;
import com.qualcomm.snapdragon.sdk.face.FacialProcessing;

public class MainActivity extends Activity implements Camera.PreviewCallback                        {
Camera mCamera = null;
CameraPreview preview;
private CameraPreview mPreview = null;
private int cameraside = 1;
private int Front_Camera_Index = 1;
private int Back_Camera_Index = 0;
private static boolean switchCamera;
private boolean _qcSDKEnabled;
FacialProcessing faceProc;
Display display;
private int displayAngle;
private int numFaces;
FaceData[] faceArray = null;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Button switchCameraButton = (Button)
      findViewById(R.id.switchCamera);
    switchCameraButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (cameraside == 1) {
                cameraside = 0;
            } else {
                cameraside = 1;
            }
        }
    });

    setCamera();
}

public void setCamera ()
{
    mCamera = Camera.open(cameraside);
    mPreview = new CameraPreview(MainActivity.this,mCamera);
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);
    mCamera.setPreviewCallback(MainActivity.this);
    _qcSDKEnabled = FacialProcessing.isFeatureSupported(FacialProcessing.FEATURE_LIST.FEATURE_FACIAL_PROCESSING);

}
 }


CameraPreview:

 package com.example.vishal.textfacialprocessing;

import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.ViewGroup;

import com.qualcomm.snapdragon.sdk.face.FacialProcessing;

import java.io.IOException;

public class CameraPreview extends SurfaceView implements          SurfaceHolder.Callback {
private SurfaceHolder mHolder;
private Camera cameraObj;

public CameraPreview(Context context, Camera camera) {
    super(context);
    cameraObj = camera;

    // Install a urfaceHolder.Callback so we get notified when the
    // underlying surface is created and destroyed.
    mHolder = getHolder();
    mHolder.addCallback(this);
    // deprecated setting, but required on Android versions prior to 3.0
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void surfaceCreated(SurfaceHolder holder) {
    // SurfaceHolder jholder = holder;
    // The Surface has been created, now tell the camera where to draw the      preview.
    try {
        cameraObj.setPreviewDisplay(holder);
        cameraObj.startPreview();
    } catch (IOException e) {


    }
}

public void surfaceDestroyed(SurfaceHolder holder) {
    // empty. Take care of releasing the Camera preview in your activity.
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    // If your preview can change or rotate, take care of those events here.
    // Make sure to stop the preview before resizing or reformatting it.

    if (mHolder.getSurface() == null){
        // preview surface does not exist
        return;
    }

    // stop preview before making changes
    try {
        cameraObj.stopPreview();
    } catch (Exception e){
        // ignore: tried to stop a non-existent preview
    }

    // set preview size and make any resize, rotate or
    // reformatting changes here

    // start preview with new settings
    try {
        cameraObj.setPreviewDisplay(mHolder);
        cameraObj.startPreview();

    } catch (Exception e){

    }
    }
    }


表现:
    
    
    
    
    

<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>




逻辑:

01-02 14:27:35.261 10635-10635/com.example.vishal.textfacialprocessing             E/Typeface: SANS_LOC file not found.
01-02 14:27:35.321 10635-10635/com.example.vishal.textfacialprocessing     E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                                 Process: com.example.vishal.textfacialprocessing, PID: 10635
                                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vishal.textfacialprocessing/com.example.vishal.textfacialprocessing.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.release()' on a null object reference
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
                                                                                         at android.app.ActivityThread.access$800(ActivityThread.java:144)
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                         at android.os.Looper.loop(Looper.java:155)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5696)
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824)
                                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.hardware.Camera.release()' on a null object reference
                                                                                         at com.example.vishal.textfacialprocessing.MainActivity.getCameraInstance(MainActivity.java:156)
                                                                                         at com.example.vishal.textfacialprocessing.MainActivity.onResume(MainActivity.java:58)
                                                                                         at com.example.vishal.textfacialprocessing.MainActivity.onCreate(MainActivity.java:50)
                                                                                         at android.app.Activity.performCreate(Activity.java:5958)
                                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
                                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
                                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) 
                                                                                         at android.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) 
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                         at android.os.Looper.loop(Looper.java:155) 
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5696) 
                                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                                         at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) 
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:824) 

最佳答案

您好Vishal您需要在清单文件中添加以下权限

<uses-permission android:name="android.permission.CAMERA" />


如果您使用的是Android M权限模型,则首先需要检查应用程序在运行时是否具有此权限,并且必须在运行时提示用户输入此权限。您在清单上定义的权限不会在安装时自动授予。

 if (checkSelfPermission(Manifest.permission.CAMERA)
    != PackageManager.PERMISSION_GRANTED) {

requestPermissions(new String[]{Manifest.permission.CAMERA},
        MY_REQUEST_CODE);
}


您需要为对话框结果提供一个回调:

@Override
public void onRequestPermissionResult(int requestCode, String[] permissions,      int[] grantResults) {
if (requestCode == MY_REQUEST__CODE) {
    if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        // Now user should be able to use camera
    }
    else {
        // Your app will not have this permission. Turn off all functions
        // that require this permission or it will force close like your
        // original question
    }
}
}

关于java - Java RuntimeException“无法连接到摄像头服务”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34569733/

10-08 23:52