海口网络平台网站开发,网页设计需要学什么软件知乎,制作软件的手机软件,付费推广网站1、QR概述
QR(Quick Response)属于二维条码的一种#xff0c;意思是快速响应的意思。QR码不仅信息容量大、可靠性高、成本低#xff0c;还可表示汉字及图像等多种文字信息、其保密防伪性强而且使用非常方便。更重要的是QR码这项技术是开源的#xff0c;在移动支付、电影票、…1、QR概述
QR(Quick Response)属于二维条码的一种意思是快速响应的意思。QR码不仅信息容量大、可靠性高、成本低还可表示汉字及图像等多种文字信息、其保密防伪性强而且使用非常方便。更重要的是QR码这项技术是开源的在移动支付、电影票、电子会员卡等场景以及很多的产品上也印刷有这样的二维码给人们的日常生活带来了很大便利。 QR码中数据值包含很多冗余值。所以即便多达30%的二维码结构被破坏也不影响二维码的可读性。QR码的存储空间随着版本号越大存储越多从V1版本的21个字符到V40版本可以存储4296个字符包括标点符号和特殊字符都可以写入QR码中。除了数字和字符之外还可以对单词和短语(例如网址)进行编码。随着更多的数据被添加到QR码代码大小增加代码结构变得更加复杂。当然QR码的存储空间还跟编码方式误差纠正等因素都有关系所以在使用时需要考虑这些因素选择合适的版本和编码方式。
2、QR码生成
2.1、Linux与Windows
安装QRCode相关模块由于本机是安装了Python2的版本也可以选用Python3版本来安装Linux环境安装python3 -m pip install qrcodeWindows环境安装(JupyterLab)
!pip install qrcode -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
安装好了之后来看一个最简的生成QRCode二维码代码信息是本人的博客网址myqr.py
import qrcodeimg qrcode.make(https://chyichin.blog.csdn.net/)
img.save(myqr.png)
需要注意的是这里的文件名称不能是关键字qrcode如果文件名为qrcode.py就会报错 AttributeError: module object has no attribute make 运行python3 myqr.py 将生成一张QR二维码的图片myqr.png 使用微信扫码可以进入这个网站也可以使用内置命令查看该图片eog myqr.png
2.2、添加logo
还可以在QR码上面添加自定义的logo图代码如下
import qrcode
from PIL import Imagedef addLogo(img,logo):imgW,imgH img.sizelogo Image.open(logo)logoW,logoH logo.sizefactor 5 #缩放因子sizeW int(imgW/factor)sizeH int(imgH/factor)if logoW sizeW:logoW sizeWif logoH sizeH:logoH sizeHlogo logo.resize((logoW,logoH),Image.Resampling.LANCZOS)#将logo粘贴到图片中心位置w int((imgW-logoW)/2)h int((imgH-logoH)/2)img.paste(logo,(w,h),maskNone)return imgdef GenQRCode(data,outname,logo):qr qrcode.QRCode(version7,error_correctionqrcode.constants.ERROR_CORRECT_H,box_size10,border4,)#添加与填充数据qr.add_data(data)qr.make(fitTrue)img qr.make_image(fill_colorblue,back_colorwhite)addLogo(img,logo)img.save(outname)return imgif __name__ __main__:GenQRCode(https://chyichin.blog.csdn.net/, myLogoQR.png, p.jpg)
其中p.jpg就是本人头像这样就将头像按照比例缩放添加到了QR二维码中心位置上面生成的QR二维码如下图可以看到除了黑白之外还可以使用自定义颜色来设置前景和背景 3、QR码分析
对于上面生成的二维码里面的每个位置所代表的信息是不一样的我们来详细看一个表格
定位标识 (Positioning markings)扫码时不需要对准可以是任意角度仍然能够准确识别。对齐标记(Alignment markings)如果二维码很大这些附加元素帮助定位。计算模式(Timing pattern)通过这些线扫描器可以识别矩阵有多大。版本信息(Version information)版本号目前有40个不同的版本号(销售行业的的版本号通常为1~7)格式信息(Format information)包含关于容错和数据掩码模式的信息使得扫描更加容易。数据和错误校正值(Data and error correction keys)保存的是实际数据。宁静区域(Quiet zone)这个区域对于扫描器来说非常重要能够将自身与周边进行分离。
其中代码中的qrcode.QRCode函数里面的参数含义如下
version版本号值为1~40的整数控制二维码的大小(最小值为112×12的矩阵)。如果想让程序自动确定将值设置为 None 并使用 fit 参数即可。error_correction控制二维码的错误纠正功能纠正多少取决于qrcode.constants的设定 ERROR_CORRECT_L大约7%或更少的错误能被纠正。 ERROR_CORRECT_M(默认)大约15%或更少的错误能被纠正。 ROR_CORRECT_H大约30%或更少的错误能被纠正。box_size控制二维码中每个小格子包含的像素数。border控制边框(二维码与图片边界的距离)包含的格子数(默认为4)
4、QR码识别
上面是生成QR码接下来就是如何让摄像头去识别QR码这里将会用到pyzbar库去解析QR码
Linux环境
python3 -m pip install qrcode pyzbar
sudo apt-get install libzbar-dev
Windows环境JupyterLab
!pip install pyzbar -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
当然如果是在命令行安装就不需要这个叹号! 由于本人没有摄像头所以依然使用无人车上面的CSI摄像头来做测试识别QR码的代码如下Recog_myqr.py
import time
import cv2 as cv
import numpy as np
import pyzbar.pyzbar as pyzbar
from PIL import Image, ImageDraw, ImageFontdef RecogQRCode(image, font_path):# 转成灰度图片gray cv.cvtColor(image, cv.COLOR_BGR2GRAY)barcodes pyzbar.decode(gray)for barcode in barcodes:# 获取QR码边界框位置画出图像中条形码的边界框(x, y, w, h) barcode.rectcv.rectangle(image, (x, y), (x w, y h), (225, 0, 0), 5)encoding UTF-8barcodeData barcode.data.decode(encoding)barcodeType barcode.type# 绘出图像上数据和类型pilimg Image.fromarray(image)# 创建画笔draw ImageDraw.Draw(pilimg)# 将识别的信息画在QR码以上25个像素处指定字体与大小fontStyle ImageFont.truetype(font_path, size12, encodingencoding)draw.text((x, y - 25), str(barcode.data, encoding), fill(255, 0, 0), fontfontStyle)# 将PIL图转成cv2图image cv.cvtColor(np.array(pilimg), cv.COLOR_RGB2BGR)print(Type:{} Data:{}.format(barcodeType, barcodeData))return image# 调节图像质量
#/usr/lib/aarch64-linux-gnu/gstreamer-1.0/libgstnvarguscamerasrc.so
def gstreamer_pipeline(capture_width640,capture_height480,display_width640,display_height480,framerate30,flip_method0,
):return (nvarguscamerasrc ! video/x-raw(memory:NVMM), width(int)%d, height(int)%d, format(string)NV12, framerate(fraction)%d/1 ! nvvidconv flip-method%d ! video/x-raw, width(int)%d, height(int)%d, format(string)BGRx ! videoconvert ! video/x-raw, format(string)BGR ! appsink% (capture_width,capture_height,framerate,flip_method,display_width,display_height,))if __name__ __main__:# 字体识别中文font_path ../font/Block_Simplified.TTF#font_path C:\Windows\Fonts\simsun.ttccapture cv.VideoCapture(gstreamer_pipeline(flip_method0), cv.CAP_GSTREAMER)cv_edition cv.__version__if cv_edition[0] 3: capture.set(cv.CAP_PROP_FOURCC, cv.VideoWriter_fourcc(*XVID))else: capture.set(cv.CAP_PROP_FOURCC, cv.VideoWriter.fourcc(M, J, P, G))capture.set(cv.CAP_PROP_FRAME_WIDTH, 640)capture.set(cv.CAP_PROP_FRAME_HEIGHT, 480)print(capture get FPS : , capture.get(cv.CAP_PROP_FPS))while capture.isOpened():start time.time()ret, frame capture.read()action cv.waitKey(10) 0xFFframe RecogQRCode(frame, font_path)end time.time()fps 1 / (end - start)text FPS : str(int(fps))cv.putText(frame, text, (30, 30), cv.FONT_HERSHEY_SIMPLEX, 0.6, (100, 200, 200), 1)cv.imshow(frame, frame)if action ord(q) or action 113: breakcapture.release()cv.destroyAllWindows()
其中gstreamer_pipeline方法里面的nvarguscamerasrc是英伟达的Argus Camera的库我们可以通过GStreamer提供的gst-inspect-1.0指令去查询CSI摄像头可设定的参数有哪些 Factory Details: Rank primary (256) Long-name NvArgusCameraSrc Klass Video/Capture Description nVidia ARGUS Camera Source Author Viranjan Pagar vpagarnvidia.com, Amit Pandya apandyanvidia.com Plugin Details: Name nvarguscamerasrc Description nVidia ARGUS Source Component Filename /usr/lib/aarch64-linux-gnu/gstreamer-1.0/libgstnvarguscamerasrc.so Version 1.0.0 License Proprietary Source module nvarguscamerasrc Binary package NvARGUSCameraSrc Origin URL http://nvidia.com/ GObject ----GInitiallyUnowned ----GstObject ----GstElement ----GstBaseSrc ----GstNvArgusCameraSrc Pad Templates: SRC template: src Availability: Always Capabilities: video/x-raw(memory:NVMM) width: [ 1, 2147483647 ] height: [ 1, 2147483647 ] format: { (string)NV12 } framerate: [ 0/1, 2147483647/1 ] Element has no clocking capabilities. Element has no URI handling capabilities. Pads: SRC: src Pad Template: src Element Properties: name : The name of the object flags: readable, writable String. Default: nvarguscamerasrc0 parent : The parent of the object flags: readable, writable Object of type GstObject blocksize : Size in bytes to read per buffer (-1 default) flags: readable, writable Unsigned Integer. Range: 0 - 4294967295 Default: 4096 num-buffers : Number of buffers to output before sending EOS (-1 unlimited) flags: readable, writable Integer. Range: -1 - 2147483647 Default: -1 typefind : Run typefind before negotiating (deprecated, non-functional) flags: readable, writable, deprecated Boolean. Default: false do-timestamp : Apply current stream time to buffers flags: readable, writable Boolean. Default: true silent : Produce verbose output ? flags: readable, writable Boolean. Default: true timeout : timeout to capture in seconds (Either specify timeout or num-buffers, not both) flags: readable, writable Unsigned Integer. Range: 0 - 2147483647 Default: 0 wbmode : White balance affects the color temperature of the photo flags: readable, writable Enum GstNvArgusCamWBMode Default: 1, auto (0): off - GST_NVCAM_WB_MODE_OFF (1): auto - GST_NVCAM_WB_MODE_AUTO (2): incandescent - GST_NVCAM_WB_MODE_INCANDESCENT (3): fluorescent - GST_NVCAM_WB_MODE_FLUORESCENT (4): warm-fluorescent - GST_NVCAM_WB_MODE_WARM_FLUORESCENT (5): daylight - GST_NVCAM_WB_MODE_DAYLIGHT (6): cloudy-daylight - GST_NVCAM_WB_MODE_CLOUDY_DAYLIGHT (7): twilight - GST_NVCAM_WB_MODE_TWILIGHT (8): shade - GST_NVCAM_WB_MODE_SHADE (9): manual - GST_NVCAM_WB_MODE_MANUAL saturation : Property to adjust saturation value flags: readable, writable Float. Range: 0 - 2 Default: 1 sensor-id : Set the id of camera sensor to use. Default 0. flags: readable, writable Integer. Range: 0 - 255 Default: 0 sensor-mode : Set the camera sensor mode to use. Default -1 (Select the best match) flags: readable, writable Integer. Range: -1 - 255 Default: -1 total-sensor-modes : Query the number of sensor modes available. Default 0 flags: readable Integer. Range: 0 - 255 Default: 0 exposuretimerange : Property to adjust exposure time range in nanoseconds Use string with values of Exposure Time Range (low, high) in that order, to set the property. eg: exposuretimerange34000 358733000 flags: readable, writable String. Default: null gainrange : Property to adjust gain range Use string with values of Gain Time Range (low, high) in that order, to set the property. eg: gainrange1 16 flags: readable, writable String. Default: null ispdigitalgainrange : Property to adjust digital gain range Use string with values of ISP Digital Gain Range (low, high) in that order, to set the property. eg: ispdigitalgainrange1 8 flags: readable, writable String. Default: null tnr-strength : property to adjust temporal noise reduction strength flags: readable, writable Float. Range: -1 - 1 Default: -1 tnr-mode : property to select temporal noise reduction mode flags: readable, writable Enum GstNvArgusCamTNRMode Default: 1, NoiseReduction_Fast (0): NoiseReduction_Off - GST_NVCAM_NR_OFF (1): NoiseReduction_Fast - GST_NVCAM_NR_FAST (2): NoiseReduction_HighQuality - GST_NVCAM_NR_HIGHQUALITY ee-mode : property to select edge enhnacement mode flags: readable, writable Enum GstNvArgusCamEEMode Default: 1, EdgeEnhancement_Fast (0): EdgeEnhancement_Off - GST_NVCAM_EE_OFF (1): EdgeEnhancement_Fast - GST_NVCAM_EE_FAST (2): EdgeEnhancement_HighQuality - GST_NVCAM_EE_HIGHQUALITY ee-strength : property to adjust edge enhancement strength flags: readable, writable Float. Range: -1 - 1 Default: -1 aeantibanding : property to set the auto exposure antibanding mode flags: readable, writable Enum GstNvArgusCamAeAntiBandingMode Default: 1, AeAntibandingMode_Auto (0): AeAntibandingMode_Off - GST_NVCAM_AEANTIBANDING_OFF (1): AeAntibandingMode_Auto - GST_NVCAM_AEANTIBANDING_AUTO (2): AeAntibandingMode_50HZ - GST_NVCAM_AEANTIBANDING_50HZ (3): AeAntibandingMode_60HZ - GST_NVCAM_AEANTIBANDING_60HZ exposurecompensation: property to adjust exposure compensation flags: readable, writable Float. Range: -2 - 2 Default: 0 aelock : set or unset the auto exposure lock flags: readable, writable Boolean. Default: false awblock : set or unset the auto white balance lock flags: readable, writable Boolean. Default: false bufapi-version : set to use new Buffer API flags: readable, writable Boolean. Default: false 然后运行python3 Recog_myqr.py将打开摄像头其识别效果如下
可以看到QR码上面显示了内容信息然后我们也可以来到终端看下其显示 正确显示了识别的类型为QRCode以及数据这里就是本人的博客网址。试着识别下微信的付款码和收款码识别情况如下 收款码 Type:QRCODE Data:wxp://XXXtQeEHmJp67RHOPVVG-D7oGonAQTxE1p6V9rG898iUklUHgbd5XXXX付款码 Type:QRCODE Data:131568199XXXX