-
导入libuvccamera.aar
-
写一个管理类封装UVCCamera相关API
import android.content.Context import android.hardware.usb.UsbDevice import android.os.Handler import android.os.HandlerThread import android.os.Message import android.util.Log import android.view.Surface import com.serenegiant.usb.DeviceFilter import com.serenegiant.usb.IFrameCallback import com.serenegiant.usb.USBMonitor import com.serenegiant.usb.UVCCamera import java.nio.ByteBufferclass UsbCameraManager(mContext:Context) :IFrameCallback{private val TAG = "UsbCameraManager"private var handlerThread = HandlerThread("UsbCameraManager")private var usbMonitor:USBMonitor?=nullprivate var handler: Handler?=nullprivate var uvcCamera:UVCCamera?=nullprivate var mPreviewSurface: Surface? = nullprivate val OPEN_CAMERA = 1init {handlerThread.start()handler = object : Handler(handlerThread.looper){override fun handleMessage(msg: Message) {super.handleMessage(msg)when(msg.what){OPEN_CAMERA->{openCamera(msg.obj as USBMonitor.UsbControlBlock)}}}}usbMonitor = USBMonitor(mContext,object :USBMonitor.OnDeviceConnectListener{override fun onAttach(device: UsbDevice?) {Log.i(TAG, "onAttach ${device?.deviceId}")val filter = DeviceFilter.getDeviceFilters(mContext, com.serenegiant.uvccamera.R.xml.device_filter)usbMonitor?.let {val devices: List<UsbDevice> = it.getDeviceList(filter[0])if (devices != null && devices.isNotEmpty()) {val cameraDevice = devices[0]it.requestPermission(cameraDevice)} else {Log.i(TAG, "连接失败")}}}override fun onDettach(device: UsbDevice?) {Log.i(TAG, "onDettach ${device?.deviceId}")}override fun onConnect(device: UsbDevice?, ctrlBlock: USBMonitor.UsbControlBlock?, createNew: Boolean) {Log.i(TAG, "onConnect ${device?.deviceId}")ctrlBlock?.let {var message = Message().apply {what = OPEN_CAMERAobj = it}handler?.sendMessage(message)}}override fun onDisconnect(device: UsbDevice?, ctrlBlock: USBMonitor.UsbControlBlock?) {Log.i(TAG, "onDisconnect ${device?.deviceId}")stop()}override fun onCancel(device: UsbDevice?) {Log.i(TAG, "onCancel ${device?.deviceId}")stop()}})}public fun start(mSurface: Surface){mPreviewSurface = mSurfaceusbMonitor?.register()}private fun openCamera(ctrlBlock: USBMonitor.UsbControlBlock){if (uvcCamera != null){stop()}uvcCamera = UVCCamera()uvcCamera?.open(ctrlBlock)uvcCamera?.setPreviewDisplay(mPreviewSurface)uvcCamera?.setPreviewSize(1920,1080)uvcCamera?.setFrameCallback(this,UVCCamera.PIXEL_FORMAT_NV21)uvcCamera?.startPreview()}override fun onFrame(frame: ByteBuffer?) {frame?.let {val length = it.capacity()it.clear()var tmpBuf = ByteArray(length)frame[tmpBuf, 0, length]Log.i(TAG,"onFrame $frame.length")}}public fun stop(){uvcCamera?.stopPreview()uvcCamera?.close()uvcCamera?.destroy()uvcCamera = null} }
-
添加预览界面
<TextureViewandroid:id="@+id/preview_view"android:layout_width="match_parent"android:layout_height="match_parent"/>
-
获取TextureView的Surface,开启预览
previewView = findViewById(R.id.preview_view)previewView?.surfaceTextureListener = object : TextureView.SurfaceTextureListener {override fun onSurfaceTextureAvailable(p0: SurfaceTexture, p1: Int, p2: Int) {val surface = Surface(p0)usbCameraManager?.start(surface)}override fun onSurfaceTextureSizeChanged(p0: SurfaceTexture, p1: Int, p2: Int) {}override fun onSurfaceTextureDestroyed(p0: SurfaceTexture): Boolean {return false}override fun onSurfaceTextureUpdated(p0: SurfaceTexture) {}}usbCameraManager = UsbCameraManager(mContext)