Camera control method, device, electronic device and storage medium in application
The method of using shared memory and mutex locks in a front-end/back-end application architecture for UVC cameras addresses handle leaks, preventing crashes by managing camera operations and handle usage, ensuring stable operation in UVC camera applications.
Patent Information
- Application Number
- CN202111382034.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-11-22
- Publication Date
- 2025-03-28
- Estimated Expiration
- 2041-11-22
AI Technical Summary
UVC cameras in USB video capture devices frequently opening and closing lead to handle leaks, causing application crashes in scenarios like facial recognition systems, resulting in process handle accumulation and eventual application failure.
Implementing a method where a front-end application controls a back-end application to manage camera operations, including creating shared memory objects and mutex locks to handle camera operations and monitor handle usage, with the front-end application sending commands to the back-end to close it when handle thresholds are exceeded.
Effectively prevents handle leaks and avoids application crashes by managing camera operations through shared memory and mutex locks, ensuring stable operation even with frequent camera openings and closings.
Smart Images

Figure CN114064247B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a camera control method, device, electronic device and storage medium in an application. Background Art
[0002] UVC camera is a camera under the protocol standard defined by USB video capture device, which is widely used in the field of face recognition. For example, in scenes such as unmanned vending machines and import and export gates, when the application applied to the scene senses that someone is approaching, it turns on the UVC camera, completes face recognition, and then turns off the camera. In these scenes, the UVC camera may be frequently turned on and off, which will cause the number of process handles of the application to continue to increase, that is, handle leakage, and eventually cause the application to crash.
[0003] Therefore, there is an urgent need to provide a method to prevent handle leakage and ensure the normal operation of the application. Summary of the invention
[0004] The purpose of the present application is to provide a camera control method, device, electronic device and storage medium in an application program to address the deficiencies in the above-mentioned prior art, so as to solve the problem of application program crash caused by handle leakage.
[0005] To achieve the above purpose, the technical solution adopted in the embodiment of the present application is as follows:
[0006] In a first aspect, an embodiment of the present application provides a camera control method in an application, which is applied to an electronic device, wherein the electronic device includes: a foreground application and a background application, wherein the foreground application includes a graphical user interface, and the background application runs in the background of the electronic device; the method includes: the foreground application responds to a user's operation of turning on and off the camera, and sends an operation instruction to the background application, wherein the operation instruction is used to instruct to turn on and off the camera;
[0007] The background application executes the operation indicated by the operation instruction according to the operation instruction;
[0008] The foreground application obtains the number of handles occupied by the background application;
[0009] If the number of handles occupied by the background application is greater than or equal to a preset threshold, the foreground application sends a close instruction to the background application, where the close instruction is used to instruct to close the background application.
[0010] Optionally, the foreground application responds to the user's operation of turning on and off the camera, and before sending the operation instruction to the background application, further includes:
[0011] The foreground application creates an image shared memory object and a command shared memory object, wherein the image shared memory object is used to identify a target image shared memory, and the target image shared memory is used to store images captured by the camera; the command shared memory object identifies a target command shared memory, and the target command shared memory is used to store commands issued by the foreground application and response information issued by the background application;
[0012] The foreground application creates an image mutex and a command mutex, wherein the image mutex is used to lock the target image shared memory, and the command mutex is used to lock the target command shared memory;
[0013] The foreground application uses the character string names used by the shared image memory object and the mutex lock object as startup parameters to start the background application.
[0014] Optionally, the foreground application sends an operation instruction to the background application in response to the user's operation of turning on and off the camera, including:
[0015] The foreground application generates the operation instruction in response to the user's operation of turning on and off the camera;
[0016] The foreground application saves the operation instruction to the target command shared memory based on the command mutex lock.
[0017] Optionally, the background application performs the operation indicated by the operation instruction according to the operation instruction, including:
[0018] The background application reads and executes the operation instructions in the target command shared memory in real time based on the command mutex lock to capture images;
[0019] The background application saves the captured image into the target image shared memory based on the image mutex lock, and the foreground application reads the captured image from the target image shared memory.
[0020] Optionally, after the foreground application sends a closing instruction to the background application, the method further includes:
[0021] The foreground application releases the image shared memory object, the command shared memory object, the image mutex lock, and the command mutex lock.
[0022] Optionally, after the foreground application sends the closing instruction to the background application, the process further includes:
[0023] The foreground application recreates the image shared memory object, the command shared memory object, the image mutex lock, and the command mutex lock, and starts the background application.
[0024] Optionally, after the foreground application sends a closing instruction to the background application, the method further includes:
[0025] The foreground application determines whether the handle occupied by the background application is released;
[0026] If not, the foreground application program calls a preset instruction of the operating system to close the background application program after a preset time period.
[0027] In a second aspect, an embodiment of the present application further provides a camera control device in an application, the device comprising:
[0028] The foreground application is used to respond to the operation of turning on and off the camera and send an operation instruction to the background application, wherein the operation instruction is used to instruct to turn on and off the camera;
[0029] The background application is used to perform the operation indicated by the operation instruction according to the operation instruction;
[0030] The foreground application is also used to obtain the number of handles occupied by the background application; and when the number of handles occupied by the background application is greater than or equal to a preset threshold, a close instruction is sent to the background application, wherein the close instruction is used to instruct the background application to close.
[0031] Optionally, the foreground application is specifically used to create an image shared memory object and a command shared memory object, the image shared memory object is used to identify a target image shared memory, the target image shared memory is used to store images taken by the camera, the command shared memory object identifies a target command shared memory, the target command shared memory is used to store commands issued by the foreground application and response information issued by the background application;
[0032] The foreground application creates an image mutex and a command mutex, wherein the image mutex is used to lock the target image shared memory, and the command mutex is used to lock the target command shared memory;
[0033] The foreground application uses the character string names used by the shared image memory object and the mutex lock object as startup parameters to start the background application.
[0034] Optionally, the foreground application is specifically used to generate the operation instruction in response to the operation of turning on and off the camera;
[0035] The foreground application saves the operation instruction to the target command shared memory based on the command mutex lock.
[0036] Optionally, the background application is specifically used to read and execute the operation instructions in the target command shared memory in real time based on the command mutex lock to perform image capture;
[0037] The background application saves the captured image into the target image shared memory based on the image mutex lock, and the foreground application reads the captured image from the target image shared memory.
[0038] Optionally, the foreground application is specifically used to release the image shared memory object, the command shared memory object, the image mutex lock and the command mutex lock.
[0039] Optionally, the foreground application is specifically used to recreate the image shared memory object, the command shared memory object, the image mutex lock and the command mutex lock, and start the background application.
[0040] Optionally, the foreground application is specifically used to determine whether the handle occupied by the background application is released;
[0041] If not, the foreground application program calls a preset instruction of the operating system to close the background application program after a preset time period.
[0042] In a third aspect, an embodiment of the present application further provides an electronic device, comprising: a processor, a storage medium and a bus, wherein the storage medium stores program instructions executable by the processor, and when an application is running, the processor communicates with the storage medium via the bus, and the processor executes the program instructions to perform the steps of the camera control method in the application as described in the first aspect above.
[0043] In a fourth aspect, an embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is read and executed, the camera control method in the application provided in the first aspect is implemented.
[0044] The beneficial effects of this application are:
[0045] The present application provides a camera control method, device, electronic device and storage medium in an application. The foreground application responds to the user's operation of turning on and off the camera, sends an operation instruction to the background application, and the background application executes the operation instruction. The foreground application obtains the number of handles occupied by the background application. If the number of handles occupied by the background application is greater than or equal to a preset threshold, the foreground application sends a closing instruction to the background application. In this method, by starting an additional background application to operate the camera and closing the background application to release the number of occupied handles, the handle leakage problem of the camera during the opening and closing process can be effectively solved, and the program crash caused by handle leakage during the frequent opening and closing of the camera can be avoided. BRIEF DESCRIPTION OF THE DRAWINGS
[0046] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the embodiments will be briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present application and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying creative work.
[0047] Figure 1 A system architecture diagram of a camera control method in an application provided in an embodiment of the present application;
[0048] Figure 2 A schematic diagram of a flow chart of a camera control method in an application provided in an embodiment of the present application;
[0049] Figure 3 A schematic diagram of a flow chart of a camera control method in an application provided in an embodiment of the present application;
[0050] Figure 4 A flowchart of a camera control method in another application provided in an embodiment of the present application;
[0051] Figure 5 A flowchart of a camera control method in another application provided in an embodiment of the present application;
[0052] Figure 6 A complete flow chart of a camera control method in an application provided in an embodiment of the present application;
[0053] Figure 7 A structural block diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0054] In order to make the purpose, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be clearly and completely described below in combination with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments.
[0055] Figure 1 A system architecture diagram of a camera control method in an application provided in an embodiment of the present application, such as Figure 1 As shown, the method is applied to an electronic device, which may be a terminal device with computing and display capabilities, such as a desktop computer or a notebook computer. The electronic device includes: a foreground application and a background application.
[0056] The foreground application may be an executable application as a client, and may include a user interface, and the user interface may include functions of opening the camera, closing the camera, and an image preview box, so that the user can open the camera, close the camera, and perform image preview operations based on the foreground application. The background application may be an executable application as a server, and may run in the background to perform operations of opening the camera, closing the camera, and real-time image acquisition. The foreground application may not only respond to user operations but also control the background application, and the background application may respond to instructions sent by the foreground application. For example, the foreground application sends an operation instruction to the background application, and the background application receives the operation instruction and executes the operation instruction.
[0057] Figure 2 The present invention provides a flow chart of a camera control method in an application program, wherein the execution subject of the method is the electronic device mentioned above. Figure 2 As shown, the method may include:
[0058] S101. The foreground application responds to the user's operation of turning on or off a camera and sends an operation instruction to the background application, where the operation instruction is used to instruct to turn on or off the camera.
[0059] Exemplarily, a user sends an operation instruction to a foreground application, and the foreground application responds to the operation instruction sent by the user. When the foreground application receives the operation instruction, it starts the background application and sends the operation instruction to the background application.
[0060] The operation instruction may be, for example, turning on the camera, turning off the camera, capturing an image, etc. The instruction may include an operation code field, and one or more fields related to operands.
[0061] Exemplarily, the operation instruction for turning on the camera may be, for example, 1, the operation instruction for turning off the camera may be, for example, 2, and the operation instruction for capturing an image may be, for example, 3.
[0062] S102: The background program executes the operation indicated by the operation instruction according to the operation instruction.
[0063] Exemplarily, after the background application receives the operation instruction sent by the foreground application, it parses the operation instruction. For example, if the operation instruction received by the background application is 1, it is parsed through 1 to find that the operation instruction is to turn on the camera, and the background application executes the operation to turn on the camera, so that the camera is turned on accordingly.
[0064] S103: The foreground application obtains the number of handles occupied by the background application.
[0065] Exemplarily, after the background application performs a complete operation of opening and closing the camera, the number of process handles of the background application increases by a value accordingly. For example, if the current number of handles of the background application is A, then when the background application performs an operation of opening and closing the camera according to the operation instruction of the foreground application, the number of handles of the background application becomes A+1, where A is an integer greater than 0.
[0066] Correspondingly, after sending the above operation instruction to the background application, the foreground application can obtain the number of handles currently occupied by the background application in real time.
[0067] S104: If the number of handles occupied by the background application is greater than or equal to a preset threshold, the foreground application sends a close instruction to the background application, where the close instruction is used to close the background application.
[0068] Exemplarily, the foreground application obtains the number of handles of the background application. If the foreground application determines that the number of handles occupied by the background application is greater than or equal to a preset threshold, it indicates that the camera may crash. Therefore, the foreground application sends an instruction to close the background application to the background application. After receiving the closing instruction, the background application executes the operation of closing the background application. At the same time, after the background application is closed, the number of handles occupied by the background application is released by the operating system, thereby avoiding the camera crash.
[0069] Furthermore, after the background application is closed, if the foreground application has not exited, the foreground application restarts the background application; if the foreground application has exited, the operation ends.
[0070] Exemplarily, the preset threshold may be 2000.
[0071] In summary, in this embodiment, the foreground application responds to the user's operation of turning on and off the camera, sends an operation instruction to the background application, and the background application executes the operation instruction. The foreground application obtains the number of handles occupied by the background application. If the number of handles occupied by the background application is greater than or equal to a preset threshold, the foreground application sends a closing instruction to the background application. In this method, by starting an additional background application to operate the camera and closing the background application to release the number of occupied handles, the handle leakage problem during the camera opening and closing process can be effectively solved, and the program crash caused by handle leakage during the frequent opening and closing of the camera can be avoided.
[0072] Figure 3 A flowchart of another camera control method in an application provided in an embodiment of the present application, optionally, as follows: Figure 3 As shown, in the above step S101, the foreground application responds to the user's operation of turning on and off the camera, and before sending the operation instruction to the background application, it may include:
[0073] S201. The foreground application creates an image shared memory object and a command shared memory object.
[0074] The above-mentioned image shared memory object is used to identify the target image shared memory, which is used to store the images taken by the camera. The above-mentioned command shared memory object is used to identify the target command shared memory, which is used to store the commands issued by the foreground program and the response information issued by the background application.
[0075] In one example, a foreground application can create an image shared memory, generate a string name based on the current timestamp, which can be named "SharingMemoryFrame", and then use the CreateFileMapping function and pass in the "SharingMemoryFrame" parameter to create a file mapping kernel object, and then use the MapViewOfFile function to map the file data to the process address space of the foreground application, and the image shared memory is created successfully.
[0076] Optionally, the foreground application can also create a command shared memory, generate a string name based on the current timestamp, which can be named "SharingMemoryCmd", and then use the CreateFileMapping function and pass in the "SharingMemoryCmd" parameter to create a file mapping kernel object, and then use the MapViewOfFile function to map the file data to the process address space of the foreground application, and the command shared memory is created successfully.
[0077] S202: The foreground application creates an image mutex and a command mutex. The image mutex is used to lock a target image shared memory, and the command mutex is used to lock the target command shared memory.
[0078] In one example, the foreground application first creates a mutex lock for protecting the shared memory of image data. The mutex lock can be a variable, which can be named "SharingMemoryFrame_Mutex", for example. Then, the CreateMutex function is used and "SharingMemoryFrame_Mutex" is passed in to create the image mutex lock.
[0079] Optionally, the foreground application may also create a mutex lock for protecting command shared memory. The mutex lock may be a variable, which may be named "SharingMemoryCmd_Mutex", for example. Then, the CreateMutex function is used and "SharingMemoryCmd_Mutex" is passed in to create the command mutex lock.
[0080] S203: The foreground application uses the character string names used by the image shared memory object and the mutex lock object as startup parameters to start the background application.
[0081] In one example, when the foreground application first appears the two parameters SharingMemoryFrame and SharingMemoryCmd, the foreground application will call the CreateProcess function to start the background application. The background application opens the shared memory object and the mutex object according to the two parameters SharingMemoryFrame and SharingMemoryCmd sent by the foreground application, and the background application can read the shared memory in real time to check whether there is a request command sent by the foreground application.
[0082] In this embodiment, the foreground application creates a shared memory object and a mutex lock object, and uses the string name used by the shared memory object and the mutex lock object as a startup parameter to start the background application, thereby ensuring that the foreground application and the background application transmit data and commands in the same shared memory object and mutex lock object, making the transmission of data and commands more accurate and improving the transmission efficiency.
[0083] Figure 4 A flowchart of a camera control method in another application provided in an embodiment of the present application, optionally, as follows: Figure 4As shown, in the above step S101, the foreground application responds to the user's operation of turning on and off the camera and sends an operation instruction to the background application, which may include:
[0084] S301. The foreground application generates an operation instruction in response to the user's operation of turning on or off the camera.
[0085] In one example, if the foreground application needs to respond to the operation of turning off the camera, the foreground application creates an operation code field, the operation code may be 2, and the foreground application saves the operation code field and generates a corresponding operation instruction.
[0086] S302: The foreground application saves the operation instruction to the target command shared memory based on the command mutex lock.
[0087] Exemplarily, before writing a command to the command shared memory, the command shared memory can be locked using a command mutex lock, for example, by modifying the value of the variable SharingMemoryCmd_Mutex_lock to 1. When the value of SharingMemoryCmd_Mutex_lock is 1, other programs that need to access the variable cannot access the command shared memory. Correspondingly, after writing the command in the command shared memory, the command shared memory can be unlocked using a command mutex lock, for example, by modifying the value of SharingMemoryCmd_Mutex_unlock to 0. When the value of SharingMemoryCmd_Mutex_lock is 0, other programs that need to access the variable can access the command shared memory normally.
[0088] In one example, when the foreground application responds to the user's operation of turning on the camera, the foreground application generates an operation instruction to turn on the camera, and at the same time the foreground application calls the command shared memory object and the command mutex lock. The foreground application can first use the command mutex lock to lock the command shared memory object, and then write the operation instruction to turn on the camera into the command shared memory. After writing the operation instruction to turn on the camera, the command mutex lock is used to unlock the command shared memory, so that the operation instruction to turn on the camera is written and saved to the target command shared memory.
[0089] In this embodiment, by using a mutex object to lock and unlock the shared memory object, it can be ensured that at any time, only one of the reading or writing processes can access the shared memory object, thereby ensuring the security of the data in the shared memory and avoiding errors in data operations.
[0090] Figure 5 A flowchart of camera control in another application provided in an embodiment of the present application is provided, optionally, as follows: Figure 5As shown, in the above step S101, the foreground application responds to the user's operation of turning on and off the camera and sends an operation instruction to the background application, which may include:
[0091] S401, the background application reads and executes the operation instructions in the target command shared memory in real time based on the command mutex lock to capture images.
[0092] For example, when instructions need to be read from the target command shared memory, before reading the command, the target command shared memory can be locked using a command mutex lock, and after the command is read, the target command shared memory can be unlocked using the command mutex lock.
[0093] In one example, the foreground application sends an image capture instruction to the background application. Before the background application reads the instruction sent by the foreground application, the background application can first use the command mutex to lock the target command shared memory. After the background application reads the image capture instruction sent by the foreground application, the background application unlocks the target command shared memory with the command mutex. After the background application reads the image capture operation instruction, the background application executes the image capture operation instruction to enable the camera to capture an image.
[0094] S402: The background application saves the captured image to the target image shared memory based on the image mutex lock, and the foreground application reads the captured image from the target image shared memory.
[0095] The background application saves the captured image to the target image shared memory, and the captured image may include image data, frame sequence number, etc.
[0096] In one example, after the background application completes image capture, it writes the image data and frame number into the target image shared memory in real time. Before writing, the target image shared memory can be locked using the image mutex lock, and after writing, the target image shared memory can be unlocked using the image mutex lock. The foreground application can read the image frame number from the target image shared memory. Before reading, the target image shared memory can be locked using the image mutex lock, and after writing, the target image shared memory can be unlocked using the image mutex lock. If the frame number read by the foreground application is different from the value read last time, it is considered that the image has been updated.
[0097] For example, when the background application completes the image capture operation, the background application writes the instruction of the completion of the operation into the command shared memory, and uses the command mutex lock to lock the command shared memory before writing, and unlocks the command mutex lock after writing. The foreground application reads the completion instruction written by the foreground application from the command shared memory in real time, and uses the command mutex lock to lock the command shared memory before reading, and unlocks the command mutex lock after reading.
[0098] For example, after the foreground application reads the image from the target image shared memory, the foreground application sends a camera-closing instruction to the background application. The foreground application writes the camera-closing instruction to the command shared memory, and uses the command mutex lock to lock the command shared memory before writing, and then uses the command mutex lock to unlock the command shared memory after writing. At the same time, after the background application receives the camera-closing instruction, it executes the camera-closing instruction.
[0099] Among them, when the foreground application reads the image from the target image shared memory, it can last for 5 seconds, waiting for the camera to output a stable image before reading the image, which can make the read data more accurate.
[0100] For example, after the background application completes the operation of closing the camera, the foreground application can obtain the number of handles occupied by the background application in real time. The foreground application determines the number of handles of the background application obtained. If the number of handles occupied by the background application exceeds a preset threshold, the foreground application sends a command to close the background application to the background application. After receiving the command to close the background application, the background application executes the command to exit the background application.
[0101] In this embodiment, the foreground application reads the image captured by the background application from the target image shared memory in real time, and obtains the number of handles occupied by the background application, so that the foreground application can obtain the number of handles of the background application more accurately, avoiding program crashes due to handle leaks. Optionally, after the foreground application sends a close instruction to the background application, it also includes:
[0102] The foreground application releases the image shared memory object, command shared memory object, image mutex, and command mutex.
[0103] Exemplarily, after the background application executes the shutdown instruction, the foreground application also correspondingly releases the aforementioned created image shared memory object, command shared memory object, image mutex lock object and command mutex lock object. For example, the data of the image shared memory object, command shared memory object, image mutex lock object and command mutex lock object can be completely cleared. Optionally, after releasing the above-mentioned image shared memory object, command shared memory object, image mutex lock and command mutex lock, the foreground application can also recreate the image shared memory object, command shared memory object, image mutex lock and command mutex lock, and start the background application.
[0104] Among them, after the foreground application releases the above-mentioned image shared memory object, command shared memory object, image mutex lock and command mutex lock, assuming that the user still needs to continue to use the camera to perform some related operations, and the foreground application has not exited, in order to respond to the camera operation command sent by the user, the foreground application needs to recreate the image shared memory object, command shared memory object, image mutex lock and command mutex lock, and at the same time, the background application needs to be started. If the user does not need to continue to use the camera and the foreground application has exited, the entire operation process ends.
[0105] In this embodiment, the user can continue to use the camera without interruption, thereby improving the user experience.
[0106] Optionally, after the foreground application sends a close instruction to the background application, the following further includes:
[0107] The foreground application determines whether the handle occupied by the background application is released. If not, the foreground application calls a preset instruction of the operating system to close the background application after a preset time.
[0108] Exemplarily, the foreground application sends a close instruction to the background application, so that the background application exits the application. At the same time, the foreground application can use the WaitForSingleObject function to determine whether the number of handles occupied by the background application is actually released. If the number of handles occupied by the background application is not actually released or the background application has not exited the application after a preset time, the foreground application can forcefully terminate the background application by calling the preset instruction of the operating system. If the background application has exited, the number of handles occupied by the background application is released by the operating system, and at the same time, the foreground application releases the image shared memory object, command shared memory object, image mutex and command mutex created previously. Exemplarily, the above-mentioned preset time can be, for example, 6 seconds, and the preset instruction can be, for example, the Taskkill instruction.
[0109] In this implementation, by determining whether the number of handles occupied by the background application is actually released or forcibly terminating the background application, it can be ensured that the background application is actually closed and the number of handles is released, thereby avoiding program crashes caused by handle leaks due to system execution errors.
[0110] Figure 6 A complete flow chart of a camera control method in an application provided in an embodiment of the present application, such as Figure 6 As shown, including:
[0111] S501: The user starts a foreground application.
[0112] S502: The foreground application creates an image shared memory object and a command shared memory object.
[0113] S503: The foreground application creates an image mutex and a command mutex.
[0114] S504: The foreground application uses the character string names used by the shared memory object and the mutex object as startup parameters to start the background application.
[0115] S505, the foreground application sends a command to the background application to control the background application to open the camera; the foreground application sends a command to the background application to control the background application to capture images, saves the captured images to the target image shared memory, and the foreground application reads them from the target image shared memory.
[0116] S506: The foreground application obtains the number of handles occupied by the background application.
[0117] S507, the foreground application determines whether the number of handles occupied by the background application exceeds the preset threshold; if the number of handles occupied by the background application does not exceed the preset threshold, return to execute the above S505 operation steps; if the number of handles occupied by the background application is greater than or equal to the preset threshold, continue to execute step S508.
[0118] S508, the foreground application terminates the background application, and the background application exits, then the number of handles occupied by the background application is released by the operating system, and at the same time, the foreground application releases the image shared memory object, the command shared memory object, the image mutex and the command mutex created previously.
[0119] S509: Determine whether the foreground application has exited. If not, return to the above step S502; if it has exited, the whole operation ends.
[0120] The specific execution process of the above steps S501-S509 has been described in the above embodiments, and the details can be referred to the above embodiments, which will not be repeated here.
[0121] The present application also provides a camera control device in an application program, referring to the aforementioned Figure 1 The device includes: a foreground application 10 and a background application 11.
[0122] The foreground application 10 is used to respond to the operation of turning on and off the camera and send an operation instruction to the background application, wherein the operation instruction is used to instruct to turn on and off the camera;
[0123] The background application 11 is used to perform the operation indicated by the operation instruction according to the operation instruction;
[0124] The foreground application 10 is also used to obtain the number of handles occupied by the background application 11; and when the number of handles occupied by the background application 11 is greater than or equal to a preset threshold, a close instruction is sent to the background application 11, and the close instruction is used to instruct the background application 11 to close.
[0125] Optionally, the foreground application 10 is specifically used to create an image shared memory object and a command shared memory object, the image shared memory object is used to identify a target image shared memory, the target image shared memory is used to store images taken by the camera, the command shared memory object identifies a target command shared memory, the target command shared memory is used to store commands issued by the foreground application and response information issued by the background application 11;
[0126] The foreground application 10 creates an image mutex and a command mutex, wherein the image mutex is used to lock the target image shared memory, and the command mutex is used to lock the target command shared memory;
[0127] The foreground application 10 uses the character string names used by the shared image memory object and the mutex lock object as startup parameters to start the background application 11 .
[0128] Optionally, the foreground application 10 is specifically used to generate the operation instruction in response to the user's operation of turning on and off the camera;
[0129] The foreground application 10 saves the operation instruction to the target command shared memory based on the command mutex lock.
[0130] Optionally, the background application 11 is specifically used to read and execute the operation instructions in the target command shared memory in real time based on the command mutex lock to perform image capture;
[0131] The background application 11 saves the captured image to the target image shared memory based on the image mutex, and the foreground application 10 reads the captured image from the target image shared memory.
[0132] Optionally, the foreground application 10 is specifically used to release the image shared memory object, the command shared memory object, the image mutex lock and the command mutex lock.
[0133] Optionally, the foreground application 10 is specifically used to recreate the image shared memory object, the command shared memory object, the image mutex lock and the command mutex lock, and start the background application.
[0134] Optionally, the foreground application 10 is specifically used to determine whether the handle occupied by the background application 11 is released;
[0135] If not, the foreground application 10 calls a preset instruction of the operating system to close the background application 11 after a preset time.
[0136] Figure 7 A structural block diagram of an electronic device 600 provided in an embodiment of the present application.
[0137] The electronic device may include: a processor 601 and a memory 602 .
[0138] The memory 602 is used to store programs, and the processor 601 calls the program stored in the memory 602 to execute the methods and steps in the above method embodiments. The specific implementation methods and technical effects are similar and will not be repeated here. The embodiment of the present application also provides a computer-readable storage medium, on which a computer program is stored, and the computer program is executed by the processor to execute the method steps in the camera control method embodiment in the above application.
[0139] Those skilled in the art can clearly understand that, for the convenience and simplicity of description, the specific working process of the system and device described above can refer to the corresponding process in the method embodiment, and will not be repeated in this application. In the several embodiments provided in this application, it should be understood that the disclosed system, device and method can be implemented in other ways. The device embodiments described above are merely schematic. For example, the division of the modules is only a logical function division. There may be other division methods in actual implementation. For example, multiple modules or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some communication interfaces, indirect coupling or communication connection of devices or modules, which can be electrical, mechanical or other forms.
[0140] In addition, each functional unit in each embodiment of the present application can be integrated into a processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. If the function 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 this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art or the part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including several instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, RandomAccess Memory), disk or optical disk and other media that can store program code.
[0141] The above are only specific implementation methods of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be covered by the protection scope of the present application.
Claims
1. A camera control method in an application, characterized in that: Applied to an electronic device, the electronic device comprises: a foreground application and a background application, the foreground application comprises a graphical user interface, and the background application runs in the background of the electronic device; The method comprises: The foreground application responds to the user's operation of turning on and off the camera and sends an operation instruction to the background application, where the operation instruction is used to instruct to turn on and off the camera; The background application executes the operation indicated by the operation instruction according to the operation instruction; The foreground application obtains the number of handles occupied by the background application; If the number of handles occupied by the background application is greater than or equal to a preset threshold, the foreground application sends a close instruction to the background application, where the close instruction is used to instruct to close the background application; The foreground application responds to the operation of turning on and off the camera, and before sending the operation instruction to the background application, the method further includes: The foreground application creates an image shared memory object and a command shared memory object, wherein the image shared memory object is used to identify a target image shared memory, and the target image shared memory is used to store images captured by the camera; the command shared memory object identifies a target command shared memory, and the target command shared memory is used to store commands issued by the foreground application and response information issued by the background application; The foreground application creates an image mutex and a command mutex, wherein the image mutex is used to lock the target image shared memory, and the command mutex is used to lock the target command shared memory; The foreground application uses the character string names used by the image shared memory object and the mutex lock object as startup parameters to start the background application; The foreground application responds to the user's operation of turning on and off the camera and sends an operation instruction to the background application, including: The foreground application generates the operation instruction in response to the user's operation of turning on and off the camera; The foreground application saves the operation instruction to the target command shared memory based on the command mutex lock; The background application performs the operation indicated by the operation instruction according to the operation instruction, including: The background application reads and executes the operation instructions in the target command shared memory in real time based on the command mutex lock to capture images; The background application saves the captured image into the target image shared memory based on the image mutex lock, and the foreground application reads the captured image from the target image shared memory.
2. The method according to claim 1, characterized in that: After the foreground application sends a closing instruction to the background application, the method further includes: The foreground application releases the image shared memory object, the command shared memory object, the image mutex lock, and the command mutex lock.
3. The method according to claim 2, characterized in that: After the foreground application releases the image shared memory object, the command shared memory object, the image mutex lock and the command mutex lock, the method further includes: The foreground application recreates the image shared memory object, the command shared memory object, the image mutex lock, and the command mutex lock, and starts the background application.
4. The method according to any one of claims 1 to 3, characterized in that: After the foreground application sends a closing instruction to the background application, the method further includes: The foreground application determines whether the handle occupied by the background application is released; If not, the foreground application program calls a preset instruction of the operating system to close the background application program after a preset time period.
5. A camera control device in an application, characterized in that: include: A foreground application and a background application, wherein the foreground application includes a graphical user interface and the background application runs in the background of the electronic device; The foreground application is used to respond to the user's operation of turning on and off the camera and send an operation instruction to the background application, wherein the operation instruction is used to instruct to turn on and off the camera; The background application is used to perform the operation indicated by the operation instruction according to the operation instruction; The foreground application is further used to obtain the number of handles occupied by the background application; and when the number of handles occupied by the background application is greater than or equal to a preset threshold, send a close instruction to the background application, wherein the close instruction is used to instruct to close the background application; The foreground application is also specifically used for: The foreground application creates an image shared memory object and a command shared memory object, wherein the image shared memory object is used to identify a target image shared memory, and the target image shared memory is used to store images captured by the camera; the command shared memory object identifies a target command shared memory, and the target command shared memory is used to store commands issued by the foreground application and response information issued by the background application; The foreground application creates an image mutex and a command mutex, wherein the image mutex is used to lock the target image shared memory, and the command mutex is used to lock the target command shared memory; The foreground application uses the character string names used by the image shared memory object and the mutex lock object as startup parameters to start the background application; The foreground application is also specifically used for: The foreground application generates the operation instruction in response to the user's operation of turning on and off the camera; The foreground application saves the operation instruction to the target command shared memory based on the command mutex lock; The background application is also specifically used for: The background application reads and executes the operation instructions in the target command shared memory in real time based on the command mutex lock to capture images; The background application saves the captured image into the target image shared memory based on the image mutex lock, and the foreground application reads the captured image from the target image shared memory.
6. An electronic device, characterized in that: The method comprises a memory and a processor, wherein the memory stores a computer program executable by the processor, and the processor implements the method according to any one of claims 1 to 4 when executing the computer program.
7. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and the computer program is executed by a processor to execute the camera control method in the application according to any one of claims 1 to 4.
Citation Information
Patent Citations
Process resource real-time monitoring method
CN105808412A
Method, apparatus, storage medium and electronic device for transmitting image information
CN109213613A