E / AndroidRuntime:致命异常:主要 java.lang.RuntimeException:无法连接到摄像头服务 在android.hardware.Camera.native_setup(本机方法) 在android.hardware.Camera。(Camera.java:423) 在android.hardware.Camera.open(Camera.java:384) 在ge.baqari.dev.capture.ui.view.CameraView.surfaceChanged(CameraView.kt:39) 在android.view.SurfaceView.updateWindow(SurfaceView.java:617) 在android.view.SurfaceView.access $ 000(SurfaceView.java:88) 在android.view.SurfaceView $ 3.onPreDraw(SurfaceView.java:183) 在android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:692) 在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2123) 在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139) 在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:4879) 在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:776) 在android.view.Choreographer.doCallbacks(Choreographer.java:579) 在android.view.Choreographer.doFrame(Choreographer.java:548) 在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:762) 在android.os.Handler.handleCallback(Handler.java:725) 在android.os.Handler.dispatchMessage(Handler.java:92) 在android.os.Looper.loop(Looper.java:153) 在android.app.ActivityThread.main(ActivityThread.java:5341) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:511) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:929) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 在dalvik.system.NativeStart.main(本地方法)
我找到了很多答案,但是他们没有帮助我,我正在尝试创建自定义相机视图,实现表面支架,但它总是使用Camera.open()方法崩溃,带参数且没有参数相同,崩溃
class CameraView : android.view.SurfaceView, android.view.SurfaceHolder.Callback {
var camera: android.hardware.Camera? = null
var previewing = false
var cameraCallback: CameraCallBack? = null
var pictureCallback: PictureCallback? = null
var cameraParameters: android.hardware.Camera.Parameters? = null
private fun init() {
holder.addCallback(this)
holder.setType(SURFACE_TYPE_NORMAL)
}
constructor(context: android.content.Context, attrs: android.util.AttributeSet) : super(context, attrs) {
init()
}
override fun surfaceCreated(surfaceHolder: android.view.SurfaceHolder) {
}
override fun surfaceChanged(surfaceHolder: android.view.SurfaceHolder, i: Int, i1: Int, i2: Int) {
if (context.packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
if (previewing) {
camera?.stopPreview()
previewing = false
}
if (camera == null) {
try {
camera = Camera.open(android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK)
cameraParameters!!.setPreviewSize(i1, i2)
camera!!.parameters = cameraParameters!!
camera!!.startPreview()
camera!!.setPreviewDisplay(holder)
previewing = true
} catch (e: java.io.IOException) {
e.printStackTrace()
}
}
}
}
override fun surfaceDestroyed(surfaceHolder: android.view.SurfaceHolder) {
camera!!.stopPreview()
camera!!.release()
previewing = false;
}
}
答案 0 :(得分:0)
问题在于CameraParameters,参数为null,您尝试设置空参数
修复:
camera = Camera.open(android.hardware.Camera.CameraInfo.CAMERA_FACING_BACK)
cameraParameters = camera!!.parameters
cameraParameters!!.setPreviewSize(i1, i2)
camera!!.parameters = cameraParameters!!
camera!!.startPreview()
camera!!.setPreviewDisplay(holder)
此外,您可以将此库用于camera preview