def generateBarcodeImage(self, barcode_type, data, REQUEST=None):
  # huBarcode's DataMatrix support has limitation for data size.
  # huBarcode's QRCode support is broken.
  # more 1-D barcode types can be added by pyBarcode library.
  barcode_type = barcode_type.lower()
  if barcode_type == 'datamatrix':
    from subprocess import Popen, PIPE
    process = Popen(['dmtxwrite'],
                     stdin=PIPE,
                     stdout=PIPE,
                     stderr=PIPE,
                     close_fds=True)
    output, _ = process.communicate(input=data)
  elif barcode_type == 'ean13':
    from hubarcode.ean13 import EAN13Encoder 
    encoder = EAN13Encoder(data)
    output = encoder.get_imagedata()
  elif barcode_type == 'code128':
    from hubarcode.code128 import Code128Encoder 
    encoder = Code128Encoder(data)
    encoder.text = '' # get barcode image only
    output = encoder.get_imagedata()
  elif barcode_type == 'qrcode':
    import qrcode
    from cStringIO import StringIO
    fp = StringIO()
    img = qrcode.make(data)
    img.save(fp, 'png')
    fp.seek(0)
    output = fp.read()
  else:
    raise NotImplementedError('barcode_type=%s is not supported' % barcode_type)
  if REQUEST is not None:
    REQUEST.RESPONSE.setHeader('Content-Type', 'image/png')
  return output
Beispiel #2
0
def generate(codec, text, bar_width=3, options=code128_default_options):
    ''' 生成条码

        codec: 'ean13', 'code128'等
        text: 文本串
        bar_width:
        options:

        返回一个wx.Bitmap
    '''
    n = codec.upper()
    if n == 'CODE128':
        enc = Code128Encoder(text, options=options) # (176, 528) to (57, 405)
    elif n== 'EAN13':
        enc = EAN13Encoder(text) # (169, 339) to (65, 185)
    else:
        raise ValueError('%s not yet supported' % codec)

    pilimage = resize(enc.get_pilimage(bar_width), codec)
    return PilImageToWxBitmap(pilimage)
Beispiel #3
0
def my_handler(sender, **kwargs):
    #raise IOError(kwargs)
    instance = kwargs.get('instance')
    #    raise IOError(instance)
    if instance.barcode_EAN13:
        encoder = EAN13Encoder(instance.barcode_EAN13)
        encoder.save(MEDIA_ROOT + '/barcodes/' + 'ean13-' + str(instance.id) +
                     '.png')
        b = MyBarcode(product=instance,
                      barcode='/barcodes/' + 'ean13-' + str(instance.id) +
                      '.png')
        b.save()
    if instance.barcode_EAN128:
        encoder = Code128Encoder(instance.barcode_EAN128)
        encoder.save(MEDIA_ROOT + '/barcodes/' + 'ean128-' + str(instance.id) +
                     '.png')
        b = MyBarcode(product=instance,
                      barcode='/barcodes/' + 'ean128-' + str(instance.id) +
                      '.png')
        b.save()
Beispiel #4
0
"""Example code for ean13 library"""
__revision__ = "$Revision$"

from hubarcode.ean13 import EAN13Encoder
import sys
import logging

logging.getLogger("ean13").setLevel(logging.DEBUG)
logging.getLogger("ean13").addHandler(logging.StreamHandler(sys.stdout))

if __name__ == "__main__":
    encoder = EAN13Encoder(sys.argv[1])
    encoder.save("test.png")