【问题标题】:Cannot scan QR code using zbarzbar无法扫描二维码
【发布时间】:2016-06-29 22:10:03
【问题描述】:

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)

【问题讨论】:

    标签: python opencv zbar


    【解决方案1】:

    不确定实际问题是什么,但我能够使用 Pil(或 Pillow)而不是 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
    

    【讨论】:

    • 谢谢,但我正在处理视频流,所以我需要用 cv2 解决这个问题
    • @abea 这很公平,但也许首先尝试使用与 Pil(low) 一起使用的 QR,这样你就知道你应该得到结果。我尝试使用通过谷歌搜索“QR 图像样本”找到的几张图片,前 2 或 3 张根本不起作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 1970-01-01
    相关资源
    最近更新 更多