嗨,我正在尝试使用Camera v2 API通过Raspberry Pi 3设备录制视频,该设备使用了与其相连的Raspberry Pi摄像机模块。

我正在使用带有Kotlin的Android Things开发此功能。

这是我的视频录制代码的样子。

  override fun startRecording(videoCameraCallback: VideoCameraCallback) {
    val cameraIdList = cameraManager.cameraIdList
    cameraManager.openCamera(cameraIdList[0], cameraStateCalback, null)

    Log.d("JJJ", "start recording called")
}

    var cameraStateCalback = object : CameraDevice.StateCallback() {
    override fun onOpened(camera: CameraDevice?) {
        if (camera != null) {
            Log.d("JJJ", "onOpened and will now create handler and capture session")
            //create handler thread
            val thread = HandlerThread("MyHandlerThread")
            thread.start()
            val handler = Handler(thread.looper)

            //create capture session
            val mediaRecorderSurface = setMediaOutputSurface()
            val surfaces = ArrayList<Surface>()
            surfaces.add(mediaRecorderSurface)
            captureRequest =  camera.createCaptureRequest(TEMPLATE_RECORD).build()
            camera.createCaptureSession(surfaces, cameraCaptureSessionCallBack,handler)
            Log.d("JJJ", "Created thread handler and capture session")
        }
    }

    override fun onDisconnected(camera: CameraDevice?) {
        Log.d("JJJ", "on disconnected")
    }

    override fun onError(camera: CameraDevice?, error: Int) {
        Log.d("JJJ", "on error")
    }
}



 var  cameraCaptureSessionCallBack = object :  CameraCaptureSession.StateCallback(){

    override fun onConfigureFailed(session: CameraCaptureSession?) {
        Log.d("JJJ", "on configured failed")
    }

    override fun onConfigured(session: CameraCaptureSession?) {
        Log.d("JJJ", "start recording")
        mediaRecorder.start()

    }
}


    private fun setMediaOutputSurface(): Surface {
          var file = File(context.filesDir.absolutePath)
        if(file.canWrite() && file.canRead()){
            Log.d("JJJ", "File location is fine")
        }

        mediaRecorder = MediaRecorder()
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE) //changed this to surface from camera and it fixed the cant get surface error

        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264)
        mediaRecorder.setVideoSize(600,600)


//        mediaRecorder.setVideoEncodingBitRate(10000000)
//        mediaRecorder.setVideoEncodingBitRate(512 * 1000)
        mediaRecorder.setOutputFile(context.filesDir.absolutePath + "/"+System.currentTimeMillis() + ".mp4")
        mediaRecorder.setVideoFrameRate(30)

        mediaRecorder.prepare()
//        mediaRecorder.start()
        return mediaRecorder.surface

}

这是我的策略
  • 使用摄像机管理器获取摄像机ID的列表。
  • 打开列表中的第一个摄像机(仅连接一个摄像机
  • 成功打开后,创建处理程序线程
  • 使用所有必要的参数(视频源,帧速率等)创建mediaRecorder
  • mediaRecorder(失败的地方)获取表面对象
  • 使用媒体播放器和先前创建的hanlder的表面创建捕获请求
  • 配置成功后,调用mediaRecorder.start开始录制视频。

  • 我得到的完整stacktrace错误如下:
      12-01 09:58:23.981 8776-8776/com.jr.survailancedropboxcam W/CameraDevice-JV-0: Stream configuration failed due to: endConfigure:372: Camera 0: Unsupported set of inputs/outputs provided
    12-01 09:58:23.985 8776-8958/com.jr.survailancedropboxcam D/JJJ: on configured failed
    12-01 09:58:23.985 8776-8776/com.jr.survailancedropboxcam E/CameraCaptureSession: Session 0: Failed to create capture session; configuration failed
    

    提前致谢

    最佳答案

    在最新版本(AndroidThings DP 6)中,您可以使用Camera v2 API

    https://android-developers.googleblog.com/2017/11/android-things-developer-preview-6.html

    这应该工作:https://github.com/googlesamples/android-Camera2Basic

    您需要确保 list 中的标志设置为true

    <activity
    
        ...
        android:hardwareAccelerated="true">
    

    关于Android录像机: failed to get surface,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47582970/

    10-10 01:45