Summary of recognition of two-dimensional codes under python

Posted by rock_xl1 on Sat, 15 Jun 2019 01:58:18 +0200

Recently, two-dimensional code image recognition has been used in the project. Under python, two-dimensional code recognition has three main modules: zbar, zbarlight and zxing.

1. Use of three modules:

#-*-coding=utf-8-*-
import os 
import logging
import zbar 
from PIL import Image 
import zxing
import random
import zbarlight

logger=logging.getLogger(__name__)
if not logger.handlers :logging.basicConfig(level=logging.INFO)
DEBUG= (logging.getLevelName(logger.getEffectiveLevel())=='DEBUG')  


def ocr_qrcode_zbar(filename):
    img=Image.open(filename)
    width, height = img.size
    raw = img.tobytes()
    
    scanner = zbar.ImageScanner()
    scanner.parse_config('enable')        
    #Converting images into data
    zarimage = zbar.Image(width, height, 'Y800', raw)    
    #Scanner scans
    scanner.scan(zarimage)
    
    data = '' 
    for symbol in zarimage:
        # Some useful processing of the results
        data += symbol.data 
    if data:
        logger.debug(u'Recognition of two-dimensional codes:%s,content: %s' %(filename ,data))
    else:
        logger.error(u'Distinguish zbar Two-dimensional code error:%s' %(filename))
        img.save('%s-zbar.jpg' %filename)  
    return data 

def ocr_qrcode_zbarlight(filename):
    img=Image.open(filename)

    width, height = img.size
    raw = img.tobytes()
        
    #Converting images into data
    data = zbarlight.qr_code_scanner(raw, width, height)    

    if data:
        logger.debug(u'Recognition of two-dimensional codes:%s,content: %s' %(filename ,data))
    else:
        logger.error(u'Distinguish zbarlight Two-dimensional code error:%s' %(filename))
        img.save('%s-zbar.jpg' %filename)  
    return data 

def ocr_qrcode_zxing(filename):
    #Generate temporary files in the current directory to avoid java Path Problem
    img= Image.open(filename)
    ran= int(random.random()*100000)
    img.save('%s%s.jpg' %(os.path.basename(filename).split('.')[0],ran))
    zx = zxing.BarCodeReader()
    data =''
    zxdata = zx.decode('%s%s.jpg' %(os.path.basename(filename).split('.')[0],ran))
    #Delete temporary files
    os.remove('%s%s.jpg' %(os.path.basename(filename).split('.')[0],ran))
    if zxdata:
        logger.debug(u'zxing Recognition of two-dimensional codes:%s,content: %s' %(filename ,zxdata.data))
        data = zxdata.data
    else:
        logger.error(u'Distinguish zxing Two-dimensional code error:%s' %(filename))
        img.save('%s-zxing.jpg' %filename)  
    return data

    
if __name__ == '__main__': 
    filename =r'D:\python\00025328.jpg'
 
    #zbarTwo-Dimensional Code Recognition
    ltext = ocr_qrcode_zbar(filename)
    logger.info( u'[%s]Zbar Two-Dimensional Code Recognition:[%s]!!!' %(filename,ltext))
    print ltext
    
    #zbarlight Two-Dimensional Code Recognition
    ltext = ocr_qrcode_zbarlight(filename)
    logger.info( u'[%s]Zxing Two-Dimensional Code Recognition:[%s]!!!' %(filename,ltext))
    print ltext 

    #zxing Two-Dimensional Code Recognition
    ltext = ocr_qrcode_zxing(filename)
    logger.info( u'[%s]Zxing Two-Dimensional Code Recognition:[%s]!!!' %(filename,ltext))
    print ltext

 

2. Use comparison

1. The ZBar and zbarlight kernels are the same. They are all compiled and loaded by dll Based on zbar.

2. The use of zbarlight is simpler than that of zbar, but a little bit of encapsulation has been made on the basis of zbar.

3. zxing is a python branch based on the zxing core of java. Its principle is to call javaw to load the core.jar package of zxing and get the output result.

The debugging of zxing is a big pit. The information on the Internet is outdated in one way or another, and the dependence of zxing on java is more serious. This blog http://www.cnblogs.com/oucsheep/p/6269813.html Relatively clear, but it is estimated that beginners will look tired.

 

 

3. Summary:

1. The picture of the project originates from the scanned paper document (qrcode). The actual situation is relatively complex. There are many possibilities, such as printing offset, overlap with other words, picture distortion, color ribbon deficiency, resulting in picture incompleteness and so on. Despite a series of image processing, at present, the three packages are not very fault-tolerant.

2. But... The "sweep" in Weixin can be recognized normally. It should be that Weixin's two-dimensional code recognition has its own unique algorithm. Unfortunately, Weixin does not have an open interface, nor does it find relevant information.

3, Alipay's "sweep", recognition rate slightly behind WeChat, but also more than several open source packages.

4. There are some paid APIs on the internet, and the recognition rate is also general. It is suspected that they are based on the secondary encapsulation of zxing and zbar.

Looking forward to master cracking or stripping the algorithm of Weixin, also welcome commercial api developers to communicate (can pay), contact QQ 16906913, thank you.

Above...

Topics: Python Java QRCode