improve qrcode templatetag

This commit is contained in:
wolflu05 2024-07-15 10:45:55 +02:00
parent 650787af55
commit cb105a66fa
No known key found for this signature in database
GPG Key ID: 9099EFC7C5EB963C
2 changed files with 47 additions and 25 deletions

View File

@ -3,12 +3,20 @@
from django import template
import barcode as python_barcode
import qrcode as python_qrcode
import qrcode.constants as ECL
from qrcode.main import QRCode
import report.helpers
register = template.Library()
QR_ECL_LEVEL_MAP = {
'L': ECL.ERROR_CORRECT_L,
'M': ECL.ERROR_CORRECT_M,
'Q': ECL.ERROR_CORRECT_Q,
'H': ECL.ERROR_CORRECT_H,
}
def image_data(img, fmt='PNG'):
"""Convert an image into HTML renderable data.
@ -22,36 +30,44 @@ def image_data(img, fmt='PNG'):
def qrcode(data, **kwargs):
"""Return a byte-encoded QR code image.
kwargs:
fill_color: Fill color (default = black)
back_color: Background color (default = white)
version: Default = 1
box_size: Default = 20
border: Default = 1
Arguments:
data: Data to encode
Keyword Arguments:
version: QR code version, (None to auto detect) (default = None)
error_correction: Error correction level (L: 7%, M: 15%, Q: 25%, H: 30%) (default = 'Q')
box_size: pixel dimensions for one black square pixel in the QR code (default = 20)
border: count white QR square pixels around the qr code, needed as padding (default = 1)
optimize: data will be split into multiple chunks of at least this length using different modes (text, alphanumeric, binary) to optimize the QR code size. Set to `0` to disable. (default = 1)
format: Image format (default = 'PNG')
fill_color: Fill color (default = "black")
back_color: Background color (default = "white")
Returns:
base64 encoded image data
"""
# Construct "default" values
params = {'box_size': 20, 'border': 1, 'version': 1}
# Construct params
fill_color = kwargs.pop('fill_color', 'black')
back_color = kwargs.pop('back_color', 'white')
image_format = kwargs.pop('format', 'PNG')
optimize = kwargs.pop('optimize', 1)
format = kwargs.pop('format', 'PNG')
params.update(**kwargs)
qr = python_qrcode.QRCode(**params)
qr.add_data(data, optimize=20)
qr.make(fit=True)
# Construct QR code params
qr = QRCode(**{
'box_size': 20,
'border': 1,
'version': None,
**kwargs,
'error_correction': QR_ECL_LEVEL_MAP[kwargs.get('error_correction', 'Q')],
})
qr.add_data(data, optimize=optimize)
qr.make(fit=False) # if version is None, it will automatically use fit=True
qri = qr.make_image(fill_color=fill_color, back_color=back_color)
# Render to byte-encoded image
return image_data(qri, fmt=format)
return image_data(qri, fmt=image_format)
@register.simple_tag()

View File

@ -25,12 +25,18 @@ const tags: Tag[] = [
description: 'Generate a QR code image',
args: ['data'],
kwargs: {
fill_color: 'Fill color (default = black)',
back_color: 'Background color (default = white)',
version: 'Version (default = 1)',
box_size: 'Box size (default = 20)',
border: 'Border width (default = 1)',
format: 'Format (default = PNG)'
version: 'QR code version, (None to auto detect) (default = None)',
error_correction:
"Error correction level (L: 7%, M: 15%, Q: 25%, H: 30%) (default = 'Q')",
box_size:
'pixel dimensions for one black square pixel in the QR code (default = 20)',
border:
'count white QR square pixels around the qr code, needed as padding (default = 1)',
optimize:
'data will be split into multiple chunks of at least this length using different modes (text, alphanumeric, binary) to optimize the QR code size. Set to `0` to disable. (default = 1)',
format: "Image format (default = 'PNG')",
fill_color: 'Fill color (default = "black")',
back_color: 'Background color (default = "white")'
},
returns: 'base64 encoded qr code image data'
},