Introduction to Barcode library / Qrcode library / MyQR library / Zxing Library
0. Contents of this chapter
0.1. Use of bar codes
-
1.1. Bar code introduction
-
1.2. Introduction to barcode Library
-
1.3. Introduction to common methods and parameters of barcode Library
-
1.4. Simple realization of bar code generation with python
0.2. Use of QR code
-
2.1. Introduction to QR code
-
2.2. A brief introduction to QRcode Library
-
2.3. Using qrcode library to realize ordinary QR code
-
2.4. Advanced usage of QRcode Library
-
2.5. Brief introduction to myqr Library
-
2.6. Use MyQR library to generate QR code with pictures
0.3. Parse QR code
-
3.1. Brief introduction to zxing Library
-
3.2. Parsing QR code content using zxing Library
0.4. Introduction:
-
The barcode and QR code images generated in this chapter are in the images folder
1. Use of bar code
1.1. Bar code introduction
A bar code is a combination of multiple black bars and blanks with different widths Coding rules A graphic arrangement used to express a set of information identifier . Common bar codes are reflectivity The black bar (referred to as the bar) and the white bar (referred to as the empty bar) are arranged in a very different way Parallel line Pattern. A bar code can mark the of an item Producing country , manufacturer, trade name date of manufacture , book classification number, e-mail start and end location, category, date and many other information Commodity circulation , library management Postal Administration,banking system And many other fields have been widely used.
The barcode is divided into four parts, from left to right:
1-3 digits: 3 digits in total, such as 693, which is one of China's country codes. (690-695 are all Chinese codes, which are distributed internationally)
4-8 digits: 5 digits in total, such as 69838, representing the manufacturer code, applied by the manufacturer and allocated by the country
9-12 digits: 4 digits in total, such as 0001, representing the commodity code in the factory, which shall be determined by the manufacturer
Bit 13: 1 bit in total, such as 3, is the check code, which is calculated from the first 12 digits according to a certain algorithm.
1.2. A brief introduction to the barcode Library
-
barcode is a library used to generate barcodes. By default, images in SVG format are generated
-
Third party Library Download:
-
pip install python-barcode
1.3. Introduction to common methods and parameters of barcode Library
-
To view the barcode formats supported by Python barcode:
-
barcode.PROVIDED_BARCODES
-
Simply create barcode get
barcode.get(name,code,writer_mode,set_style,bottom_text) Parameter interpretation: name: barcode format; -- > Required parameter ode: barcode content; --- > Required parameter [ImageWriter or SVGWriter: the writer to use; the default is SVGWriter set_style: (Dictionary) the setting style for generating barcode pictures; The default is default_writer_options this variable bottom_text: text displayed at the bottom of the barcode]
-
Create barcode object (EAN):
EAN(): The parameter write is NONE, and the format of the saved image file is svg, If the parameter is ImageWrite(), the default picture format is PNG. If it needs to be changed to JPEG, it needs to be set in options = {"format": "JPEG"}
-
Save barcode image save()
The save function has two parameters: save(filename,options=None) The filename parameter is the name of the saved file. No extension is required. The extension will be automatically added according to the settings, and the full file name will be returned by the function. When the previous constructor uses the default writer, it is saved as an SVG file with an extension of svg. The default value of the options parameter is None, and the default parameter is used at this time. If you need to modify settings, use the dictionary to pass in parameters, such as: save ("D: \ \ barcode", {text ':'abcd', 'format':'jpeg '}). Available parameters are as follows: 'module_width ': the default value is 0.2, and the width of each barcode (?), Unit: mm 'module_height ': the default value is 15.0. The bar code height is in mm 'quiet_zone ': the default value is 6.5, and the width of blank space at both ends is in millimeters 'font_size ': the default value is 10. The text font size is in points 'text_distance ': the default value is 5.0. The distance between text and barcode is in millimeters 'background': default value is' white ', background color 'foreground': the default value is' black ', which is the foreground color 'text ': the default value' ', which displays text and code by default. You can also set it yourself 'write_text ': the default value is True. Whether to display text. If it is True, the value of text will be generated automatically. If it is False, it will not be generated (if the value of text is manually set at this time, the text will still be displayed). 'center_text ': the default value is True. Whether to center the text 'format': the default value is' PNG ', which saves the file format. The default value is PNG. It can also be set to JPEG, BMP, etc. it is only valid when using ImageWriter. 'dpi ': the default value is 300, and the picture resolution is,, which is only valid when using ImageWriter.
1.4. Simple realization of bar code generation with python
Guide Package:
import barcode
Create barcode format object
# Create a barcode format object. The parameter is the supported format EAN = barcode.get_barcode_class('code39')
Define barcode content
# Barcode content message = "693202111523382"
The default format is SVG, so in order to make the generated barcode a picture, the package Guide: ImageWriter
from barcode.writer import ImageWriter
Create barcode format
# Create barcode object ean = EAN(message,writer=ImageWriter())
Save barcode picture
# Save the barcode picture and return the path fullname = ean.save("code39") print(f"Barcode path:{fullname}")
1.4.1. Summary of barcode generated codes:
import barcode from barcode.writer import ImageWriter # print(f"python-barcode Supported barcode formats:\n{barcode.PROVIDED_BARCODES}") #View the barcode formats supported by Python barcode # Learn more: https://pypi.org/project/python-barcode/0.8.1/ # Create a barcode format object. The parameter is the supported format EAN = barcode.get_barcode_class('code39') # Barcode content """ Custom barcode content: 693:One of China's country codes 2021.11.5 Time to write code 23:38:Time to write code 2: 6 is calculated from the first 12 digits according to a certain algorithm+9+3-2-0-2-1-1-1-5-2+3+3-8 """ message = "693202111523382" # Create barcode object ean = EAN(message,writer=ImageWriter()) # Save the barcode picture and return the path fullname = ean.save("images/bar code") print("Barcode generation succeeded") print(f"Barcode path:{fullname}")
2. Use of QR code
2.1. Introduction to QR code
QR Code is also called two-dimensional Bar Code. The common two-dimensional code is QR Code. QR full name is Quick Response. It is a super popular coding method on mobile devices in recent years. It can store more information and represent more data types than the traditional Bar Code bar code.
Two dimensional bar code (2-dimensional bar code) is a black-and-white graphic recording data symbol information distributed in a plane (two-dimensional direction) according to a certain law with a specific geometric figure; In coding, the concepts of "0" and "1" bit streams constituting the internal logic basis of the computer are skillfully used, and several geometric bodies corresponding to binary are used to represent literal and numerical information, Automatic information processing is realized by automatic reading through image input equipment or photoelectric scanning equipment: it has some commonalities of bar code technology: each code system has its specific character set; Each character has a certain width; It has certain verification function, etc. At the same time, it also has the function of automatic identification of different information and processing graphic rotation change points.
On August 3, 2016, the payment and clearing Association payment The institution issued the code for bar code payment business (Exposure Draft), which clearly pointed out the security standards that payment institutions need to follow to carry out bar code business. This was stopped by the central bank in 2014 two-dimensional barcode payment After the first official recognition of QR code payment status.
2.2. A brief introduction to QRcode Library
qrcode library is a library used to generate QR code images. The download link of the library is as follows: qrcode · PyPI or GitHub - lincolnloop/python-qrcode: Python QR Code image generator
When using this library, you need to install the image library, which depends on django, olefile and pilot libraries.
The commands to install using pip are as follows:
pip install qrcode pip install image
2.3. Using qrcode library to realize ordinary QR code
Guide Package
import qrcode
Set QR code content
data = "Ordinary QR code generated successfully" #QR code jump link or content, but it seems that Chinese is not good
Generate QR code
image = qrcode.make(data=data)
Display QR code
image.show()
Save QR code
image.save("images/Ordinary QR code generation.jpg")
2.3.1. Generate common QR code case code summary
# qrcode ordinary QR code generation import qrcode data = "Ordinary QR code generated successfully" #QR code jump link or content, but it seems that Chinese is not good # Generate QR code image = qrcode.make(data=data) # Display QR code image.show() # Save QR code image.save("images/Ordinary QR code generation.jpg") print("Ordinary QR code generated successfully!!!") print("="*10)
2.4. Advanced usage of QRcode Library
Use of. QRCode() method
. QRCode() method parameters: #version: the lattice matrix size of QR code can be 1 to 40, the minimum is 21 * 21, and the minimum is 177 * 177 # error_correction: error tolerance rate of QR code, default ERROR_CORRECT_M. An error rate of less than 15% is allowed # box_size: the number of pixels contained in each small grid of QR code #Border: the number of small cells from the QR code to the picture border. The default value is 4
Guide Package
import qrcode
Create QRCode object
qr = qrcode.QRCode(version=1, error_correction=1, box_size=10, border=4 )
Set QR code content
data = "QR code with color generated successfully" #QR code jump link or content, but it seems that Chinese is not good
Using Add_ The data() method appends data to the current QR object
qr.add_data(data=data)
Enable QR code color settings
qr.make(fit=True)
Set the background and painting color of QR
# fill_color ` and ` back_color ` you can change the background and painting color of QR img = qr.make_image(fill_color="blue",back_color="white")
QR code display
img.show()
QR code saving
img.save("images/QR code with color.jpg")
2.4.1. Generate QR code with color case code summary
# QR code generation with color import qrcode """ .QRCode()Method parameters: version:The lattice matrix size of QR code can be 1 to 40,1 Minimum 21*21,40 It's 177*177 error_correction: QR code error tolerance, default ERROR_CORRECT_M,Allowable less than 15% Error rate of box_size: The number of pixels contained in each cell of the QR code border: The number of small cells from QR code to picture border. The default value is 4 """ qr = qrcode.QRCode(version=1, error_correction=1, box_size=10, border=4 ) data = "QR code with color generated successfully" #QR code jump connection or content, but it seems that Chinese is not good qr.add_data(data=data) # Enable QR code color settings qr.make(fit=True) img = qr.make_image(fill_color="blue",back_color="white") # QR code display # img.show() # QR code image saving img.save("images/QR code with color.jpg") print("QR code with color generated successfully!!")
2.5. Brief introduction to myqr Library
MyQR library is very powerful and can generate a variety of QR codes. It can generate normal QR code, black-and-white picture QR code, color picture QR code and dynamic QR code
The Github address of MyQR is as follows
https://github.com/sylnsfar/qrcode
The MyQR library can be installed from the command line or between pycharms
pip install myqr
2.6. Use MyQR library to generate QR code with pictures
2.6.1. Generate ordinary QR code with picture
We just need to call myqr Run () method, passing the given parameters can generate the specified QR code.
run() method parameters:
run(words, version=1, level='H', picture=None, colorized=False, contrast=1.0, brightness=1.0, save_name=None, save_dir=os.getcwd())
Introduction to run() method parameters
Guide Package:
from MyQR import myqr
The content of the QR code can be characters or links, but it seems that Chinese is not easy to use
words = "QR code with graph generated successfully"
Call myqr Run () method, passing the given parameters can generate the specified QR code
myqr.run( words=words, version=1, picture='images/A1.png', colorized=True, save_name="images/QR code with picture.png" )
2.6.1.1. Case code summary of generating QR code with graph
# QR code with graph from MyQR import myqr """ words: QR code content, links or sentences version: QR code size, range[1,40] level: QR code error correction level, range:{L,M,Q,H},H It is the most advanced and the default is. picture: Custom QR code background image. The supported formats are .jpg,.png,.bmp,.gif,The default is black and white colorized: QR code background color, default to False,Black and white contrast: Contrast. The higher the value, the higher the contrast. The default value is 1.0 brightness: Brightness: the higher the value, the higher the brightness. The default value is 1.0,The value is often the same as the contrast save_name: QR code name, default to qrcode.png save_dir: QR code path, the default is the program working path """ # QR code content words = "QR code with graph generated successfully" myqr.run( words=words, version=1, picture='images/A1.png', colorized=True, save_name="images/QR code with picture.png" ) print("QR code with graph generated successfully!!!") print("="*10)
2.6.2. Generate QR code of driving graph
Just change the picture parameter in the run() function to GIF dynamic graph
Guide Package
from MyQR import myqr
The content of the QR code can be characters or links, but it seems that Chinese is not easy to use
words = "The QR code. GIF of the drawing was saved successfully"
Use the run() function to generate the QR code
myqr.run( words=words, version=1, picture="images/penguin.gif", colorized=True, save_name="images/Two dimensional code of driving graph.gif" )
2.6.2.1. Summary of QR code case code for generating driving diagram
# Two dimensional code of driving graph from MyQR import myqr # more: https://pypi.org/project/MyQR/ https://github.com/sylnsfar/qrcode words = "The QR code. GIF of the drawing was saved successfully" myqr.run( words=words, version=1, picture="images/penguin.gif", colorized=True, save_name="images/Two dimensional code of driving graph.gif" ) print("Two dimensional code of driving graph.gif Saved successfully!!") print("="*10)
3. Parse QR code
3.1. Brief introduction to zxing Library
zxing library is an open source, multi format 1D/2D barcode image processing library, which is implemented in Java and has ports in other languages, but there is an unexpected EOF. Our focus is to use the built-in camera on the phone to scan and decode barcodes on the device without communicating with the server. However, the project can also be used to encode and decode barcodes on desktops and servers.
Currently supported formats:
UPC-A and UPC-EEAN-8 and EAN-13Code 39 | Code 93Code 128ITF | CodabarRSS-14 (all variants)RSS Expanded (most variants)QR Code | Data MatrixAztec ('beta' quality)PDF 417 ('alpha' quality) |
---|---|---|---|
Third party package download:
pip install zxing
Learn more: Python zxing package_ Program module - PyPI - Python Chinese network
3.2. Parsing QR code content using zxing Library
Guide Package:
import zxing
Instantiate BarCodeReader
reader = zxing.BarCodeReader()
Call the decode() function, decode (barcode or 2D code to be parsed)
In the decode function, Popen in the subprocess package is used to call the jar package, which is similar to the pipeline communication in VB and VC.
barcode = reader.decode("images/Two dimensional code of driving graph.gif")
Get parsed content
result = barcode.parsed print(f"Parsing QR code content:{result}")
3.2.1. Analysis of QR code case code summary
import zxing reader = zxing.BarCodeReader() # In the decode function, Popen in the subprocess package is used to call the jar package, which is similar to the pipeline communication in VB and VC. barcode = reader.decode("images/Two dimensional code of driving graph.gif") result = barcode.parsed print(f"Parsing QR code content:{result}")
3.3. Use the zxing library to parse the contents of the barcode
Guide Package:
import zxing
Instantiate BarCodeReader
reader = zxing.BarCodeReader()
Call the decode() function, decode (barcode or 2D code to be parsed)
In the decode function, Popen in the subprocess package is used to call the jar package, which is similar to the pipeline communication in VB and VC.
barcode = reader.decode("images/bar code.png")
Get parsed content
result = barcode.parsed print(f"Analyze the content of barcode:{result}")
3.3.1. Analysis of barcode case code summary
# Parse barcode import zxing reader = zxing.BarCodeReader() # In the decode function, Popen in the subprocess package is used to call the jar package, which is similar to the pipeline communication in VB and VC. barcode = reader.decode("images/bar code.png") result = barcode.parsed print(f"Analyze the content of barcode:{result}")
If you need source code, please visit: QR code and bar code: simply implement QR code and bar code with python