USB Camera Information Acquisition Method, Device, Terminal Device and Storage Medium
By obtaining access rights to USB camera devices on Android mobile devices and establishing direct communication connections, the compatibility problem between mobile devices and USB cameras is solved, and fast and stable image data acquisition and simplified development process are achieved, improving the user experience.
Patent Information
- Application Number
- CN202410944091.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-15
- Publication Date
- 2025-08-01
- Estimated Expiration
- 2044-07-15
AI Technical Summary
Mobile devices on existing Android systems are difficult to effectively compatible with USB cameras and cannot meet the video acquisition needs, especially due to poor compatibility caused by the lack of V4L2 driver interface.
By acquiring the USB camera device on the mobile device, sending open access requests, establishing a connection relationship between the local environment and the camera, directly communicating with the camera device, avoiding relying on the V4L2 interface, and achieving fast and stable data transmission and image data acquisition.
Improves compatibility between mobile devices and USB cameras, supports flexible permission control, simplifies the development process, achieves faster and more stable data transmission and real-time image acquisition, and improves user experience.
Smart Images

Figure CN118660216B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of the interoperability between hardware and software, and particularly to a method, a device, a terminal device and a storage medium for collecting USB camera information. Background Art
[0002] In the era when short videos and live broadcasts are prevalent, video capture has also become a hot topic of concern. Most people use devices such as mobile phones or tablets to shoot videos or conduct live broadcasts. However, the imaging quality of the cameras on mobile phones and tablets is limited by the space technology of mobile phones and tablets, and there is still a significant gap between the imaging quality of the cameras on mobile phones and tablets and that of professional cameras. In order to improve the video capture quality, it is necessary to externally connect a USB camera to a device such as a mobile phone or a tablet. In addition, in application scenarios such as video conferencing and remote teaching, it is also necessary to externally connect a USB camera to a mobile device to collect video data. For example, in an examination system, multiple USB cameras can be externally connected to a tablet to shoot the examination operation process of candidates.
[0003] In the prior art, the common connection method is to connect the camera to a computer, a laptop or other intelligent devices through a USB interface, and then use the V4L2 (Video for Linux 2) driver interface and the / dev / videoX file to support the externally connected USB camera device. V4L2 is a standard interface for video device drivers in the Linux kernel, which allows application programs to access and control video devices, including USB cameras. Through the V4L2 driver interface, an application program can obtain the image data captured by the camera sensor and perform real-time processing, display or storage. However, most Android mobile phones and tablets on the market do not directly open the V4L2 driver interface, such as Huawei, Xiaomi, etc.; usually only some manufacturers of customized tablets will default to open the V4L2 driver, such as Huawei, Xiaomi, etc., usually only individual manufacturers of customized tablets will default to open the V4L2 driver interface, and the compatibility is poor. This results in that most mobile devices with Android systems are actually difficult to collect video through an externally connected USB camera, and cannot meet the growing video capture needs of users. Summary of the Invention
[0004] The present invention aims to provide a method, a device, a terminal device and a storage medium for collecting USB camera information to solve the above technical problems, improve the compatibility between mobile devices based on the Android system and third-party camera devices, and meet the video capture needs of users.
[0005] To solve the above technical problems, the present invention provides a method for collecting USB camera information, including:
[0006] Obtain the camera device connected to the mobile device;
[0007] Send an access permission opening request to the camera device so that the camera device opens access permission to the mobile device;
[0008] Obtain the connection information of each first camera device and establish a connection relationship between the local environment and each of the first camera devices based on the connection information; wherein, the first camera device is a camera device that opens access permission to the mobile device;
[0009] Collect the image data of each camera device into the local environment according to the established connection relationship.
[0010] In the above solution, by sending an access permission opening request to the camera device, it is ensured that the user can flexibly control the usage permission of the camera according to needs. It improves security and at the same time supports the user to make settings in different scenarios. Establishing a connection relationship between the local environment and the camera based on the connection information can achieve faster and more stable data transmission, reduce the complexity of the middle layer, and improve the data processing efficiency. Through the established connection relationship, the image data of the camera can be collected in real time to meet the user's requirements for real-time processing and display. This is particularly important for application scenarios such as live broadcast and monitoring. And this method directly communicates with the camera device and does not rely on the V4L2 interface solution, which simplifies the development process. Developers can more easily achieve cross-device compatibility, reduce the complex adaptation of the underlying driver, and users can more conveniently access the USB camera when using the mobile device, meeting various needs such as daily video collection and live broadcast, and improving the overall user experience.
[0011] In one implementation, the obtaining of the camera devices connected to the mobile device specifically includes:
[0012] Obtain the list of USB devices connected to the mobile device based on the UsbManager instance,
[0013] Filter the USB devices in the USB device list according to a preset extension function to obtain the USB camera devices connected to the mobile device.
[0014] In one implementation, the sending of an access permission opening request to the camera device so that the camera device opens access permission to the mobile device specifically includes:
[0015] Obtain the access permission status of each camera device relative to the mobile device;
[0016] When a second camera device that has not granted access permission to the mobile device is detected, the requestPermission method is called to send an access permission opening request to the second camera device; wherein, the requestPermission method is to request access permission to the second camera device from the operating system of the mobile device.
[0017] In one implementation, before obtaining the connection information of each camera device, it further includes real-time monitoring of the device status of the camera device, specifically:
[0018] Register a broadcast receiver in the operating system of the mobile device and add a broadcast listening action in the broadcast receiver; wherein, the broadcast listening action includes the insertion, removal, and access permission opening request result of the camera device;
[0019] Based on the broadcast receiver, monitor the access permission opening request result and the device plugging and unplugging status of the camera device relative to the mobile device.
[0020] In one implementation, the obtaining of the connection information of each first camera device and the establishment of the connection relationship between the local environment and each first camera device specifically includes:
[0021] Open a third camera device based on the openDevice method and obtain the connection information of the third camera device; wherein, the openDevice method is used to open the connection between the mobile device and the specified third camera device;
[0022] Transmit the connection information to the local environment so that the local environment establishes a connection with the third camera device; wherein, the connection information includes: camera device name, product ID, vendor ID, and file descriptor.
[0023] In one implementation, the collecting of the image data of each camera device into the local environment according to the established connection relationship specifically includes:
[0024] Create and initialize a first context; wherein, the communication requirements between the camera device and the mobile device are defined in the first context.
[0025] Obtain a fourth camera device that has established a connection relationship with the mobile device and open the connection with the fourth camera device.
[0026] Based on the callback function, real-time obtain the image data of the fourth camera device.
[0027] In a second aspect, the present application also provides a USB camera information acquisition device, including: a device acquisition module, a permission opening module, a connection establishment module, and a data acquisition module;
[0028] The device acquisition module is used to acquire the camera devices connected to the mobile device;
[0029] The permission opening module is used to send an access permission opening request to the camera device so that the camera device opens the access permission to the mobile device;
[0030] The connection establishment module is used to acquire the connection information of each first camera device and establish a connection relationship between the local environment and each of the first camera devices based on the connection information; wherein, the first camera device is the camera device that opens the access permission to the mobile device;
[0031] The data acquisition module is used to collect the image data of each camera device into the local environment according to the established connection relationship.
[0032] In the above solution, by sending an access permission opening request to the camera device, it is ensured that the user can flexibly control the usage permission of the camera according to the needs. It improves security and at the same time supports the user to make settings in different scenarios. Establishing a connection relationship between the local environment and the camera based on the connection information can achieve faster and more stable data transmission, reduce the complexity of the intermediate layer, and improve the data processing efficiency. Through the established connection relationship, the image data of the camera can be collected in real time to meet the user's requirements for real-time processing and display. This is particularly important for application scenarios such as live streaming and monitoring. And this method communicates directly with the camera device and does not rely on the V4L2 interface solution, simplifies the development process, enables developers to more easily achieve cross-device compatibility, reduces the complex adaptation of the underlying driver, and allows users to more conveniently access the USB camera when using the mobile device, meeting various needs such as daily video acquisition and live streaming, and enhancing the overall user experience.
[0033] In one implementation, the device acquisition module is used to acquire the camera devices connected to the mobile device, specifically including:
[0034] Obtaining the list of USB devices connected to the mobile device based on the UsbManager instance,
[0035] Filtering the USB devices in the USB device list according to a preset extension function to obtain the USB camera devices connected to the mobile device.
[0036] In one implementation, the permission enabling module is used to send an access permission opening request to the camera device so that the camera device opens the access permission to the mobile device, specifically including:
[0037] Obtain the access permission status of each camera device relative to the mobile device;
[0038] When it is detected that there is a second camera device that has not opened the access permission to the mobile device, call the requestPermission method to send an access permission opening request to the second camera device; wherein, the requestPermission method is to request the operating system of the mobile device for the access permission to the second camera device.
[0039] In one implementation, before obtaining the connection information of each camera device, it further includes real-time monitoring of the device status of the camera device, specifically:
[0040] Register a broadcast receiver in the operating system of the mobile device and add a broadcast listening action in the broadcast receiver; wherein, the broadcast listening action includes the insertion, removal, and access permission opening request result of the camera device;
[0041] Based on the broadcast receiver, monitor the access permission opening request result and the device plugging and unplugging status of the camera device relative to the mobile device.
[0042] In one implementation, the connection establishment module is used to obtain the connection information of each first camera device and establish a connection relationship between the local environment and each first camera device based on the connection information, specifically including:
[0043] Open a third camera device based on the openDevice method and obtain the connection information of the third camera device; wherein, the openDevice method is used to open the connection between the mobile device and the specified third camera device;
[0044] Transmit the connection information to the local environment so that the local environment establishes a connection with the third camera device; wherein, the connection information includes: camera device name, product ID, vendor ID, and file descriptor.
[0045] In one implementation, the data acquisition module is used to collect the image data of each camera device into the local environment according to the established connection relationship, specifically including:
[0046] Create and initialize a first context; wherein, the communication requirements between the camera device and the mobile device are defined in the first context;
[0047] Obtain a fourth camera device that has established a connection relationship with the mobile device and open the connection with the fourth camera device;
[0048] Obtain the image data of the fourth camera device in real time based on the callback function.
[0049] In a third aspect, the present application further provides a terminal device, including a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor. When the processor executes the computer program, the above-mentioned USB camera information acquisition method is implemented.
[0050] In a fourth aspect, the present application further provides a computer-readable storage medium. The computer-readable storage medium includes a stored computer program. Wherein, when the computer program runs, it controls the device where the computer-readable storage medium is located to execute the above-mentioned USB camera information acquisition method. BRIEF DESCRIPTION OF THE DRAWINGS
[0051] Figure 1 It is a schematic flowchart of a USB camera information acquisition method provided in an embodiment of the present invention;
[0052] Figure 2 It is a schematic block diagram of a USB camera information acquisition device provided in an embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0053] The following will further describe in detail the specific embodiments of the present invention in conjunction with the drawings and embodiments. The following embodiments are used to illustrate the present invention, but are not used to limit the scope of the present invention.
[0054] The terms "first" and "second" in the specification, claims and drawings of the present application are used to distinguish different objects, rather than to describe a specific order. In addition, the terms "comprising" and "having" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product or device that includes a series of steps or units is not limited to the listed steps or units, but optionally further includes steps or units that are not listed, or optionally further includes other steps or units inherent to these processes, methods, products or devices.
[0055] Referring to "embodiment" herein means that a specific feature, structure, or characteristic described in conjunction with the embodiment can be included in at least one embodiment of the present application. The phrase appears in various places in the specification and does not necessarily refer to the same embodiment, nor is it an independent or alternative embodiment that is mutually exclusive with other embodiments. Those skilled in the art will explicitly and implicitly understand that the embodiments described herein can be combined with other embodiments.
[0056] First, some terms in this application are explained for the convenience of those skilled in the art to understand.
[0057] (1) UsbManager: It is a class in the Android system used to manage the interaction and communication with USB devices. It allows applications to detect all USB devices connected to the Android device and request permission to communicate with them. Through the UsbManager, an application can obtain connection information about the USB device and further establish a communication channel with it.
[0058] (2) Docking Station: It is a device commonly used to expand the functions and connectivity of a computer or mobile device. The docking station usually contains multiple different types of interfaces, such as USB ports, HDMI ports, Ethernet ports, audio interfaces, etc., allowing users to connect the device to the docking station to achieve functions such as expanding interfaces, charging, converting a mobile device into a workstation mode, and simplifying connections.
[0059] (3) hasPermission(device): The hasPermission() method of the UsbManager is used to check whether the given USB device has been granted permission for the application to access. If it returns true, it means the application has been authorized to access the USB device; if it returns false, it means the application has not been authorized to access the USB device.
[0060] (4) requestPermission(device, pendingIntent): The requestPermission() method is used to request the system to grant the application access permission to the specified USB device. When calling this method, it is necessary to pass in the USB device object for which permission is requested and a PendingIntent object used to receive the broadcast notification of the authorization result.
[0061] Embodiment 1
[0062] Refer to Figure 1 , Figure 1 which is a schematic flowchart of a method for collecting USB camera information provided in an embodiment of the present invention. The embodiment of the present invention provides a method for collecting USB camera information, including steps 101 to 104, and the specific steps are as follows:
[0063] Step 101: Obtain the camera device connected to the mobile device.
[0064] In one embodiment, the obtaining of the camera device connected to the mobile device specifically includes: obtaining a list of USB devices connected to the mobile device based on an instance of UsbManager, filtering the USB devices in the USB device list according to a preset extension function, and obtaining the USB camera device connected to the mobile device.
[0065] When Android devices such as mobile phones and tablets are connected to a USB camera device, multiple USB devices can be connected simultaneously through a docking station. When collecting the image information of the USB camera, it is necessary to first determine whether the USB device connected to the mobile device is a camera device. In the embodiment of the present invention, all USB devices are obtained through the UsbManager method, and the USB camera devices are filtered out. Preferably, the code implementation process can be as follows:
[0066] Val usbManager = context.getSystemService(AppCompatActivity.USB_SERVICE) as UsbManager;
[0067] val usbDevices = usbManager.deviceList.value.filter { it.isUVCDevice()};
[0068] The above code obtains the system service USB_SERVICE through the getSystemService() method and converts it into an instance of UsbManager for convenient management operations of candidate USB devices. usbManager.deviceList will return a MutableLiveData<HashMap<String, UsbDevice>> object representing all USB devices currently connected to the device..value is used to obtain the actual device list, and filter{it.isUVCDevice()} calls the custom isUVCDevice() extension function to check each UsbDevice, that is, the USB device object, to see if it meets the characteristics of a USB camera device. The code implementation process of its filtering is as follows:
[0069] fun UsbDevice.isUVCDevice(): Boolean{
[0070] return deviceClass == 239 && deviceSubclass == 2
[0071] }
[0072] Among them, isUVCDevice() is an extension function used to determine whether a given UsbDevice is a USB camera device. In the description information of the USB device, the determination is made through deviceClass and deviceSubclass. deviceClass == 239 indicates that the device belongs to the video device class, and deviceSubclass == 2 indicates that the device is a subclass of the USB video device (UVC device). Through the above code, the USB camera device connected to the mobile device can be obtained.
[0073] Step 102: Send an access permission opening request to the camera device so that the camera device opens the access permission to the mobile device.
[0074] In one embodiment, the sending an access permission opening request to the camera device so that the camera device opens the access permission to the mobile device specifically includes: obtaining the access permission status of each camera device relative to the mobile device; when it is detected that there is a second camera device that has not opened the access permission to the mobile device, calling the requestPermission method to send an access permission opening request to the second camera device; where the requestPermission method is to request the access permission to the second camera device from the operating system of the mobile device.
[0075] In the embodiment of the present invention, the hasPermission method of UsbManager is used to determine whether the camera device grants access permission to the mobile device. If the permission is not granted, the requestPermission method of UsbManager is used to request authorization. The code implementation process is as follows:
[0076] if(usbManager.hasPermission(device)){ / / Already authorized
[0077] }else{
[0078] / / Request authorization
[0079] usbManager.requestPermission(
[0080] device,
[0081] PendingIntent.getBroadcast(
[0082] context,110,Intent(ACTION_USB_PERMISSION),
[0083] PendingIntent.FLAG_MUTABLE))
[0084] }
[0085] Among them, PendingIntent.getBroadcast() is used to obtain a PendingIntent object for sending a broadcast, which contains the processing logic for the authorization result. When the authorization result is returned, the system will send a broadcast with the authorization status, and the application can handle the authorization result by registering the corresponding broadcast receiver. The requestPermission() method of UsbManager is used to request the Android operating system to grant the application access permission to the specified USB device, that is, the camera device. Here, the "system" refers to the operating system running on the Android device, that is, the Android operating system. The Android operating system is responsible for managing the hardware and software resources of the device, including the connection and permission management of USB devices. When the application uses the requestPermission() method of UsbManager, it is actually sending a request to the Android operating system, hoping to access and operate the USB device in a secure and controlled environment. The system will decide whether to grant the application the requested USB device access permission according to the user's operations and permission management policies.
[0086] In one embodiment, before obtaining the connection information of each of the camera devices, it further includes real-time monitoring of the device status of the camera device, specifically: registering a broadcast receiver in the operating system of the mobile device and adding a broadcast listening action in the broadcast receiver; wherein, the broadcast listening action includes the insertion, removal, and access permission open request result of the camera device; monitoring the access permission open request result and the device plugging and unplugging status of the camera device relative to the mobile device based on the broadcast receiver.
[0087] In the embodiment of the present invention, before registering a broadcast receiver in the Android operating system of the mobile device, it is necessary to first define the broadcast receiver through the BroadcastReceiver object and the onReceive() method. The code implementation process of defining the broadcast receiver is as follows:
[0088] val mUsbReceiver = object : BroadcastReceiver() {
[0089]
[0090] Among them, the BroadcastReceiver object is used to receive broadcast messages sent by the operating system and execute corresponding logic when receiving broadcasts. The nReceive() method distinguishes different events according to the action field of the broadcast, including the insertion of the camera device, the removal of the camera device, and the result of the permission opening request.
[0091] Furthermore, a filter is defined through IntentFilter and the addAction() method. The code implementation process of its filter is as follows:
[0092] val intentFilter = IntentFilter()
[0093] intentFilter.addAction(ACTION_USB_PERMISSION)
[0094] intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED)
[0095] intentFilter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED)
[0096] Among them, IntentFilter is used to specify which broadcast events will trigger the onReceive() method of this broadcast receiver. The addAction() method is used to add broadcast actions to be listened for, including the result of the camera device access permission opening request and the plugging and unplugging events of the camera device.
[0097] After the broadcast receiver is defined, it can be registered. The code implementation process of registering the broadcast receiver is as follows:
[0098] registerReceiver(mUsbReceiver, intentFilter, 0)
[0099] Among them, the registerReceiver() method registers the broadcast receiver mUsbReceiver into the context of the application. The intentFilter parameter specifies the set of broadcast actions that the receiver will handle. The third parameter (flag) is usually 0, indicating the default behavior, that is, the registered broadcast receiver will persist until it is unregistered by calling unregisterReceiver(). After the broadcast receiver is registered, when the corresponding actions such as the camera device being inserted, the camera device being removed, and the result feedback of the permission open request occur, the broadcast class object (mUsbReceiver) can receive (monitor) the corresponding callbacks.
[0100] Step 103: Obtain the connection information of each first camera device and establish a connection relationship between the local environment and each of the first camera devices; wherein, the first camera device is a camera device that has granted access permissions to the mobile device.
[0101] In one embodiment, the obtaining the connection information of each first camera device and establishing a connection relationship between the local environment and each of the first camera devices specifically includes: opening a third camera device based on the openDevice method and obtaining the connection information of the third camera device; wherein, the openDevice method is used to open the connection between the mobile device and the specified third camera device; transmitting the connection information to the local environment so that the local environment establishes a connection with the third camera device; wherein, the connection information includes: camera device name, product ID, vendor ID, and file descriptor.
[0102] In the embodiment of the present invention, after the user grants access permissions, the openDevice method of the UsbManager is used to open the USB camera, obtain the connection information of the USB camera, and transfer the connection information to the native to prepare for establishing a connection. The code implementation process is as follows:
[0103] connection = mUsbManager.openDevice(device)
[0104] val split = device.deviceName.split(" / ")
[0105] val mBusNum = split[split.size - 2].toInt()
[0106] val mDevNum = split[split.size - 1].toInt()
[0107] val sb = StringBuilder(split[0])
[0108] for (i in 1 until split.size - 2) sb.append(" / ").append(split[i])
[0109] nativeOpen(
[0110] nativeUVCCamera, device.vendorId, device.productId,
[0111] connection!!.fileDescriptor,
[0112] mBusNum, mDevNum, sb.toString() )
[0114] Among them, the openDevice() method is used to open a connection to the specified USB device. It returns a UsbDeviceConnection object that allows the application to communicate with the USB device. The parameter it takes in is a UsbDevice object representing the USB device to be opened. If successful, it returns a UsbDeviceConnection object; if the opening fails, it returns null. device.deviceName provides the name of the device, usually containing information such as the bus number and device number. The device name is split into multiple parts by the split(" / ") method and the bus number (mBusNum) and device number (mDevNum) are extracted for subsequent use. The device name without the bus number and device number is reconstructed through StringBuilder for convenient subsequent calls. nativeOpen() is a function that calls a native layer method. It passes several parameters: nativeUVCCamera: a reference to the native layer. device.vendorId and device.productId: the vendor ID and product ID of the device respectively, which are used to uniquely identify the USB device. connection!!.fileDescriptor: obtains the file descriptor of the opened connection for communication between the native layer and the USB device. mBusNum and mDevNum: the previously extracted bus number and device number. sb.toString(): the reconstructed device name for the native layer to identify. The "native layer" refers to the part written in native code (usually written in C or C++). By calling the nativeOpen() function, the connection information is passed to the native layer to open and operate the USB camera device in the native code.
[0115] Step 104: Collect the image data of each of the camera devices into the local environment according to the established connection relationship.
[0116] In one embodiment, the collecting the image data of each of the camera devices into the local environment according to the established connection relationship specifically includes: creating and initializing a first context; wherein, the communication requirements between the camera device and the mobile device are defined in the first context; obtaining a fourth camera device that has established a connection relationship with the mobile device and opening a connection to the fourth camera device; and obtaining the image data of the fourth camera device in real time based on a callback function.
[0117] In the embodiment of the present invention, the uvc_init2 function is used to initialize the USB camera context. The code implementation process is as follows:
[0118] uvc_context_t *mContext = nullptr;
[0119] uvc_error_t result = uvc_init2(&mContext, nullptr, usbfs);
[0120] Among them, this function creates a context for managing USB camera devices and sets the communication between the mobile device and the USB device. &mContext: Output parameter, the returned context pointer. nullptr: Optional device allocator (usually nullptr). usbfs: Specified USB file system path, usually / dev / bus / usb.
[0121] Then, use the uvc_get_device_with_fd function to obtain USB camera information, and its code implementation process is as follows:
[0122] uvc_device_t *mDevice = nullptr;
[0123] uvc_get_device_with_fd(mContext, &mDevice, venderId, productId, nullptr, fd, bus_num, dev_addr);
[0124] Among them, mContext is the context initialized before; &mDevice is the output parameter, the returned device pointer. venderId and productId represent the vendor and product ID of the camera; nullptr: Optional device string (usually nullptr); fd represents the file descriptor of the USB device; bus_num and dev_addr represent the USB bus number and device address. In the embodiment of the present invention, the information of the fourth camera device is obtained according to the file descriptor and other identification information.
[0125] Furthermore, use the uvc_open function to establish a connection with the fourth camera device, and its code implementation process is as follows:
[0126] uvc_device_handle_t *mDeviceHandle = nullptr;
[0127] uvc_open(mDevice, &mDeviceHandle);
[0128] Among them, mDevice represents the pointer of the device to be opened; &mDeviceHandle is the output parameter, returning the device handle. In the embodiment of the present invention, the connection with the fourth camera device is opened through the uvc_open function and the device handle is obtained.
[0129] Furthermore, the uvc_get_stream_ctrl_format_size function is used to set the image information (image width and height, image format, frame rate) of the USB camera. The code implementation process is as follows:
[0130] uvc_stream_ctrl_t ctrl;
[0131] uvc_get_stream_ctrl_format_size(mDeviceHandle, &ctrl,
[0132] UVC_FRAME_FORMAT_H264, 1920, 1080, 30);
[0133] Among them, mDeviceHandle represents the opened device handle; &ctrl is the output parameter, returning the stream control structure; UVC_FRAME_FORMAT_H264 represents the requested image format, which is H.264 here; 1920 and 1080 are the requested image width and height; 30 is the requested frame rate.
[0134] Finally, the uvc_start_streaming_bandwidt function is used to set the image data callback function and start the acquisition of image data. The callback function will continuously receive image data, and after receiving the image data, the image data can be used for operations such as live broadcast. The code implementation process is as follows:
[0135] uvc_start_streaming_bandwidth(mDeviceHandle,
[0136] &ctrl, preview_frame_callback, this, 1.0f, 0);
[0137] Among them, mDeviceHandle represents the opened device handle; &ctrl represents the structure containing flow control information; preview_frame_callback represents the callback function for receiving image data; this represents the context of the callback function (usually a pointer to a class instance); 1.0f is the flow bandwidth, usually representing the bandwidth requirement of the video stream; 0 represents some additional parameters or options (such as frame loss strategy, etc.). Based on the above callback function, the embodiments of the present invention can collect the image data of the camera device in real time.
[0138] Preferably, when it is necessary to end the acquisition, the uvc_stop_streaming, uvc_close, uvc_unref_device, and uvc_exit functions can be called to end the acquisition and close the resources. The code implementation process is as follows:
[0139] uvc_stop_streaming(mDeviceHandle);
[0140] uvc_close(mDeviceHandle);
[0141] uvc_unref_device(mDevice);
[0142] uvc_exit(mContext);
[0143] Among them, uvc_stop_streaming means to stop receiving the image data stream from the USB camera; mDeviceHandle: the previously opened device handle, used to specify the device stream to be stopped; uvc_close means to close the connection with the USB camera and release the device handle, mDeviceHandle is the previously opened device handle, used to specify the device connection to be closed; uvc_unref_device is used to release the previously connected camera device; mDevice represents the pointer to the previously obtained camera device, used to release its related resources; uvc_exit means to exit and release the context of the USB camera device, mContext is the previously initialized camera context, that is, the first context, used to release all the resources and memory it manages.
[0144] In the embodiments of the present invention, a USB camera information acquisition device is further provided, including a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor. When the processor executes the computer program, the above-mentioned USB camera information acquisition method is implemented.
[0145] In an embodiment of the present invention, a computer-readable storage medium is further provided. The computer-readable storage medium includes a stored computer program. When the computer program runs, it controls the device where the computer-readable storage medium is located to execute the above USB camera information acquisition method.
[0146] Exemplarily, the computer program may be divided into one or more modules. One or more modules are stored in the memory and executed by the processor to complete the present invention. One or more modules may be a series of computer program instruction segments capable of performing specific functions, and these instruction segments are used to describe the execution process of the computer program in the USB camera information acquisition device.
[0147] The USB camera information acquisition device may be a computing device such as a desktop computer, a notebook, a palm computer, and a cloud server. The USB camera information acquisition device may include, but is not limited to, a processor, a memory, and a display. Those skilled in the art can understand that the above components are only examples of the USB camera information acquisition device and do not constitute a limitation on the USB camera information acquisition device. It may include more or fewer components than those described, or combine certain components, or different components. For example, the USB camera information acquisition device may further include input / output devices, network access devices, a bus, etc.
[0148] The so-called processor may be a central processing unit (CPU), or may also be other general-purpose processors, digital signal processors (DSPs), application specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc. The processor is the control center of the USB camera information acquisition device and connects various parts of the entire USB camera information acquisition device through various interfaces and lines.
[0149] The memory can be used to store computer programs and / or modules. By running or executing the computer programs and / or modules stored in the memory and calling the data stored in the memory, the processor can implement various functions of the USB camera information acquisition device. The memory mainly includes a program storage area and a data storage area. Among them, the program storage area can store an operating system, application programs required for at least one function (such as a sound playback function, a text conversion function, etc.); the data storage area can store data created according to the use of the mobile phone (such as audio data, text message data, etc.). In addition, the memory can include high-speed random access memory and can also include non-volatile memory, such as a hard disk, memory, plug-in hard disk, Smart Media Card (SMC), Secure Digital (SD) card, FlashCard, at least one magnetic disk storage device, flash device, or other volatile solid-state storage devices.
[0150] Among them, if the module for USB camera information acquisition is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on such an understanding, to implement all or part of the processes in the above-mentioned embodiment methods of the present invention, it can also be completed by instructing relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium. When the computer program is executed by the processor, the steps of the above-mentioned various method embodiments can be implemented. Among them, the computer program includes computer program code, and the computer program code can be in the form of source code, object code, executable file, or some intermediate form, etc. The computer-readable medium can include: any entity or device capable of carrying computer program code, recording medium, USB flash drive, mobile hard disk, magnetic disk, optical disc, computer memory, read-only memory (ROM), random access memory (RAM), electrical carrier signal, telecommunication signal, and software distribution medium, etc. Those of ordinary skill in the art can understand and implement it without creative efforts.
[0151] An embodiment of the present invention provides a method for collecting USB camera information. By sending a request to open the access permission to the camera device, it ensures that users can flexibly control the usage permission of the camera according to their needs. It improves security and at the same time supports users to make settings in different scenarios. Based on the connection information, a connection relationship between the local environment and the camera is established, which can achieve faster and more stable data transmission, reduce the complexity of the intermediate layer, and improve the data processing efficiency. Through the established connection relationship, the image data of the camera can be collected in real time to meet the user's requirements for real-time processing and display. This is particularly important for application scenarios such as live streaming and monitoring. And this method directly communicates with the camera device and does not rely on the V4L2 interface solution, simplifies the development process, developers can more easily achieve cross-device compatibility, reduces the complex adaptation of the underlying driver, and users can more conveniently access the USB camera when using mobile devices, meeting various needs such as daily video collection and live streaming, and improving the overall user experience.
[0152] Embodiment 2
[0153] See Figure 2 , Figure 2 which is a schematic diagram of the modules of a USB camera information collection device provided in an embodiment of the present invention. An embodiment of the present invention provides a USB camera information collection device, including: a device acquisition module 201, a permission opening module 202, a connection establishment module 203, and a data collection module 204;
[0154] The device acquisition module 201 is used to acquire the camera device connected to the mobile device;
[0155] The permission opening module 202 is used to send a request to open the access permission to the camera device so that the camera device opens the access permission to the mobile device;
[0156] The connection establishment module 203 is used to acquire the connection information of each first camera device and establish a connection relationship between the local environment and each of the first camera devices based on the connection information; wherein, the first camera device is the camera device that opens the access permission to the mobile device;
[0157] The data collection module 204 is used to collect the image data of each camera device into the local environment according to the established connection relationship.
[0158] In one embodiment, the device acquisition module 201 is used to acquire the camera devices connected to the mobile device, specifically including: obtaining the list of USB devices connected to the mobile device based on the UsbManager instance, screening the USB devices in the USB device list according to a preset extension function, and obtaining the USB camera devices connected to the mobile device.
[0159] In one embodiment, the permission enabling module 202 is used to send an access permission opening request to the camera device so that the camera device opens the access permission to the mobile device, specifically including: obtaining the access permission status of each camera device relative to the mobile device; when it is detected that there is a second camera device that has not opened the access permission to the mobile device, calling the requestPermission method to send an access permission opening request to the second camera device; where the requestPermission method is to request the operating system of the mobile device for the access permission to the second camera device.
[0160] In one embodiment, before obtaining the connection information of each camera device, it further includes real-time monitoring of the device status of the camera device, specifically: registering a broadcast receiver in the operating system of the mobile device and adding a broadcast listening action in the broadcast receiver; where the broadcast listening action includes the insertion, removal, and access permission opening request result of the camera device; monitoring the access permission opening request result and the device plugging and unplugging status of the camera device relative to the mobile device based on the broadcast receiver.
[0161] In one embodiment, the connection establishment module 203 is used to obtain the connection information of each first camera device and establish a connection relationship between the local environment and each first camera device based on the connection information, specifically including: opening a third camera device based on the openDevice method and obtaining the connection information of the third camera device; where the openDevice method is used to open the connection between the mobile device and the specified third camera device; transmitting the connection information to the local environment so that the local environment establishes a connection with the third camera device; where the connection information includes: camera device name, product ID, vendor ID, and file descriptor.
[0162] In one embodiment, the data acquisition module 204 is configured to collect the image data of each camera device into the local environment according to the established connection relationship, specifically including: creating and initializing a first context; wherein, the communication requirements between the camera device and the mobile device are defined in the first context; obtaining a fourth camera device that has established a connection relationship with the mobile device and opening the connection with the fourth camera device; and obtaining the image data of the fourth camera device in real time based on a callback function.
[0163] Those skilled in the art can clearly understand that for the convenience and brevity of description, the specific working process of the above-described device can refer to the corresponding process in the foregoing method embodiment, and will not be elaborated herein.
[0164] The embodiment of the present invention provides a USB camera information acquisition device. By sending an access permission opening request to the camera device, it ensures that the user can flexibly control the usage permission of the camera according to needs. It improves security and at the same time supports the user to make settings in different scenarios. Based on the connection information, a connection relationship between the local environment and the camera is established, which can achieve faster and more stable data transmission, reduce the complexity of the middle layer, and improve the data processing efficiency. Through the established connection relationship, the image data of the camera can be collected in real time to meet the user's requirements for real-time processing and display. This is particularly important for application scenarios such as live broadcast and monitoring. And this method directly communicates with the camera device, does not rely on the V4L2 interface solution, simplifies the development process, the developer can more easily achieve cross-device compatibility, reduces the complex adaptation of the underlying driver, and the user can more conveniently access the USB camera when using the mobile device, meeting various needs such as daily video acquisition and live broadcast, and improving the overall user experience.
[0165] The above is only the preferred embodiment of the present invention. It should be noted that for those of ordinary skill in the art, without departing from the technical principle of the present invention, several improvements and replacements can still be made, and these improvements and replacements should also be regarded as the protection scope of the present invention.
Claims
1. A method for collecting USB camera information, characterized in that, Comprising: Obtain the camera devices connected to the mobile device; Send an access permission opening request to the camera device to enable the camera device to open the access permission to the mobile device; Obtain the connection information of each first camera device and establish a connection relationship between the local environment and each of the first camera devices based on the connection information; wherein, the first camera device is the camera device that has opened the access permission to the mobile device; Collect the image data of each camera device into the local environment according to the established connection relationship; specifically, open the third camera device based on the openDevice method and obtain the connection information of the third camera device; wherein, the openDevice method is used to open the connection between the mobile device and the specified third camera device; Transmit the connection information to the local environment so that the local environment establishes a connection with the third camera device; wherein, the connection information includes: camera device name, product ID, vendor ID, and file descriptor; The obtaining of the camera devices connected to the mobile device specifically includes: Obtain the list of USB devices connected to the mobile device based on the UsbManager instance, Filter the USB devices in the USB device list according to a preset extension function to obtain the USB camera devices connected to the mobile device.
2. The method for collecting USB camera information according to claim 1, wherein The sending of the access permission opening request to the camera device to enable the camera device to open the access permission to the mobile device specifically includes: Obtain the access permission status of each camera device relative to the mobile device; When it is detected that there is a second camera device that has not opened the access permission to the mobile device, call the requestPermission method to send an access permission opening request to the second camera device; wherein, the requestPermission method is to request the access permission to the second camera device from the operating system of the mobile device.
3. The method for collecting USB camera information according to claim 1, wherein, Before obtaining the connection information of each camera device, it further includes real-time monitoring of the device status of the camera device, specifically: Register a broadcast receiver in the operating system of the mobile device and add a broadcast listening action in the broadcast receiver; wherein, the broadcast listening action includes the insertion, removal, and access permission opening request result of the camera device; Monitor the access permission opening request result and the device plugging and unplugging status of the camera device relative to the mobile device based on the broadcast receiver.
4. The method for collecting USB camera information according to claim 1, characterized in that, The collecting of the image data of each camera device into the local environment according to the established connection relationship specifically includes: Create and initialize a first context; wherein, the communication requirements between the camera device and the mobile device are defined in the first context; Obtain the fourth camera device that has established a connection relationship with the mobile device and open the connection with the fourth camera device; Obtain the image data of the fourth camera device in real time based on the callback function.
5. A USB camera information acquisition device, characterized in that, Comprising: Device acquisition module, permission opening module, connection establishment module, and data acquisition module; The device acquisition module is used to acquire the camera devices connected to the mobile device; The permission opening module is used to send an access permission opening request to the camera device so that the camera device opens the access permission to the mobile device; The connection establishment module is used to acquire the connection information of each first camera device and establish a connection relationship between the local environment and each of the first camera devices based on the connection information; wherein, the first camera device is the camera device that opens the access permission to the mobile device; The data acquisition module is used to collect the image data of each camera device into the local environment according to the established connection relationship; specifically, open the third camera device based on the openDevice method and acquire the connection information of the third camera device; wherein, the openDevice method is used to open the connection between the mobile device and the specified third camera device; Transmit the connection information to the local environment so that the local environment establishes a connection with the third camera device; wherein, the connection information includes: camera device name, product ID, vendor ID, and file descriptor; The device acquisition module is used to acquire the camera devices connected to the mobile device, specifically including: Obtain the list of USB devices connected to the mobile device based on the UsbManager instance, Filter the USB devices in the USB device list according to the preset extension function to obtain the USB camera devices connected to the mobile device.
6. A terminal device, characterized in that, It includes a processor, a memory, and a computer program stored in the memory and configured to be executed by the processor. When the processor executes the computer program, it implements the USB camera information acquisition method according to any one of claims 1 to 4.
7. A computer-readable storage medium, characterized in that, The computer-readable storage medium includes a stored computer program, wherein when the computer program runs, it controls the device where the computer-readable storage medium is located to execute the USB camera information acquisition method according to any one of claims 1 to 4.
Citation Information
Patent Citations
Camera access method and device with Linux compatible with Android
CN113448648A