python - 无法使用zbar扫描QR码

原文 标签 python opencv zbar

My QR code
当我运行我的代码,我总是得到“[])”作为输出,这意味着Zbar不能读取QR我的图像格式有问题吗我搞不懂为什么这不管用。
我的代码:

import numpy as np
import cv2
import zbar
import sys

class ImageSource(object):
    def __init__(self,source):
        self.is_camera = type(source)==int
        if self.is_camera:
            self.source = cv2.VideoCapture(source)
        else:
            self.source = cv2.imread(source,1)

    def get_size(self):
        if self.is_camera:
            return (int(self.source.get(3)),int(self.source.get(4)))
        else:
            return (self.source.shape[1],self.source.shape[0])

    def get_next(self):
        if self.is_camera:
            return self.source.read()[1]
        else:
            return self.source

    def release(self):
        if self.is_camera:
            self.source.release()

class QRScanner(object):
    def __init__(self, width, height):
        self.scanner = zbar.ImageScanner()
        self.scanner.parse_config('enable')
        self.width = width
        self.height = height

    def get_qrcodes(self, image):
        zbar_img = self.cv2_to_zbar_image(image)
        self.scanner.scan(zbar_img)
        result=[]
        for symbol in zbar_img:
            if symbol.type!=zbar.Symbol.QRCODE: continue
            fixed_data = symbol.data.decode("utf8").encode("shift_jis").decode("utf8")

            result.append(QRCode(fixed_data,symbol.location))
        del(zbar_img)
        return result

    def cv2_to_zbar_image(self, cv2_image):
        return zbar.Image(self.width, self.height, 'Y800',cv2_image.tostring())


cap = ImageSource("1.png")
frame = cap.get_next()
scanner = QRScanner(cap.get_size()[0],cap.get_size()[1])
print scanner.get_qrcodes(frame)

最佳答案

不知道实际的问题是什么,但我能够得到这个工作使用Pil(或枕头)而不是cv2。

import cv2
import Image
import zbar
import sys

class ImageSource(object):
    def __init__(self,source):
        self.is_camera = type(source)==int
        if self.is_camera:
            self.source = cv2.VideoCapture(source)
        else:
            self.source = cv2.imread(source,1)

    def get_size(self):
        if self.is_camera:
            return (int(self.source.get(3)),int(self.source.get(4)))
        else:
            return (self.source.shape[1],self.source.shape[0])

    def get_next(self):
        if self.is_camera:
            return self.source.read()[1]
        else:
            return self.source

    def release(self):
        if self.is_camera:
            self.source.release()

class QRScanner(object):
    def __init__(self, width, height):
        self.scanner = zbar.ImageScanner()
        self.scanner.parse_config('enable')
        self.width = width
        self.height = height

    def get_qrcodes(self, image):
        zbar_img = self.cv2_to_zbar_image(image)
        self.scanner.scan(zbar_img)
        result=[]
        for symbol in zbar_img:
            if symbol.type!=zbar.Symbol.QRCODE: continue
            fixed_data = symbol.data.decode("utf8").encode("shift_jis").decode("utf8")

            result.append((fixed_data,symbol.location))
        del(zbar_img)
        return result

    def cv2_to_zbar_image(self, cv2_image):
        return zbar.Image(self.width, self.height, 'Y800',cv2_image.tostring())

pil = Image.open(sys.argv[1]).convert('L')
width, height = pil.size
scanner = QRScanner(width,height)
print scanner.get_qrcodes(pil)

另外,我尝试过的所有二维码都不适用,但有一个确实有效:
http://media.techtarget.com/rms/misc/qr_code_barcode.jpg

working qr code for above

相关文章:

python - 无法使用python match()解析字符串-获取错误AttributeError:'NoneType'对象没有属性'group'

python - 太多价值无法解包

java - OpenCV删除扫描工件并旋转内容

android - 使用Zbar或Zxing全方位扫描条形码

javascript - 条形码扫描器反应本机android UI组件未显示

python - 要输入一个变量方程式以python编程

python - 龙卷风-龙卷风中的“全局变量”?

c - 在OpenCV中使用中值过滤器进行图像压缩,但不使用中值过滤器功能

image-processing - 使用任意旋转角度变形透视图

android - 我想要扫描经典和倒置(浅黑暗)QR码。该怎么办?