当前位置: 首页 > news >正文

怎么把现有网站开发php徐州焊接球网架公司

怎么把现有网站开发php,徐州焊接球网架公司,好看的影视大全下载,四川省建筑施工企业特种作业人员Android在后台读取UVC摄像头的帧数据流并推送 添加UvcCamera依赖库 使用原版的 saki4510t/UVCCamera 在预览过程中断开可能会闪退,这里使用的是 jiangdongguo/AndroidUSBCamera 中修改的版本,下载到本地即可。 https://github.com/jiangdongguo/AndroidU…

Android在后台读取UVC摄像头的帧数据流并推送

  1. 添加UvcCamera依赖库
    使用原版的 saki4510t/UVCCamera 在预览过程中断开可能会闪退,这里使用的是
    jiangdongguo/AndroidUSBCamera 中修改的版本,下载到本地即可。
    https://github.com/jiangdongguo/AndroidUSBCamera

  2. 监听UVC连接回调

    mUSBMonitor = USBMonitor(context, mOnDeviceConnectListener)mUSBMonitor.register()public interface OnDeviceConnectListener {void onAttach(UsbDevice device);void onDetach(UsbDevice device);void onConnect(UsbDevice device, UsbControlBlock ctrlBlock, boolean createNew);void onDisconnect(UsbDevice device, UsbControlBlock ctrlBlock);void onCancel(UsbDevice device);}
  1. 检测到UVC后连接该设备

USB连接上会回调,onAttach, 本地判断连接上的USB设备是否UVC,如果是的话可以尝试调用连接该对象。调用mUSBMonitor.requestPermission(cam)就会请求权限并且连接该对象。连接成功后会回调 onConnect。

    var connectJob: Disposable? = nulloverride fun onAttach(device: UsbDevice?) {BLLog.i(TAG, "onAttach")BLLog.toast("USB_DEVICE_ATTACHED")connectJob?.dispose()connectJob = CommonUtils.runDelayed(1000) {autoConnectUvcDevice()}}private fun autoConnectUvcDevice() {val context = BLSession.getApplicationContext()val filter: List<DeviceFilter> =DeviceFilter.getDeviceFilters(context, R.xml.device_filter_uvc)val devs: List<UsbDevice> = mUSBMonitor?.getDeviceList(filter[0]) ?: listOf()val cam = findUsbCam(devs)BLLog.log2File(TAG, "autoConnect 共有USB数量:${devs.size}, Uvc: ${cam?.productName}")if (cam == null) {BLLog.i(TAG, "未连接USB摄像头")} else {mUSBMonitor.requestPermission(cam)}}

device_filter_uvc.xml

<usb><usb-device class="239" subclass="2" />	<!-- all device of UVC -->
</usb>

如何判断该连接对象是UVC对象,如果名字中包含USBCam或 interfaceClass = USB_CLASS_VIDEO

    private fun findUsbCam(devs: List<UsbDevice>): UsbDevice? {for (dev in devs) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {val name = "" + dev.productName + dev.manufacturerNameBLLog.i(TAG, "findUsbCam name:$name")if (name.contains("USBCam")) {return dev}for (i in 0 until dev.interfaceCount) {val inter = dev.getInterface(i)BLLog.i(TAG, "getInterface($i):$inter")if (inter.interfaceClass == UsbConstants.USB_CLASS_VIDEO) {return dev}}}}return null}
  1. 在onConnect中保存UvcCamera对象
            override fun onConnect(device: UsbDevice?,ctrlBlock: USBMonitor.UsbControlBlock?,createNew: Boolean) {BLLog.i(TAG, "onConnect  ${device?.productName}")synchronized(mSync) {try {// 保存最新的Uvc对象val camera = UVCCamera();camera.open(ctrlBlock)BLLog.log2File(TAG,"supportedSize:" + camera.supportedSize + "thread:" + Thread.currentThread().name)if (applyPreviewSize(camera)) {mUVCCamera?.destroy()mUVCCamera = camerapreviewStatus = PreViewStatus.None} else {BLLog.console(TAG, "UVC不支持此分辨率:$defPreviewSize")if (mUVCCamera == null) {mUVCCamera = camera}}uvcChangedSub.onNext(true)} catch (e:Exception){BLLog.log2File(TAG, "onConnect Recv exception: $e")}}}
  1. 预览并获取YUC视频帧
    如果需要预览到UI中显示,需要创建SurfaceView或者TextureView.
        mUVCCamera?.setPreviewDisplay(previewSurface)mUVCCamera?.startPreview()

如果不需要预览到UI中显示,可以new一个SurfaceTexture对象传进去即可;必须要调用预览才能获取到YUV数据。

        val surfaceTexture = SurfaceTexture(0)mUVCCamera?.setPreviewTexture(surfaceTexture)BLLog.i(TAG, "startPreviewWithAir")mUVCCamera?.startPreview()

预览后获取YUV帧流:

        val width = mUVCCamera?.previewSize?.width ?: defPreviewSize.widthval height = mUVCCamera?.previewSize?.height ?: defPreviewSize.heightyuvCallback = callbackmUVCCamera?.setFrameCallback({ buffer: ByteBuffer ->
//            BLLog.i(TAG, "onFrame ${width}*${height}")// yuv格式if (acceptFrame()) {val format = MediaFormat.createVideoFormat("", width, height)val data = ByteArray(buffer.remaining())buffer.get(data)val frame = YuvFrameData(format, data, width, height)yuvCallback?.onGetYuvData(frame)}}, UVCCamera.PIXEL_FORMAT_NV21)

获取到的YUV帧可以使用其他推流SDK进行推流即可,比如使用阿里云推流SDK推流。
这完成可以在后台进行推流,不需要UI上展示,节省设备的性能。

  1. 连接类参考:
// Uvc设备连接器
object UvcConnector : BaseBussModel(ModelType.Shared) {private val TAG = UvcConnector::class.java.simpleNameprivate val KEY_UVC_PREVIEW_SIZE = "KEY_UVC_PREVIEW_SIZE"// 默认支持:640*480, 1920*1080private var defPreviewSize = MySize.parseSize("1920*1080")!!enum class PreViewStatus {None,Visible,Air,}private lateinit var mUSBMonitor: USBMonitor@Volatileprivate var mUVCCamera: UVCCamera? = nullprivate val mSync = Object()private var previewStatus = PreViewStatus.None@Volatileprivate var yuvCallback: IMediaKit.OnYuvListener? = null// 状态变更消息private var uvcChangedSub = PublishSubject.create<Boolean>()override fun onStartUp() {super.onStartUp()BLLog.i(TAG, "onStartUp")val context = BLSession.getApplicationContext()mUSBMonitor = USBMonitor(context, mOnDeviceConnectListener)mUSBMonitor.register()CommonUtils.runAsync(::loadPreviewSize)}override fun onShutdown() {BLLog.i(TAG, "onShutdown")mUSBMonitor.unregister()mUSBMonitor.destroy()super.onShutdown()}fun hasUvcDevice(): Boolean {return mUVCCamera != null}fun previewStatus(): PreViewStatus {return previewStatus}fun getSubject() = uvcChangedSubfun getNowSize(): MySize? {return mUVCCamera?.previewSize?.let {MySize(it.width, it.height)}}fun getExpSize(): MySize {return defPreviewSize}fun startPreview(previewSurface: Surface): CallResult {BLLog.i(TAG, "startPreview")if (!hasUvcDevice()) {return CallResult(false, "未连接设备")}if (previewStatus == PreViewStatus.Air) {mUVCCamera?.stopPreview()}if (!applyPreviewSize(mUVCCamera)) {BLLog.console(TAG, "UVC不支持此分辨率:$defPreviewSize")return CallResult(false, "UVC不支持此分辨率:$defPreviewSize")}mUVCCamera?.setPreviewDisplay(previewSurface)mUVCCamera?.startPreview()previewStatus = PreViewStatus.Visibleif (yuvCallback != null) {setYuvCallback(yuvCallback!!)}uvcChangedSub.onNext(true)return CallResult(true, "成功")}fun stopPreview() {BLLog.i(TAG, "stopPreview")if (previewStatus != PreViewStatus.Visible) {return}mUVCCamera?.stopPreview()previewStatus = PreViewStatus.None// 需要接收数据if (yuvCallback != null) {startPreviewWithAir()setYuvCallback(yuvCallback!!)}uvcChangedSub.onNext(true)}fun clearYuvCallback() {yuvCallback = nullif (previewStatus == PreViewStatus.Air) {mUVCCamera?.stopPreview()previewStatus = PreViewStatus.NoneuvcChangedSub.onNext(true)}}fun setYuvCallback(callback: IMediaKit.OnYuvListener): Boolean {if (mUVCCamera == null) {return false}if (previewStatus == PreViewStatus.None) {startPreviewWithAir()}val width = mUVCCamera?.previewSize?.width ?: defPreviewSize.widthval height = mUVCCamera?.previewSize?.height ?: defPreviewSize.heightyuvCallback = callbackmUVCCamera?.setFrameCallback({ buffer: ByteBuffer ->
//            BLLog.i(TAG, "onFrame ${width}*${height}")// yuv格式if (acceptFrame()) {val format = MediaFormat.createVideoFormat("", width, height)val data = ByteArray(buffer.remaining())buffer.get(data)val frame = YuvFrameData(format, data, width, height)yuvCallback?.onGetYuvData(frame)}}, UVCCamera.PIXEL_FORMAT_NV21)return true}private fun acceptFrame(): Boolean {return Random.nextInt(30) <= 25}private fun startPreviewWithAir() {if (!applyPreviewSize(mUVCCamera)) {BLLog.console(TAG, "UVC不支持此分辨率:$defPreviewSize")return}val surfaceTexture = SurfaceTexture(0)mUVCCamera?.setPreviewTexture(surfaceTexture)BLLog.i(TAG, "startPreviewWithAir")mUVCCamera?.startPreview()previewStatus = PreViewStatus.AiruvcChangedSub.onNext(true)}private fun findUsbCam(devs: List<UsbDevice>): UsbDevice? {for (dev in devs) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {val name = "" + dev.productName + dev.manufacturerNameBLLog.i(TAG, "findUsbCam name:$name")if (name.contains("USBCam")) {return dev}for (i in 0 until dev.interfaceCount) {val inter = dev.getInterface(i)BLLog.i(TAG, "getInterface($i):$inter")if (inter.interfaceClass == UsbConstants.USB_CLASS_VIDEO) {return dev}}}}return null}private fun autoConnectUvcDevice() {val context = BLSession.getApplicationContext()val filter: List<DeviceFilter> =DeviceFilter.getDeviceFilters(context, R.xml.device_filter_uvc)val devs: List<UsbDevice> = mUSBMonitor?.getDeviceList(filter[0]) ?: listOf()val cam = findUsbCam(devs)BLLog.log2File(TAG, "autoConnect 共有USB数量:${devs.size}, Uvc: ${cam?.productName}")if (cam == null) {BLLog.i(TAG, "未连接USB摄像头")} else {mUSBMonitor.requestPermission(cam)}}private fun applyPreviewSize(camera: UVCCamera?):Boolean {if (camera == null){return false}try {camera.setPreviewSize(defPreviewSize.width,defPreviewSize.height,UVCCamera.FRAME_FORMAT_MJPEG)} catch (e: IllegalArgumentException) {BLLog.log2File(TAG, "setPreviewSize1 $defPreviewSize: $e")try {// fallback to YUV modecamera.setPreviewSize(defPreviewSize.width,defPreviewSize.height,UVCCamera.DEFAULT_PREVIEW_MODE)} catch (e1: IllegalArgumentException) {BLLog.log2File(TAG, "setPreviewSize2 $defPreviewSize: $e1")return false}}return true}private val mOnDeviceConnectListener: USBMonitor.OnDeviceConnectListener =object : USBMonitor.OnDeviceConnectListener {var connectJob: Disposable? = nulloverride fun onAttach(device: UsbDevice?) {BLLog.i(TAG, "onAttach")BLLog.toast("USB_DEVICE_ATTACHED")connectJob?.dispose()connectJob = CommonUtils.runDelayed(1000) {autoConnectUvcDevice()}}override fun onDetach(device: UsbDevice?) {BLLog.i(TAG, "onDetach")BLLog.toast("USB_DEVICE_DETACHED")synchronized(mSync) {if (mUVCCamera != null) {mUVCCamera?.destroy()mUVCCamera = nullpreviewStatus = PreViewStatus.NoneuvcChangedSub.onNext(true)}}}override fun onConnect(device: UsbDevice?,ctrlBlock: USBMonitor.UsbControlBlock?,createNew: Boolean) {BLLog.i(TAG, "onConnect  ${device?.productName}")synchronized(mSync) {try {// 保存最新的Uvc对象val camera = UVCCamera();camera.open(ctrlBlock)BLLog.log2File(TAG,"supportedSize:" + camera.supportedSize + "thread:" + Thread.currentThread().name)if (applyPreviewSize(camera)) {mUVCCamera?.destroy()mUVCCamera = camerapreviewStatus = PreViewStatus.None} else {BLLog.console(TAG, "UVC不支持此分辨率:$defPreviewSize")if (mUVCCamera == null) {mUVCCamera = camera}}uvcChangedSub.onNext(true)} catch (e:Exception){BLLog.log2File(TAG, "onConnect Recv exception: $e")}}}override fun onDisconnect(device: UsbDevice?, ctrlBlock: USBMonitor.UsbControlBlock?) {BLLog.i(TAG, "onDisconnect ${device?.productName}")synchronized(mSync) {mUVCCamera?.destroy()mUVCCamera = nullpreviewStatus = PreViewStatus.NoneuvcChangedSub.onNext(true)}}override fun onCancel(device: UsbDevice?) {BLLog.i(TAG, "onCancel")}}fun setPreviewSize(size: MySize) {defPreviewSize = sizeSharedPreferenceHelper.saveCustom(KEY_UVC_PREVIEW_SIZE, size.toString())}private fun loadPreviewSize() {val str = SharedPreferenceHelper.loadCustom(KEY_UVC_PREVIEW_SIZE, "")defPreviewSize = MySize.parseSize(str) ?: defPreviewSize}
}
http://www.yayakq.cn/news/316517/

相关文章:

  • 电子商务网站建设岗位要求wordpress调用导航菜单
  • 宜兴建设局 审图中心 网站搭建商城到底哪家好
  • 网站搭建合同如何让百度口碑收录自己的网站
  • 外贸seo培训做网站排名优化有用吗
  • 松江建设投资有限公司网站线上营销推广方式都有哪些
  • 大学网站建设方案wordpress qq快捷登陆
  • 四川省城乡住房建设厅网站编写网站策划书
  • 学多久可以做网站 知乎注册一个公司一年需要多少钱
  • wordpress 3.3.1wordpress seo不好
  • 水果网站建设方案模板建站哪里有
  • 凡科建站平台昆明seo建站
  • 大同格泰网站建设网站建设与维护的论述题
  • 网站做跳转的意义wordpress被百度收录
  • 首页调用网站栏目id英文案例网站
  • 江门市建设工程投标网站云建站自动建站系统源码
  • asp.net 网站写好后如何运行网站模块
  • 高效网站建设咨询网站长图怎么做
  • 计算机网站建设和维护sem竞价教程
  • 长沙建网站一般要多少钱国外专业做汽配的网站
  • 有网站源码怎么做网站重庆网站建设的价格低
  • 广东建设工程注册中心网站网站怎么换域名
  • 常用的网站推广方法有哪些用自己的电脑做服务器建网站
  • 做网站怎么删除图片网站推广网站关键词排名怎么做
  • 做网站 使用权 所有权株洲专业建设网站
  • 能制作视频的软件厦门做网站优化公司
  • 网上做视频赚钱的网站有哪些wordpress 源码解读
  • 网站托管服务方案wordpress地图插件
  • 用vps建网站备案为什么要用模板建站?
  • 中山企业门户网站建设做网站一定要psd吗
  • 淮北建网站南京市高淳区住房与城乡建设局网站