2011-07-06 30 views
4

我使用pyBarcode生成PNG,並且條形碼下方的數字在右側被截斷。我如何推動它留下幾個像素?將條形碼置於條形碼下方,使用pyBarcode

the number is getting cut off

根據the documentation我需要做這樣的事情:

barcode.writer.BaseWriter(paint_text=my_callback) 

而且這樣定義的回調:

my_callback(xpos, ypos) 

和:

use self.text as text 

我該如何將所有這些應用到我的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) 
+0

你可以作爲對pyBarcode筆者的錯誤報告。 –

+0

@Craig我做過了:https://bitbucket.org/whitie/pybarcode/issue/6/how-to-position-the-text-below-the-barcode – ram1

回答

1

編輯:在回答之後,我注意到你有一個工廠的設置的quiet_zone1.0。將它改回6.5,我想它會顯得很好。

編輯2:我誤解了你遇到的確切問題。

無論出於何種原因,pyBarcode的作者將文本居中置於條形碼的中間位置。當渲染方法調用_paint_text()時,它傳入xpos/2,它將其設置在條形碼的中間。我猜這對他使用的默認字體沒問題,但是當你增加字體時,它不再適合。

相反,我可以通過覆蓋_paint_text()方法將它放在左側。在下面的最後一行中,變量pos只是一個包含(x,y)座標的元組,告訴PIL在哪裏繪製條形碼上的文本。所以我確定x與條形碼排在一起。如果你需要對齊,你可以使用xpos變量來獲得它所需的位置。

這給一個鏡頭:

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)) 

    def _paint_text(self, xpos, ypos): 
     # this should align your font to the left side of the bar code: 
     xpos = self.quiet_zone 
     pos = (mm2px(xpos, self.dpi), mm2px(ypos, self.dpi)) 
     font = ImageFont.truetype(FONT, self.font_size) 
     self._draw.text(pos, self.text, font=font, fill=self.foreground) 
+0

[截圖。](http:// dl .dropbox.com/u/26816257/barcode_quiet.png)隨着'quiet_zone'的增加,條形碼下方的數字變得可見,但它偏離中心。我需要這個看起來像一個正常的條形碼。 – ram1

+0

我明白了。上面的檢查,我編輯了我的答案 - 希望它可以解決您的問題。 –

+0

我添加了_paint_text功能,但現在條形碼完全不出現。我也嘗試添加:'import os'和'from PIL import Image,ImageDraw,ImageFont'和'PATH = os.path.dirname(os.path.abspath(__ file __))'以及'FONT = os。 path.join(PATH,'DejaVuSansMono.ttf')'per [this](https://bitbucket.org/whitie/pybarcode/src/1400018306e5/barcode/writer.py) - 但仍然沒有顯示條形碼。思考? – ram1