pyBarcode
を使用して、PNGを生成しています。バーコードの下の数字が右に切り取られています。それを数ピクセル左に微調整するにはどうすればよいですか?pyBarcodeを使用して番号をバーコードの下に配置します。
barcode.writer.BaseWriter(paint_text=my_callback)
そして、このようにしてコールバック関数を定義します:
my_callback(xpos, ypos)
と:
use self.text as text
を
はthe documentationによると、私はこのような何かをする必要があり3210
これを私のDjangoビュー(下)にどのくらい正確に適用できますか?
def barcode(request):
import barcode
from barcode.writer import ImageWriter
from cStringIO import StringIO
def mm2px(mm, dpi=300):
return (mm * dpi)/25.4
class MyImageWriter(ImageWriter):
def calculate_size(self, modules_per_line, number_of_lines, dpi=300):
width = 2 * self.quiet_zone + modules_per_line * self.module_width
height = 1.0 + self.module_height * number_of_lines
if self.text:
height += (self.font_size + self.text_distance)/3
return int(mm2px(width, dpi)), int(mm2px(height, dpi))
f = BarcodeForm(request.GET)
if f.is_valid():
try:
i = StringIO()
bc_factory = barcode.get_barcode_class(f.PYBARCODE_TYPE[f.cleaned_data['barcode_type']])
bc_factory.default_writer_options['quiet_zone'] = 1.0
bc_factory.default_writer_options['text_distance'] = 1.0
bc_factory.default_writer_options['module_height'] = 15.0
bc_factory.default_writer_options['module_width'] = 0.3
bc_factory.default_writer_options['font_size'] = 46
bc = bc_factory(f.cleaned_data['text'], writer=MyImageWriter())
bc.write(i)
return HttpResponse(i.getvalue(), mimetype='image/png')
except Exception, e:
return HttpResponseBadRequest(str(e))
else:
return HttpResponseBadRequest('Missing text or unsupported barcode type: %s' % f.errors)
:
はこの打撃を与えます。 –
@Craig私はしました:https://bitbucket.org/whitie/pybarcode/issue/6/how-to-position-the-text-below-the-barcode – ram1