Two-dimensional code scanning methodTechnical Field
The invention relates to the field of detection, in particular to a two-dimensional code scanning method.
Background
With the progress of science and technology, the development of the electronic industry is rapidly advanced, electronic systems are more and more complex, the requirements on electronic reliability and maintainability are higher and higher, and the quality of a PCB (printed Circuit Board) serving as an important component in the development and production of the electronic systems directly influences the quality of the whole equipment.
Therefore, the PCB is generally required to be tested to a certain extent in the actual production process, when the PCB is tested, the two-dimensional code on the PCB needs to be read firstly, the decoded two-dimensional code is input into a system, the detection data is convenient to correspond to the PCB, then the PCB is fixed on a corresponding clamp, and the PCB is pushed to a detection device for testing.
At present, scanning of the two-dimensional code is carried out by a worker holding a scanning gun, and the detection efficiency is low.
Disclosure of Invention
The invention aims to provide a two-dimensional code scanning method, which is characterized in that a camera is used for acquiring a two-dimensional code on a PCB and decoding the two-dimensional code, so that information on the two-dimensional code is acquired and a result is returned, and the detection efficiency is improved.
The technical purpose of the invention is realized by the following technical scheme: the two-dimensional code scanning method comprises the following steps: when the PCB is pushed in place, receiving a decoding instruction; capturing an image with a camera; detecting whether the image has the two-dimensional code, if so, preprocessing the image, and otherwise, capturing the image again; decoding the captured two-dimensional code; if the decoding is successful, a decoding result is returned, otherwise, the image is restored and then the decoding processing is carried out again.
By adopting the technical scheme, when the PCB is pushed in place, the camera is utilized to acquire the two-dimensional code on the PCB, the two-dimensional code is decoded, the decoded result is returned after the information of the two-dimensional code is acquired, and the image is decoded again after being repaired after the decoding fails, so that the decoding success rate is improved, manual scanning is replaced, the detection efficiency is improved, and the reliability of the decoded result is high.
Preferably, the step of repairing the image comprises: cropping a two-dimensional code region from the captured image; acquiring a two-dimensional code angle and correcting; positioning the two-dimensional code area again and cutting; repairing the periphery of the two-dimensional code; and placing the repaired two-dimensional code image in the center of a background with proper size and gray level.
By adopting the technical scheme, the two-dimensional code is adjusted and repaired and then placed in a proper background center, so that the successful decoding probability of the two-dimensional code is improved.
Preferably, when the periphery of the two-dimensional code is repaired, whether the characteristic information of the two-dimensional code is read or not is selected, if so, the periphery of the two-dimensional code is repaired according to the characteristic information, otherwise, direction judgment is performed according to the periphery characteristic of the two-dimensional code, and the periphery of the two-dimensional code is repaired according to a default value.
By adopting the technical scheme, whether the characteristic information of the two-dimensional code is read or not is selected according to a preset scheme for repairing, a more appropriate repairing scheme is selected, and the detection reliability is improved.
Preferably, when the image is preprocessed, a region of interest to be processed in the image is set; resizing the image; carrying out smoothing processing on the image; histogram equalization processing is performed.
By adopting the technical scheme, the image is preprocessed, and the success rate and the reliability of two-dimensional code decoding are improved.
Preferably, after receiving the decoding instruction, whether the last decoding thread is finished is judged, if yes, the decoding thread is continued, and if not, the camera is prompted to be busy.
By adopting the technical scheme, the situation of data confusion caused by the simultaneous decoding operation of two-dimensional codes is avoided.
Preferably, before capturing the image, it is determined whether the camera needs to be reloaded, if so, the camera is restarted, otherwise, it is determined whether the camera is already opened, if so, a buffer area of the camera is cleared, otherwise, the camera is opened.
By adopting the technical scheme, if the camera is inserted again, the camera needs to be reloaded, and if the camera is opened, the buffer area of the camera is cleared, so that the normal operation of a decoding thread is ensured.
Preferably, the image is captured again by the camera when the image fails to be decoded again after being restored.
By adopting the technical scheme, the decoding still fails after the image is repaired, the image is obtained again for decoding operation, and the success rate of decoding is improved.
Preferably, when the decoding fails after the image is restored, whether the decoding is overtime is judged, if yes, the decoding is returned to overtime, and if not, the camera is used again to capture the image.
By adopting the technical scheme, when the time spent on decoding exceeds the preset time, the overtime is judged, the decoding process is terminated, and the overtime is returned.
Preferably, before receiving the decoding instruction, the method waits for the call of the instruction, and if the called instruction is the decoding instruction, the decoding process is performed; if the called instruction is a configuration reading instruction, reading configuration and returning a value; if the calling instruction is a write configuration instruction, writing configuration and returning a result; if the called instruction is an instruction for obtaining an instruction list, obtaining a supported instruction list and returning to the instruction list; and if the called instruction is an instruction for acquiring instruction, acquiring and returning the instruction of the corresponding instruction.
By adopting the technical scheme, corresponding actions are carried out when different instructions are received, and a plurality of methods for obtaining results are provided.
Preferably, the instruction is called via the RPC protocol.
By adopting the technical scheme, the efficiency of instruction calling is improved.
In conclusion, the invention has the following beneficial effects: the image corresponding to the two-dimensional code on the PCB is acquired by the camera, the image is preprocessed, the two-dimensional code is decoded, the decoding result is returned after the information of the two-dimensional code is acquired, the image is restored after the decoding fails, the decoding success rate is improved, manual scanning is replaced, the detection efficiency is improved, and the reliability of the decoding result is high.
Drawings
FIG. 1 is a main flow chart of the present embodiment;
fig. 2 is a decoding flowchart of the present embodiment;
FIG. 3 is a flowchart of a decoding thread of the present embodiment;
FIG. 4 is a flowchart of image preprocessing of the present embodiment;
fig. 5 is a flowchart of decoding the two-dimensional code according to the present embodiment.
Detailed Description
The present invention will be described in further detail with reference to the accompanying drawings.
The following specific examples are given by way of illustration only and not by way of limitation, and it will be apparent to those skilled in the art from this disclosure that various changes and modifications can be made in the examples without inventive faculty, and yet still be protected by the scope of the claims set forth above.
The two-dimensional code scanning method, as shown in fig. 1, includes the following steps:
starting Json-RPC decoding service, waiting for and calling a command by using an RPC protocol, if the called command is a decoding command, performing a decoding process, returning a decoding result to the system by using the RPC protocol after the decoding is finished, wherein the decoding command is sent by a clamp, and when a worker places a PCB to be detected on the clamp and pushes the PCB to be detected into a detection device, the clamp sends the decoding command;
if the called instruction is a configuration reading instruction, reading the configuration and returning a value through an RPC protocol;
if the call instruction is a write configuration instruction, writing configuration and returning a result through an RPC protocol;
if the called instruction is an instruction for acquiring an instruction list, acquiring a supported instruction list and returning the instruction list through an RPC protocol;
and if the called instruction is an instruction for acquiring instruction, acquiring and returning the instruction of the corresponding instruction through an RPC protocol.
For the decoding process, as shown in fig. 2, it is first determined whether the previous decoding thread is finished, if the previous decoding thread is finished, the current decoding thread is started, otherwise, the result is marked as "camera busy" and the result is returned.
And when starting the decoding thread of the current time, synchronously waiting for the decoding thread to end until the decoding thread is overtime, if the decoding process is not overtime, acquiring the result of the decoding thread and returning the decoding result, otherwise, marking the decoding thread as overtime, ending the decoding thread, marking the result as 'overtime', and returning the result.
For the decoding thread, as shown in fig. 3, it is first determined whether the camera needs to be reloaded, if the camera is reinserted, the camera needs to be reloaded, and if the camera is reloaded, the camera is restarted, and then the camera is used to capture an image of the position of the two-dimensional code in the PCB.
And if the camera is not reloaded, judging whether the camera is opened, and if the camera is opened, capturing an image of the position of the two-dimensional code in the PCB by using the camera after cleaning a buffer area of the camera.
And if the camera is not opened, the operation of opening the camera is carried out, if the camera is successfully opened, the camera is used for capturing the image of the position of the two-dimensional code in the PCB, otherwise, the result of the decoding thread is marked as 'camera opening failure'.
After an image is captured by a camera, whether a two-dimensional code exists in a captured picture is detected, if no two-dimensional code exists, the focusing and exposure values of the camera are reset, the image is captured again, and when the times of resetting the focusing and exposure values of the camera exceed 2 times, the result of a decoding thread is marked as 'no code'.
If the two-dimensional code is detected, preprocessing the image, improving the decoding success rate and quality, decoding the two-dimensional code after preprocessing the image, if the decoding is successful, outputting the result of the decoding thread as the decoding result, otherwise, judging whether the decoding thread is overtime, if the decoding thread is overtime, marking the decoding thread result as overtime, otherwise, capturing the image again.
As shown in fig. 4, the image preprocessing process includes the steps of setting an area of interest in an image to be processed, resetting the size of the image, smoothing the image, performing histogram equalization, and outputting the preprocessed image, so as to improve the success rate and reliability of two-dimensional code decoding.
The two-dimensional code decoding process is as shown in fig. 5, and two-dimensional code decoding is performed on a two-dimensional code in a preprocessed image, where a two-dimensional code decoding algorithm is the prior art and is not described herein any more, and if decoding is successful, two-dimensional code decoding information is output as a decoding result, otherwise, decoding processing is performed again after the image is repaired.
When the image is repaired, the method comprises the following steps:
cropping a two-dimensional code region from the captured image;
acquiring a two-dimensional code angle and correcting;
positioning the two-dimensional code area again and cutting;
repairing the periphery of the two-dimensional code;
and placing the repaired two-dimensional code image in a background center with proper size and gray level.
When the periphery of the two-dimensional code is repaired, whether the characteristic information of the two-dimensional code is read or not is selected, if so, the periphery of the two-dimensional code is repaired according to the characteristic information, otherwise, direction judgment is carried out according to the periphery characteristics of the two-dimensional code, and the periphery of the two-dimensional code is repaired according to a default value.
Has the advantages that: the image corresponding to the two-dimensional code on the PCB is acquired by the camera, the image is preprocessed, the two-dimensional code is decoded, the decoding result is returned after the information of the two-dimensional code is acquired, the image is restored after the decoding fails, the decoding success rate is improved, manual scanning is replaced, the detection efficiency is improved, and the reliability of the decoding result is high.