我在真实设备上处理相机应用程序时遇到问题。当我在genymotion模拟器版本4.4.2(API 19)上运行我的应用程序时它工作正常...但是当我在我的真实设备上运行华为P9 Lite版本6(API 23)时它崩溃了。以下是Logcat上显示的错误:
05-01 23:31:10.963 9308-9308/edu.sfsu.cs.orange.ocr W/CameraBase: An error
occurred while connecting to camera: 0
05-01 23:31:10.963 9308-9308/edu.sfsu.cs.orange.ocr E/Camera-JNI:
android_hardware_Camera_native_setup Error: -1
05-01 23:31:10.963 9308-9308/edu.sfsu.cs.orange.ocr E/Camera: Camera new
cameraInitNormal:-1
在我的类中,我处理相机对象,我看到“android.hardware.camera已被弃用” 我认为它可能来自这个,我应该使用“android.hardware.camera2”代替。但是当我使用很多相机对象和它的方法时,我的代码会有很多变化。这可以避免吗?还是这个问题来自另一件事?
相机的权限已包含在我的清单中,我在构建文件中使用此配置:
compileSdkVersion 24
buildToolsVersion '25.0.0'
minSdkVersion 9
targetSdkVersion 24
更新
这就是我认为问题来自弃用方法的原因:
try {
// Open and initialize the camera
cameraManager.openDriver(surfaceHolder);
// Creating the handler starts the preview, which can also throw a RuntimeException.
handler = new CaptureActivityHandler(this, cameraManager, isContinuousModeActive);
} catch (IOException ioe) {
showErrorMessage("Error", "Couldn't initialize camera. Please try restarting device.");
} catch (RuntimeException e) {
// Barcode Scanner has seen crashes in the wild of this variety:
// java.?lang.?RuntimeException: Fail to connect to camera service
showErrorMessage("Error", "Could not initialize camera. Please try restarting device.");
}
当应用程序在我的手机上启动时,它会抛出此RuntimeException。
openDrive 方法:
public synchronized void openDriver(SurfaceHolder holder) throws IOException {
Camera theCamera = camera;
if (theCamera == null) {
theCamera = Camera.open();
if (theCamera == null) {
throw new IOException();
}
camera = theCamera;
}
camera.setPreviewDisplay(holder);
if (!initialized) {
initialized = true;
configManager.initFromCameraParameters(theCamera);
if (requestedFramingRectWidth > 0 && requestedFramingRectHeight > 0) {
adjustFramingRect(requestedFramingRectWidth, requestedFramingRectHeight);
requestedFramingRectWidth = 0;
requestedFramingRectHeight = 0;
}
}
configManager.setDesiredCameraParameters(theCamera);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
reverseImage = prefs.getBoolean(PreferencesActivity.KEY_REVERSE_IMAGE, false);
}
我有什么想法可以解决这个问题?提前谢谢
答案 0 :(得分:0)
快速调试提示是为targetSdkVersion 19编译应用程序。看起来在API版本之间Camera接口必须已经解密了一些东西。此外,如果您有能力通过串行终端或adb查看内核日志,那么查看"错误:-1"对硬件意味着它是否真的到达内核中的摄像头驱动程序?
adb root
adb shell
#cat /proc/kmsg