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

Unity android 接USBCamera

目录


一、前提

1. unity打包android后,链接USB摄像头,需要USB权限。

二、流程

1.Unity导出android工程,Player配置如图:

2.导出android工程

3.在android工程中找到AndroidManifest.xml加入usb权限相关

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"><application android:extractNativeLibs="true"><activity android:name="com.unity3d.player.UnityPlayerActivity" android:theme="@style/UnityThemeSelector" android:screenOrientation="fullSensor" android:launchMode="singleTask" android:configChanges="mcc|mnc|locale|touchscreen|keyboard|keyboardHidden|navigation|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|fontScale|layoutDirection|density" android:resizeableActivity="false" android:hardwareAccelerated="false"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" />//此处<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /></intent-filter><meta-data android:name="unityplayer.UnityActivity" android:value="true" /><meta-data android:name="android.notch_support" android:value="true" /></activity><meta-data android:name="unity.splash-mode" android:value="0" /><meta-data android:name="unity.splash-enable" android:value="True" /><meta-data android:name="unity.launch-fullscreen" android:value="True" /><meta-data android:name="unity.allow-resizable-window" android:value="False" /><meta-data android:name="notch.config" android:value="portrait|landscape" /></application><uses-feature android:glEsVersion="0x00020000" /><uses-permission android:name="android.permission.INTERNET" /><uses-permission android:name="android.permission.CAMERA" />//此处<uses-feature android:name="android.hardware.usb.host" />//此处<uses-permission android:name="android.permission.USB_PERMISSION" /><uses-feature android:name="android.hardware.camera" android:required="false" /><uses-feature android:name="android.hardware.camera.autofocus" android:required="false" /><uses-feature android:name="android.hardware.camera.front" android:required="false" /><uses-feature android:name="android.hardware.touchscreen" android:required="false" /><uses-feature android:name="android.hardware.touchscreen.multitouch" android:required="false" /><uses-feature android:name="android.hardware.touchscreen.multitouch.distinct" android:required="false" />
</manifest>

备注主要三行如下:

<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
<uses-feature android:name="android.hardware.usb.host" />
<uses-permission android:name="android.permission.USB_PERMISSION" />

4.找到UnityPlayerActivity.java,在unity启动onResume方法里面新增打开USB权限设置:

private static final String TAG = "unity";private static final String ACTION_USB_PERMISSION = "com.android.usb.USB_PERMISSION";private UsbManager usbManager;private UsbDeviceConnection connection;private PendingIntent permissionIntent;private boolean permissionGranted = false;private BroadcastReceiver usbReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();Log.d(TAG, "USB设备连接有变化,链接到:"+action);if (ACTION_USB_PERMISSION.equals(action)) {synchronized (this) {unregisterReceiver(usbReceiver);UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {if (usbDevice != null) {// 用户已授权访问USB设备connectUsbDevice(usbDevice);}} else {Log.d(TAG, "用户未授权访问USB设备");
//                        requestPermission(usbDevice);}}}}};// Resume Unity@Override protected void onResume(){super.onResume();if (MultiWindowSupport.getAllowResizableWindow(this))return;mUnityPlayer.resume();openUsbDevice();}private  void openUsbDevice(){//        //获取注册usbusbManager = (UsbManager) getSystemService(Context.USB_SERVICE);IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);// 获取已连接的USB设备列表HashMap<String, UsbDevice> deviceList = usbManager.getDeviceList();Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();if (deviceIterator.hasNext()) {UsbDevice  device = deviceIterator.next();permissionIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);Log.e(TAG, "getDeviceList: " + device.getVendorId()+"=="+device.getProductId());///注册广播registerReceiver(usbReceiver, filter);usbManager.requestPermission(device, permissionIntent);} else {Log.d(TAG, "没有找到已连接的USB设备");Toast.makeText(this, "没有找到已连接的USB设备", Toast.LENGTH_SHORT).show();}}private void connectUsbDevice(UsbDevice usbDevice) {if (usbDevice == null) {return;}connection = usbManager.openDevice(usbDevice);if (connection != null) {// 在此处执行USB通信操作Log.d(TAG, "USB设备已连接");Toast.makeText(this, "USB设备已连接", Toast.LENGTH_SHORT).show();} else {Log.d(TAG, "无法打开USB设备连接");Toast.makeText(this, "无法打开USB设备连接", Toast.LENGTH_SHORT).show();}}

注意:应用需要打开摄像机权限

http://www.lryc.cn/news/448813.html

相关文章:

  • 演示:基于WPF的DrawingVisual开发的频谱图和律动图
  • 【数据结构初阶】排序算法(中)快速排序专题
  • Redis缓存双写一致性笔记(上)
  • PCB基础
  • PostgreSQL 17:新特性与性能优化深度解析
  • [Linux#58][HTTP] 自己构建服务器 | 实现网页分离 | 设计思路
  • 7.MySQL内置函数
  • 如何快速自定义一个Spring Boot Starter!!
  • 【音视频】ffmpeg其他常用过滤器filter实现(6-4)
  • 云栖3天,云原生+ AI 多场联动,新产品、新体验、新探索
  • jackson对于对象序列化的时候默认空值和手动传入的null的不同处理
  • L8打卡学习笔记
  • VBA解除Excel工作表保护
  • bash: unzip: 未找到命令,sudo: nano:找不到命令
  • tauri开发配置文件和文件夹访问路径问题
  • 【web安全】——信息收集
  • 赵长鹏今日获释,下一步会做什么?币安透露2024年加密货币牛市的投资策略!
  • SpringMVC之ContextHolder
  • 什么是SQL注入?
  • 混合密码系统——用对称密钥提高速度,用公钥密码保护会话密钥
  • Three.js粒子系统与特效
  • Tableau数据可视化入门
  • Linux云计算 |【第四阶段】RDBMS1-DAY2
  • 后台监控中的云边下控耗时、边缘采集耗时 、云边下控量
  • 【学习笔记】手写 Tomcat 四
  • 探索基因奥秘:汇智生物如何利用组蛋白甲基化修饰测序技术革新农业植物基因组研究?
  • 二叉搜索树的介绍、模拟实现二叉搜索树、leetcode---根据二叉树创建字符串、leetcode---二叉树的最近公共祖先等的介绍
  • 人工智能的基本概念与发展历程
  • 【IPV6从入门到起飞】5-6 IPV6+Home Assistant(ESPHome+ESP-cam)实时监控
  • 生成式AI的未来