Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 运行时异常无法连接到某些设备中的摄像头服务_Java_Android_Android Camera_Barcode Scanner_Android Vision - Fatal编程技术网

Java 运行时异常无法连接到某些设备中的摄像头服务

Java 运行时异常无法连接到某些设备中的摄像头服务,java,android,android-camera,barcode-scanner,android-vision,Java,Android,Android Camera,Barcode Scanner,Android Vision,我正在我的应用程序中使用条形码扫描仪。在某些移动设备中,它给出了运行时异常“无法连接到摄像头” 这是我用来拍照的代码 /** * Opens the camera and applies the user settings. * * @throws RuntimeException if the method fails */ @SuppressLint("InlinedApi") private Camera createCamera() { int requestedCam

我正在我的应用程序中使用条形码扫描仪。在某些移动设备中,它给出了运行时异常“无法连接到摄像头”

这是我用来拍照的代码

 /**
 * Opens the camera and applies the user settings.
 *
 * @throws RuntimeException if the method fails
 */
@SuppressLint("InlinedApi")
private Camera createCamera() {
    int requestedCameraId = getIdForRequestedCamera(mFacing);
    if (requestedCameraId == -1) {
        throw new RuntimeException("Could not find requested camera.");
    }
    Camera camera = Camera.open(requestedCameraId);

    SizePair sizePair = selectSizePair(camera, mRequestedPreviewWidth, mRequestedPreviewHeight);
    if (sizePair == null) {
        throw new RuntimeException("Could not find suitable preview size.");
    }
    Size pictureSize = sizePair.pictureSize();
    mPreviewSize = sizePair.previewSize();

    int[] previewFpsRange = selectPreviewFpsRange(camera, mRequestedFps);
    if (previewFpsRange == null) {
        throw new RuntimeException("Could not find suitable preview frames per second range.");
    }

    Camera.Parameters parameters = camera.getParameters();

    if (pictureSize != null) {
        parameters.setPictureSize(pictureSize.getWidth(), pictureSize.getHeight());
    }

    parameters.setPreviewSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
    parameters.setPreviewFpsRange(
            previewFpsRange[Camera.Parameters.PREVIEW_FPS_MIN_INDEX],
            previewFpsRange[Camera.Parameters.PREVIEW_FPS_MAX_INDEX]);
    parameters.setPreviewFormat(ImageFormat.NV21);

    setRotation(camera, parameters, requestedCameraId);

    if (mFocusMode != null) {
        if (parameters.getSupportedFocusModes().contains(
                mFocusMode)) {
            parameters.setFocusMode(mFocusMode);
        } else {
            Log.i(TAG, "Camera focus mode: " + mFocusMode + " is not supported on this device.");
        }
    }

    // setting mFocusMode to the one set in the params
    mFocusMode = parameters.getFocusMode();

    if (mFlashMode != null) {
        if (parameters.getSupportedFlashModes() != null) {
            if (parameters.getSupportedFlashModes().contains(
                    mFlashMode)) {
                parameters.setFlashMode(mFlashMode);
            } else {
                Log.i(TAG, "Camera flash mode: " + mFlashMode + " is not supported on this device.");
            }
        }
    }

    // setting mFlashMode to the one set in the params
    mFlashMode = parameters.getFlashMode();

    camera.setParameters(parameters);

    // Four frame buffers are needed for working with the camera:
    //
    //   one for the frame that is currently being executed upon in doing detection
    //   one for the next pending frame to process immediately upon completing detection
    //   two for the frames that the camera uses to populate future preview images
    camera.setPreviewCallbackWithBuffer(new CameraPreviewCallback());
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));
    camera.addCallbackBuffer(createPreviewBuffer(mPreviewSize));

    return camera;
}

   * Stops the camera.
 */
@Override
protected void onPause() {
    super.onPause();
    if (mPreview != null) {
        mPreview.stop();
    }
}

/**
 * Releases the resources associated with the camera source, the associated detectors, and the
 * rest of the processing pipeline.
 */
@Override
protected void onDestroy() {
    super.onDestroy();
    if (mPreview != null) {
        mPreview.release();
    }

}

    /**
 * Restarts the camera.
 */
@Override
protected void onResume() {
    super.onResume();
        startCameraSource();
}
我得到的运行时异常是

    java.lang.RuntimeException: Fail to connect to camera service
at android.hardware.Camera.<init>(Camera.java:532)
at android.hardware.Camera.open(Camera.java:360)
java.lang.RuntimeException:无法连接到相机服务
在android.hardware.Camera.(Camera.java:532)
打开(Camera.java:360)

有关此问题的任何帮助,因为在某些设备中,它正在工作,但在某些设备中,它不工作,因此我无法获得此问题。

在使用之前,请始终检查相机是否可用

    private Camera mCamera;     
    /**
     * Opens the camera and applies the user settings.
     *
     * @throws RuntimeException if the method fails
     */
    @SuppressLint("InlinedApi")
    private Camera createCamera() {
       int requestedCameraId = getIdForRequestedCamera(mFacing);
       if (requestedCameraId == -1) {
           throw new RuntimeException("Could not find requested camera.");
       }

       if (mCamera != null){
          mCamera.release();
          mCamera = null;
       }

       mCamera= Camera.open(requestedCameraId);

       if(camera == null){
          Toast.makeText(mContext, "Camera service is not available currently.", Toast.LENGTH_LONG.show())    
            }
           //....
       }
     }
并在onDestroy()之前重新设置摄像头。


您在哪个操作系统版本中运行应用程序?我正在使用Android 6.0.1 Calling Camera。如果另一个应用程序已经在使用该摄像头,open()会引发异常。@修复您是否添加了access Camera的运行时权限?在menifest中使用此权限
/**
 * Releases the resources associated with the camera source, the associated detectors, and the
 * rest of the processing pipeline.
 */
@Override
protected void onDestroy() {
    super.onDestroy();
    if (mPreview != null) {
        mPreview.release();
        mCamera.release();
        mCamera = null;
}