Disclosure of Invention
In order to solve the problem that in a Linux Android-compatible environment, Android applications can use cameras (including USB cameras and built-in cameras) of a Linux system to complete operation of various functions of a front camera and a rear camera, the invention provides a Linux Android-compatible camera access method and device.
On one hand, the invention provides a method for accessing a camera by Linux compatible with Android, which is applied to an operation environment compatible with an Android system in Linux, and comprises the following steps:
step S1: creating a container on Linux, and mounting a camera on the Linux side into the container;
step S2: starting a container on Linux, and starting an Android system in the container;
step S3: starting a camera frame service at an Android side, polling and scanning equipment under a dev directory, and screening out a camera; the method comprises the steps that a camera framework service sends a notification message to an HAL layer on an Android side, and the HAL layer caches camera information;
step S4: the method comprises the steps that a camera framework service of an Android system virtualizes a single camera into a front camera and a rear camera, and the camera framework service informs information of the virtual front camera and the virtual rear camera to an HAL layer through an interface so that the camera of the Linux system is added to a front camera information list and a rear camera information list of the HAL layer;
step S5: and the Android application accesses the camera according to the front camera information list and the rear camera information list.
Further, the method starts a camera frame service on an Android side, polls equipment under a scanning/dev directory, and screens out the cameras, and specifically comprises the following steps:
starting a camera frame service at an Android side, polling and scanning equipment under a/dev directory, firstly judging whether equipment which is not processed by a system exists in the equipment under the/dev directory, if so, continuously judging whether the equipment which is not processed by the system supports a V42L specification, and if not, finishing scanning; if the device which is not processed by the system supports the V42L specification, the image capturing capability of the device is continuously judged, and if the device which is not processed by the system does not support the V42L specification, the device under the scanning/dev directory is continuously polled; when the image capturing capability of the equipment meets the system requirement, continuously judging whether the format of the data acquired by the equipment is supported by the system, and when the image capturing capability of the equipment does not meet the system requirement, continuously polling and scanning the equipment in the dev directory; if the format of the data collected by the equipment is supported by the system, caching the information of the camera, and if the format of the data collected by the equipment is not supported by the system, continuously polling the equipment under the scanning/dev directory until the scanning is finished.
Further, the camera framework service notifies the HAL layer of the information of the virtual front camera and the virtual rear camera through an interface, so that the cameras of the Linux system are added to the front camera information list and the rear camera information list of the HAL layer, specifically:
the camera framework service judges the virtual front camera information and the virtual rear camera information: if the virtual front camera information does not exist in the front camera information list, adding the virtual front camera information into the front camera information list, and if the virtual front camera information does not exist in the front camera information list, not adding the virtual front camera information; if the virtual rear camera information does not exist in the rear camera information list, adding the virtual rear camera information into the rear camera information list, and if the virtual rear camera information does not exist in the rear camera information list, not adding the virtual rear camera information; and the HAL layer informs the camera framework service of the updated front camera information list and the updated rear camera information list, and the camera framework service updates the camera information.
Further, the step S1 is creating a container on Linux, and mounting a Linux-side camera into the container, including: opening device rights to the container when the container is created; and mounting the cameras on the Linux side into the container through the control group.
Further, the step S5 accessing the camera by the Android application according to the front camera information list and the rear camera information list includes: when the Android application calls the camera, the camera frame service firstly judges whether the camera exists or not and whether the camera is occupied or not, if the camera exists and is not occupied, the HAL layer is informed, and the HAL layer opens the camera.
On the other hand, the invention provides a Linux-Android-compatible access camera device, which is applied to an operating environment compatible with an Android system on Linux, and comprises the following components:
a container creation unit to: creating a container on Linux, and mounting a camera on the Linux side into the container;
a container activation unit for: starting a container on Linux, and starting an Android system in the container;
a camera information cache processing unit configured to: starting a camera frame service at an Android side, polling and scanning equipment under a dev directory, and screening out a camera; the method comprises the steps that a camera framework service sends a notification message to an HAL layer on an Android side, and the HAL layer caches camera information;
a virtual camera unit to: the method comprises the steps that a camera framework service of an Android system virtualizes a single camera into a front camera and a rear camera, and the camera framework service informs information of the virtual front camera and the virtual rear camera to an HAL layer through an interface so that the camera of the Linux system is added to a front camera information list and a rear camera information list of the HAL layer;
an access camera unit to: and the Android application accesses the camera according to the front camera information list and the rear camera information list.
Further, the camera information cache processing unit is configured to:
starting a camera frame service at an Android side, polling and scanning equipment under a/dev directory, firstly judging whether equipment which is not processed by a system exists in the equipment under the/dev directory, if so, continuously judging whether the equipment which is not processed by the system supports a V42L specification, and if not, finishing scanning; if the device which is not processed by the system supports the V42L specification, the image capturing capability of the device is continuously judged, and if the device which is not processed by the system does not support the V42L specification, the device under the scanning/dev directory is continuously polled; when the image capturing capability of the equipment meets the system requirement, continuously judging whether the format of the data acquired by the equipment is supported by the system, and when the image capturing capability of the equipment does not meet the system requirement, continuously polling and scanning the equipment in the dev directory; if the format of the data collected by the equipment is supported by the system, caching the information of the camera, and if the format of the data collected by the equipment is not supported by the system, continuously polling the equipment under the scanning/dev directory until the scanning is finished.
Further, the virtual camera unit is to:
the camera framework service judges the virtual front camera information and the virtual rear camera information: if the virtual front camera information does not exist in the front camera information list, adding the virtual front camera information into the front camera information list, and if the virtual front camera information does not exist in the front camera information list, not adding the virtual front camera information; if the virtual rear camera information does not exist in the rear camera information list, adding the virtual rear camera information into the rear camera information list, and if the virtual rear camera information does not exist in the rear camera information list, not adding the virtual rear camera information; and the HAL layer informs the camera framework service of the updated front camera information list and the updated rear camera information list, and the camera framework service updates the camera information.
Further, the container creating unit is configured to: opening device rights to the container when the container is created; and mounting the cameras on the Linux side into the container through the control group.
Further, the access camera unit is to: when the Android application calls the camera, the camera frame service firstly judges whether the camera exists or not and whether the camera is occupied or not, if the camera exists and is not occupied, the HAL layer is informed, and the HAL layer opens the camera.
Compared with the prior art, the invention has the following advantages:
at present, most applications can use the camera to realize various functions, and the technical scheme of the invention solves the problem that no camera is available in the application of the Android system because no camera is arranged in the front and back of the Linux operating system compatible with the Android, so that the invention plays an immeasurable role in the ecological expansion of the Linux operating system. Meanwhile, the USB camera is detachable and replaceable, so that a selection space suitable for the camera is provided for a client.
Detailed Description
In order to make the objects, technical solutions and advantages of the present invention more apparent, the present invention is described in further detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention.
For the operating environment compatible with Android running on the Linux operating system, the Android operating environment runs in a container.
Containers are a lightweight virtualization technology supported by the Linux kernel in recent years. A new Linux system (including other systems customized based on the Linux system, such as Android) can be operated in the Linux system through a container technology, and a client system and a host system are isolated from each other through the container. Different from the traditional scheme based on the virtual machine technology, in the container technology, the client system and the host system share resources such as a CPU (central processing unit), a memory, storage and the like, and the resource overhead is low. In addition, through proper configuration, the client system can directly access the hardware resource of the host, and the performance of the client system is further improved.
Due to the isolation effect of the container docker, Linux cannot access the internal storage directory of Android, and Android applications on Linux cannot directly access Linux-side files.
Fig. 1 is a schematic diagram of a method for accessing a camera with Linux compatible with Android, which is provided by an embodiment of the present invention, and is applied to an operation environment with Linux compatible with Android system, where the method includes the following steps:
step S1: creating a container on Linux, and mounting a camera on the Linux side into the container;
opening part of equipment access rights simultaneously when an Android environment container is created;
in an Android environment compatible with a Linux system, all hardware devices used in the Android environment are derived from the Linux system, and when the Android environment is restarted or started for the first time, the access right of the existing devices in the Linux system needs to be opened to the Android environment. For compatibly running the Android running environment on the Linux operating system, the Android running environment runs in a container, so that the HAL layer hardware abstraction layer of the Android has no right to directly access Linux system hardware equipment. The method can create the container through the existing Linux command, and directly opens the device authority to the container when the container is created, so that the Android environment has the capability of accessing the device authority.
The device files under the/dev directory of the Linux system are used to represent the peripherals, e.g./dev/video 0 represents the first media device (camera). However, this/dev/video 0 is only convenient for the user to observe, and the representation of different devices in the l inux kernel is realized by a major and a minor Number, and corresponding drivers are loaded through the major and the minor Number.
major number: indicating different device types
minor number: representing different partitions of the same device
For example:
the major numbers of the video0, the video1, the video2 and the video3 are all 81, the minor number of the video is 0, and 1 is added for each new camera minor number. It can be seen that we can represent cameras by major number 81. When the docker is used for creating the Android container, 81 device access rights can be opened to the Android container.
docker is a container technology that effectively partitions resources of a single operating system into isolated groups to better balance conflicting resource usage requirements among the isolated groups. In the Linux-compatible Android environment, the Android system runs in a docker container of a Linux end.
The container creation instruction (docker create) can create a docker container by docker mirroring, a — device-group-rule of docker create 'c 81: × rmw' parameter can mount a device group of which cgroup (control group) is 81 (camera device) into the docker container, and set the authority of the device group of which cgroup (control group) is 81 (camera device) so that the Android system running in the docker container has the authority to open and operate the device group of which Linux-side cgroup (control group) is 81 (camera device).
Cgroup (control groups) is a mechanism provided by the Linux kernel that can limit, record, and isolate the physical resources used by a process group. After a USB camera is connected to the Linux system, an equipment node with Cgroup of 81 (generally, dev/video0 or dev/video1, the name is not fixed) is generated. Therefore, when the container is created, the camera device at the Linux end can be mounted in the docker container through the parameter of-device-group-rule: 'c 81: × rmw'.
Step S2: starting a container on Linux, and starting an Android system in the container;
step S3: starting a camera frame service at an Android side, polling and scanning equipment under a dev directory, and screening out a camera; the method comprises the steps that a camera framework service sends a notification message to an HAL layer on an Android side, and the HAL layer caches camera information;
in the foregoing steps, when an Android container is created, access permissions of multiple devices in Linux are already openly shared to an Android environment, and we need to screen out camera devices from the devices and notify the camera devices to a HAL layer for caching for App to call, and meanwhile, a data format collected by the devices must be one of data formats allowed by a system.
The V4L2 is a set of video framework specially designed for Linux equipment, the main framework of the framework is an L inux kernel, and the data format collected by the equipment and the equipment for detection are based on the V4L2 command. The command of V4L2 will determine whether the current device and driver support the V4L2 specification, obtain the capability set of the device, and obtain the format supported by the device.
Step S4: the method comprises the steps that a camera framework service of an Android system virtualizes a single camera into a front camera and a rear camera, and the camera framework service informs information of the virtual front camera and the virtual rear camera to an HAL layer through an interface so that the camera of the Linux system is added to a front camera information list and a rear camera information list of the HAL layer;
in the Android system, two places are provided for caching the camera information of the system: android system camera framework, camera HAL layer. When the App calls the camera, the system camera frame firstly judges whether the camera exists or not and is occupied or not, if the camera which is not occupied exists, the camera is transferred to the HAL layer, and the HAL layer is requested to open the camera.
The Linux camera information is cached in the camera framework service in the previous steps, but the camera cannot be directly called by the App at the moment, because the Android system is basically oriented to the mobile device at present and only supports the operation of the camera through the ID 1 (front camera) and the ID 0 (rear camera), and there is no method for calling the camera with unknown ID at present. In actual operation, whether the App opens the front camera or the rear camera is determined by own business of the App, the PC is provided with built-in cameras possibly, and USB cameras with unknown numbers can be inserted.
Step S5: and the Android application accesses the camera according to the front camera information list and the rear camera information list.
Further, the method starts a camera frame service on an Android side, polls equipment under a scanning/dev directory, and screens out the cameras, and specifically comprises the following steps:
starting a camera frame service at an Android side, polling and scanning equipment under a/dev directory, firstly judging whether equipment which is not processed by a system exists in the equipment under the/dev directory, if so, continuously judging whether the equipment which is not processed by the system supports a V42L specification, and if not, finishing scanning; if the device which is not processed by the system supports the V42L specification, the image capturing capability of the device is continuously judged, and if the device which is not processed by the system does not support the V42L specification, the device under the scanning/dev directory is continuously polled; when the image capturing capability of the equipment meets the system requirement, continuously judging whether the format of the data acquired by the equipment is supported by the system, and when the image capturing capability of the equipment does not meet the system requirement, continuously polling and scanning the equipment in the dev directory; if the format of the data collected by the equipment is supported by the system, caching the information of the camera, and if the format of the data collected by the equipment is not supported by the system, continuously polling the equipment under the scanning/dev directory until the scanning is finished.
Further, the camera framework service notifies the HAL layer of the information of the virtual front camera and the virtual rear camera through an interface, so that the cameras of the Linux system are added to the front camera information list and the rear camera information list of the HAL layer, specifically:
the camera framework service judges the virtual front camera information and the virtual rear camera information: if the virtual front camera information does not exist in the front camera information list, adding the virtual front camera information into the front camera information list, and if the virtual front camera information does not exist in the front camera information list, not adding the virtual front camera information; if the virtual rear camera information does not exist in the rear camera information list, adding the virtual rear camera information into the rear camera information list, and if the virtual rear camera information does not exist in the rear camera information list, not adding the virtual rear camera information; and the HAL layer informs the camera framework service of the updated front camera information list and the updated rear camera information list, and the camera framework service updates the camera information.
Further, the step S1 is creating a container on Linux, and mounting a Linux-side camera into the container, including: opening device rights to the container when the container is created; and mounting the cameras on the Linux side into the container through the control group.
Further, the step S5 accessing the camera by the Android application according to the front camera information list and the rear camera information list includes: when the Android application calls the camera, the camera frame service firstly judges whether the camera exists or not and whether the camera is occupied or not, if the camera exists and is not occupied, the HAL layer is informed, and the HAL layer opens the camera.
Fig. 2 is a schematic view of a Linux-Android-compatible access camera device provided in an embodiment of the present invention, where the Linux-Android-compatible access camera device is applied in an operating environment where an Android system is compatibly operated on Linux, and the Linux-Android-compatible access camera device includes:
a container creation unit to: creating a container on Linux, and mounting a camera on the Linux side into the container;
a container activation unit for: starting a container on Linux, and starting an Android system in the container;
a camera information cache processing unit configured to: starting a camera frame service at an Android side, polling and scanning equipment under a dev directory, and screening out a camera; the method comprises the steps that a camera framework service sends a notification message to an HAL layer on an Android side, and the HAL layer caches camera information;
a virtual camera unit to: the method comprises the steps that a camera framework service of an Android system virtualizes a single camera into a front camera and a rear camera, and the camera framework service informs information of the virtual front camera and the virtual rear camera to an HAL layer through an interface so that the camera of the Linux system is added to a front camera information list and a rear camera information list of the HAL layer;
an access camera unit to: and the Android application accesses the camera according to the front camera information list and the rear camera information list.
Further, the camera information cache processing unit is configured to:
starting a camera frame service at an Android side, polling and scanning equipment under a/dev directory, firstly judging whether equipment which is not processed by a system exists in the equipment under the/dev directory, if so, continuously judging whether the equipment which is not processed by the system supports a V42L specification, and if not, finishing scanning; if the device which is not processed by the system supports the V42L specification, the image capturing capability of the device is continuously judged, and if the device which is not processed by the system does not support the V42L specification, the device under the scanning/dev directory is continuously polled; when the image capturing capability of the equipment meets the system requirement, continuously judging whether the format of the data acquired by the equipment is supported by the system, and when the image capturing capability of the equipment does not meet the system requirement, continuously polling and scanning the equipment in the dev directory; if the format of the data collected by the equipment is supported by the system, caching the information of the camera, and if the format of the data collected by the equipment is not supported by the system, continuously polling the equipment under the scanning/dev directory until the scanning is finished.
Further, the virtual camera unit is to:
the camera framework service judges the virtual front camera information and the virtual rear camera information: if the virtual front camera information does not exist in the front camera information list, adding the virtual front camera information into the front camera information list, and if the virtual front camera information does not exist in the front camera information list, not adding the virtual front camera information; if the virtual rear camera information does not exist in the rear camera information list, adding the virtual rear camera information into the rear camera information list, and if the virtual rear camera information does not exist in the rear camera information list, not adding the virtual rear camera information; and the HAL layer informs the camera framework service of the updated front camera information list and the updated rear camera information list, and the camera framework service updates the camera information.
Further, the container creating unit is configured to: opening device rights to the container when the container is created; and mounting the cameras on the Linux side into the container through the control group.
Further, the access camera unit is to: when the Android application calls the camera, the camera frame service firstly judges whether the camera exists or not and whether the camera is occupied or not, if the camera exists and is not occupied, the HAL layer is informed, and the HAL layer opens the camera.
Fig. 3 is a schematic diagram of a network architecture of a Linux-compatible Android virtual front camera and a Linux-compatible Android virtual rear camera provided in an embodiment of the present invention. In the figure, each camera (including a built-in camera and a USB camera) on the Linux system is added to a front camera information list and a rear camera information list, that is, the Linux camera is virtualized into a camera that can be supported by the Android system, and the Android application can be selected from the lists when the front camera or the rear camera is opened, so that multiple options are available.
Finally, it should be noted that: although the present invention has been described in detail with reference to the foregoing embodiments, it will be apparent to those skilled in the art that changes may be made in the embodiments and/or equivalents thereof without departing from the spirit and scope of the invention. Any modification, equivalent replacement, or improvement made within the spirit and principle of the present invention should be included in the protection scope of the present invention.