我需要使用Python / Django创建Code128条形码,这些条形码必须嵌入HTML文档中。
我不想在磁盘上制作任何临时(或缓存)文件。这就是我想将它们嵌入数据URI方案的原因。
结果必须是这样的:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA
AAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO
9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot">
你能给我推荐一个简单的方法吗?
现在我使用ReportLab创建这样的条形码并将它们嵌入到PDF文件中,但我不知道如何将它们导出为数据URI方案。如果这是推荐的方法。
答案 0 :(得分:12)
这是我自己的解决方案:
from base64 import b64encode
from reportlab.lib import units
from reportlab.graphics import renderPM
from reportlab.graphics.barcode import createBarcodeDrawing
from reportlab.graphics.shapes import Drawing
def get_barcode(value, width, barWidth = 0.05 * units.inch, fontSize = 30, humanReadable = True):
barcode = createBarcodeDrawing('Code128', value = value, barWidth = barWidth, fontSize = fontSize, humanReadable = humanReadable)
drawing_width = width
barcode_scale = drawing_width / barcode.width
drawing_height = barcode.height * barcode_scale
drawing = Drawing(drawing_width, drawing_height)
drawing.scale(barcode_scale, barcode_scale)
drawing.add(barcode, name='barcode')
return drawing
def get_image():
barcode = get_barcode(value = '01234567890', width = 600)
data = b64encode(renderPM.drawToString(barcode, fmt = 'PNG'))
print '<img src="data:image/png;base64,{0}">'.format(data)
此外,您还可以将条形码旋转90°:
def get_barcode_rotated(value, width, barWidth = 0.05 * units.inch, fontSize = 30, humanReadable = True):
barcode = createBarcodeDrawing('Code128', value = value, barWidth = barWidth, fontSize = fontSize, humanReadable = humanReadable)
drawing_width = width
barcode_scale = drawing_width / barcode.width
drawing_height = barcode.height * barcode_scale
drawing = Drawing(drawing_width, drawing_height)
drawing.scale(barcode_scale, barcode_scale)
drawing.add(barcode, name='barcode')
drawing_rotated = Drawing(drawing_height, drawing_width)
drawing_rotated.rotate(90)
drawing_rotated.translate(0, -drawing_height)
drawing_rotated.add(drawing, name='drawing')
return drawing_rotated
以下是一个示例结果:
答案 1 :(得分:5)
这应该可以解决问题。我使用Code128 python模块生成条形码。
<强>码强>
from Code128 import Code128
from base64 import b64encode
val = "9782212110708"
Code128().getImage(val, path="./")
data = b64encode(open(val + '.png').read())
print '<img src="data:image/png;base64,{0}">'.format(data)
<强>输出强>
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAIMAAAAyAQAAAABXcFUb
AAAAjklEQVR4nGP8r2v6J/ihYotFKrs5qx9H2TwmBnQwKjIqMnRFGP+jCTzErkvS6IR80Yu5iScU
GBgYGFgYGP4b3j6t9Xn+ZG4jA6gIAxtDos26ROHFcF2M+32/XPjLwPCX8QLMnAOfXyz4xcDA8B+m
63/djHUCHxkYfkEt+///v8zHJg6GBpbi4/L///9/AADHAS8/nZ4QEQAAAABJRU5ErkJggg==">
<强>更新强>
在评论中有一个很好的建议来修改Code128模块,这样它就不必将图像保存到文件系统中。您可以更改Code128,使其返回图像对象,而不是将其保存到文件中。您只需要更改一行代码即可实现此目的。从以下地址更改第162行:
im.save(path+value+"."+lower(extension), upper(extension))
为:
return im
答案 2 :(得分:1)
您是否尝试使用自己的图像数据表示的每个符号动态组装条形码?这是一个例子:
&LT; IMG
ALT = “105,{出发赛}” 标题= “105,{出发赛}” SRC = “数据:图像/ PNG; BASE64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAABAQMAAAAy + cYDAAAABlBMVEUAAAD /// + l2Z / dAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAADklEQVQIHQEDAPz / ACxgALsAjajKmzkAAAAASUVORK5CYII =” 宽度= “30”高度= “60” &GT;&LT; IMG
alt =“00,A:{space},B:{space}”title =“00,A:{space},B:{space}”src =“data:image / png; base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAABAQMAAAAy + cYDAAAABlBMVEUAAAD // / + l2Z / dAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAADklEQVQIHQEDAPz / ACZgAK8Ah / KrWu0AAAAASUVORK5CYII =“width =”30“height =”60“&gt;&lt; img
alt =“98,A:{Escape B},B:{Escape A}”title =“98,A:{Escape B},B:{Escape A}”src =“data:image / png; base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAABAQMAAAAy + cYDAAAABlBMVEUAAAD /// + l2Z / dAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAADklEQVQIHQEDAPz / AAugALkArBSbglwAAAAASUVORK5CYII =“width =”30“height =”60“&gt;&lt; img
alt =“21,A:{5},B:{5}”title =“21,A:{5},B:{5}”src =“data:image / png; base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAABAQMAAAAy + cYDAAAABlBMVEUAAAD // / + l2Z / dAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAADklEQVQIHQEDAPz / ACNgAKkAhD / i5lYAAAAASUVORK5CYII =“width =”30“height =”60“&gt;&lt; img
alt =“14,A:{。},B:{。}”title =“14,A:{。},B:{。}”src =“data:image / png; base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAABAQMAAAAy + cYDAAAABlBMVEUAAAD // / + l2Z / dAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAADklEQVQIHQEDAPz / AGYgAO8Ah9WahQgAAAAASUVORK5CYII =“width =”30“height =”60“&gt;&lt; img
alt =“00,A:{space},B:{space}”title =“00,A:{space},B:{space}”src =“data:image / png; base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAABAQMAAAAy + cYDAAAABlBMVEUAAAD // / + l2Z / dAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAADklEQVQIHQEDAPz / ACZgAK8Ah / KrWu0AAAAASUVORK5CYII =“width =”30“height =”60“&gt;&lt; img
alt =“14,A:{。},B:{。}”title =“14,A:{。},B:{。}”src =“data:image / png; base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAABAQMAAAAy + cYDAAAABlBMVEUAAAD // / + l2Z / dAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAADklEQVQIHQEDAPz / AGYgAO8Ah9WahQgAAAAASUVORK5CYII =“width =”30“height =”60“&gt;&lt; img
ALT = “64,A:{NUL},B:{}" title=" 64,A:{NUL},B:{
}” SRC = “数据:图像/ PNG; BASE64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAABAQMAAAAy + cYDAAAABlBMVEUAAAD /// + l2Z / dAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAADklEQVQIHQEDAPz / AF5gAR8Av0A69KoAAAAASUVORK5CYII =” 宽度=“30 “height =”60“&gt;&lt; img
alt =“95,A:{US},B:{DEL}”title =“95,A:{US},B:{DEL}”src =“data:image / png; base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAABAQMAAAAy + cYDAAAABlBMVEUAAAD // / + l2Z / dAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAADklEQVQIHQEDAPz / AELgAWcBIz + IUR4AAAAASUVORK5CYII =“width =”30“height =”60“&gt;&lt; img
alt =“07,A:{'},B:{'}”title =“07,A:{'},B:{'}”src =“data:image / png; base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAABAQMAAAAy + cYDAAAABlBMVEUAAAD // / + l2Z / dAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAADklEQVQIHQEDAPz / AGdgATEAyJ / 9LboAAAAASUVORK5CYII =“width =”30“height =”60“&gt;&lt; img
alt =“74,A:{LF},B:{j}”title =“74,A:{LF},B:{j}”src =“data:image / png; base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAABAQMAAAAy + cYDAAAABlBMVEUAAAD // / + l2Z / dAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAADklEQVQIHQEDAPz / AHmgAZUBGuCD0Y8AAAAASUVORK5CYII =“width =”30“height =”60“&gt;&lt; img
alt =“101,A:{FNC 4},B:{Switch A},C:{Switch A}”title =“101,A:{FNC 4},B:{Switch A},C:{Switch A }” SRC = “数据:图像/ PNG; BASE64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAABAQMAAAAy + cYDAAAABlBMVEUAAAD /// + l2Z / dAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAADklEQVQIHQEDAPz / ABQgAEsANWhuDt4AAAAASUVORK5CYII =” 宽度= “30” HEIGHT = “60” &GT;&LT; IMG
alt =“25,A:{9},B:{9}”title =“25,A:{9},B:{9}”src =“data:image / png; base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAABAQMAAAAy + cYDAAAABlBMVEUAAAD // / + l2Z / dAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAADklEQVQIHQEDAPz / ABpgAJcAex9glyQAAAAASUVORK5CYII =“width =”30“height =”60“&gt;&lt; img
alt =“12,A:{,},B:{,}”title =“12,A:{,},B:{,}”src =“data:image / png; base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAABAQMAAAAy + cYDAAAABlBMVEUAAAD // / + l2Z / dAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAADklEQVQIHQEDAPz / AExgAPsArevKOWsAAAAASUVORK5CYII =“width =”30“height =”60“&gt;&lt; img
ALT = “106,{停止}” 标题= “106,{停止}” SRC = “数据:图像/ PNG; BASE64,iVBORw0KGgoAAAANSUhEUgAAAA0AAAABAQMAAAA / 57ZEAAAABlBMVEUAAAD /// + l2Z / dAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAADklEQVQIHQEDAPz / ADigARMA2dpJzT8AAAAASUVORK5CYII =” 宽度= “30” 高度= “60” &GT;
009821140014649507749
这代表与您的示例相同的条形码,但不需要中间图形,占用大约三分之一的空间。用于构成图像的数据可以从数组访问,并用于使用Javascript或其他客户端或服务器端应用程序填充目标HTML。
您可以在http://notionovus.com/blog/code-128-barcode/
找到完整的Code 128符号列表我不确定轮换。我还没试过。
答案 3 :(得分:0)
对于其他googlers:建议的解决方案不依赖于ReportLab,我们可以在我们的html模板中从其base64表示中包含条形码,因为我们已经使用另一个库创建了图像:
data_uri = open("sample.png", "rb").read().encode("base64").replace("\n", "")
# HTML Image Element
img_tag = '<img alt="" src="data:image/png;base64,{0}">'.format(data_uri)
print img_tag
# CSS Background Image
css = 'background-image: url(data:image/png;base64,{0});'.format(data_uri)
print css