Raspberry Pi [learn] ten, two-dimensional code recognition using zbar

First, the introduction of several functions

1, the region where the two-dimensional code pass into RGB, and converts it into an image inside pil

pil= Image.fromarray(frame).convert('L')#转换成 L 模式, 即灰度模式
width, height = pil.size
raw = pil.tobytes()

2, calling library functions zbar dimensional code recognition

zarimage = zbar.Image(width, height, 'Y800', raw)#把图像装换成数据
scanner.scan(zarimage)#扫描器进行扫描

Second, the sample code

First, you need to enter the following code, the installation zbarlibrary

sudo apt-get install python-zbar
import cv2
import numpy as np
import zbar
from PIL import Image

# create a reader
scanner = zbar.ImageScanner()
# configure the reader
scanner.parse_config('enable')
font=cv2.FONT_HERSHEY_SIMPLEX
camera=cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    grabbed, frame = camera.read()
    if not grabbed:
        break
    pil= Image.fromarray(frame).convert('L')
    width, height = pil.size
    raw = pil.tobytes()
    zarimage = zbar.Image(width, height, 'Y800',raw)
    scanner.scan(zarimage)
    for symbol in zarimage:
    # do something useful with results 
		if not symbol.count:
			print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data
		cv2.putText(frame,symbol.data,(20,100),font,1,(0,255,0),4)
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

Third, the operating results

Take the following two-dimensional code, for example, try the effect slightly ~
Here Insert Picture Description

Not bad effect

Here Insert Picture Description

Published 653 original articles · won praise 1016 · Views 730,000 +

Guess you like

Origin blog.csdn.net/ReCclay/article/details/103697480