|  | # -*- coding: utf-8 -*-
from django.conf import settings
from PIL import Image, ImageEnhance
from pyzbar import pyzbar
# The zbar DLLs are included with the Windows Python wheels.
# On other operating systems, you will need to install the zbar shared library.
#
# Mac OS X:
# brew install zbar
#
# Linux:
# sudo apt-get install libzbar0
#
# Install this Python wrapper; use the second form to install dependencies of the command-line scripts:
# pip install pyzbar
def zbar(path):
    img = Image.open(path)
    # img = ImageEnhance.Brightness(img).enhance(2.0)  # 增加亮度
    # img = ImageEnhance.Sharpness(img).enhance(17.0)  # 锐利化
    # img = ImageEnhance.Contrast(img).enhance(4.0)  # 增加对比度
    # img = img.convert('L')  # 灰度化
    # img.show()
    barcodes = pyzbar.decode(img)
    # for barcode in barcodes:
    #     barcodeData = barcode.data.decode("utf-8")
    #     print(barcodeData)
    return [bar.data.decode('utf-8').strip() for bar in barcodes]
def test_zbar():
    # In[4]: test_zbar()
    # [u'189415']
    print(zbar(settings.TESTING_ZBAR))
if __name__ == '__main__':
    test_zbar()
 |