有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

安卓 Java RuntimeException“无法连接到摄像头服务”

我在stackoverflow和其他网站上看到了许多类似的问题,但没有找到解决问题的方法。我已请求了正确的权限,但仍收到此错误。起初我以为这是因为我使用的是AVD,但即使在使用手机时,我仍然会遇到这个错误

主要活动:

  package com.example.vishal.textfacialprocessing;

import 安卓.app.Activity;
import 安卓.content.Context;
import 安卓.content.res.Configuration;
import 安卓.hardware.Camera;
import 安卓.os.Bundle;
import 安卓.util.Log;
import 安卓.view.Display;
import 安卓.view.View;
import 安卓.view.WindowManager;
import 安卓.widget.Button;
import 安卓.widget.FrameLayout;
import 安卓.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);

}
 }

摄影评论:

 package com.example.vishal.textfacialprocessing;

import 安卓.content.Context;
import 安卓.hardware.Camera;
import 安卓.util.Log;
import 安卓.view.SurfaceHolder;
import 安卓.view.SurfaceView;
import 安卓.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
    安卓:allowBackup="true"
    安卓:icon="@mipmap/ic_launcher"
    安卓:label="@string/app_name"
    安卓:supportsRtl="true"
    安卓:theme="@style/AppTheme" >
    <activity
        安卓:name=".MainActivity" >
        <intent-filter>
            <action 安卓:name="安卓.intent.action.MAIN" />
            <category 安卓:name="安卓.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Logicat:

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 安卓.hardware.Camera.release()' on a null object reference
                                                                                         at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
                                                                                         at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
                                                                                         at 安卓.app.ActivityThread.access$800(ActivityThread.java:144)
                                                                                         at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
                                                                                         at 安卓.os.Handler.dispatchMessage(Handler.java:102)
                                                                                         at 安卓.os.Looper.loop(Looper.java:155)
                                                                                         at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
                                                                                         at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:824)
                                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void 安卓.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 安卓.app.Activity.performCreate(Activity.java:5958)
                                                                                         at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
                                                                                         at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
                                                                                         at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474) 
                                                                                         at 安卓.app.ActivityThread.access$800(ActivityThread.java:144) 
                                                                                         at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1359) 
                                                                                         at 安卓.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                         at 安卓.os.Looper.loop(Looper.java:155) 
                                                                                         at 安卓.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.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029) 
                                                                                         at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:824) 

共 (1) 个答案

  1. # 1 楼答案

    您好,Vishal您需要在menifest文件中添加以下权限

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

    如果你使用的是安卓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
        }
    }
    }