我一直在使用 OpenCV 方法从我的相机中获取图像。我想使用 zbar 库从这些图像中解码 QR 码,但是在我将图像转换为 PIL 以由 zbar 处理之后,解码似乎不起作用。
import cv2.cv as cv
import zbar
from PIL import Image
cv.NamedWindow("camera", 1)
capture = cv.CaptureFromCAM(0)
while True:
img = cv.QueryFrame(capture)
cv.ShowImage("camera", img)
if cv.WaitKey(10) == 27:
break
# create a reader
scanner = zbar.ImageScanner()
# configure the reader
scanner.parse_config('enable')
# obtain image data
pil = Image.fromstring("L", cv.GetSize(img), img.tostring())
width, height = pil.size
raw = pil.tostring()
# wrap image data
image = zbar.Image(width, height, 'Y800', raw)
# scan the image for barcodes
scanner.scan(image)
# extract results
for symbol in image:
# do something useful with results
print 'decoded', symbol.type, 'symbol', '"%s"' % symbol.data
cv.DestroyAllWindows()