Я пытался подключить веб-камеру к приложению Android с помощью openCV.
Но openCv не может найти мою веб-камеру.
Мой пример приложения имеет разрешение USB-хоста и разрешение камеры. OpenCv инициализируется также успешно.
Но VideoCapture не может найти мою камеру.
Класс устройства моей веб-камеры — 239(0xEF), а не 0xF.
У вас есть мнение?
Вот мой полный код подключения.
class MainActivity : AppCompatActivity() {
Код: Выделить всё
private val TAG = "MainActivity"
private lateinit var usbManager: UsbManager
private lateinit var device: UsbDevice
private lateinit var videoCapture: VideoCapture
private lateinit var surfaceView: SurfaceView
private lateinit var surfaceHolder: SurfaceHolder
val ACTION_USB_PERMISSION = "ACTION_USB_PERMISSION"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// SurfaceView 초기화
surfaceView = findViewById(R.id.surfaceView)
surfaceHolder = surfaceView.holder
surfaceHolder.addCallback(object : SurfaceHolder.Callback {
override fun surfaceCreated(holder: SurfaceHolder) {
// 비디오 스트리밍 시작
startVideoStreaming()
}
override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {}
override fun surfaceDestroyed(holder: SurfaceHolder) {
stopVideoStreaming()
}
})
usbManager = getSystemService(Context.USB_SERVICE) as UsbManager
val filter = IntentFilter(UsbManager.ACTION_USB_DEVICE_ATTACHED)
registerReceiver(usbReceiver, filter)
// USB 장치 검색
discoverUsbDevices()
}
private fun discoverUsbDevices() {
val deviceList = usbManager.deviceList
for (usbDevice in deviceList.values) {
if (usbDevice.deviceClass == 239) {
device = usbDevice
requestPermission(device)
}
}
}
private fun requestPermission(device: UsbDevice) {
val permissionIntent = PendingIntent.getBroadcast(this, 0, Intent(ACTION_USB_PERMISSION),
PendingIntent.FLAG_IMMUTABLE)
usbManager.requestPermission(device, permissionIntent)
}
private val usbReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (ACTION_USB_PERMISSION == intent?.action) {
val usbDevice: UsbDevice? = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE)
if (usbDevice != null && intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
// 웹캠 초기화 및 스트리밍 시작
startVideoStreaming()
}
}
}
}
private fun startVideoStreaming() {
// OpenCV 초기화
if (!OpenCVLoader.initDebug()) {
Log.e("OpenCV", "Initialization Failed")
return
}
// VideoCapture 객체 생성, USB 카메라의 인덱스는 0으로 설정 (첫 번째 카메라)
videoCapture = VideoCapture(0)
Log.d(TAG, "startVideoStreaming: ${videoCapture.isOpened}")
// 비디오 스트리밍을 위한 쓰레드 생성
Thread {
val frame = Mat()
while (videoCapture.isOpened) {
if (videoCapture.read(frame)) {
// 프레임을 SurfaceView에 렌더링
Log.d(TAG, "startVideoStreaming: ")
renderFrame(frame)
}
}
}.start()
}
private fun renderFrame(frame: Mat) {
// 프레임을 Bitmap으로 변환
val bitmap = Bitmap.createBitmap(frame.cols(), frame.rows(), Bitmap.Config.ARGB_8888)
Utils.matToBitmap(frame, bitmap)
// UI 스레드에서 SurfaceView에 비트맵 렌더링
runOnUiThread {
val canvas = surfaceHolder.lockCanvas()
canvas.drawBitmap(bitmap, 0f, 0f, null)
surfaceHolder.unlockCanvasAndPost(canvas)
}
}
private fun stopVideoStreaming() {
videoCapture.release()
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... -class-239