From 737cc3176e36d571dffa83d7124c8a21d93322ab Mon Sep 17 00:00:00 2001 From: Romain Porte Date: Tue, 16 May 2017 20:55:30 +0200 Subject: [PATCH 1/4] First implementation of software barcode Actually the hardware barcode implementation is very specific and not generic enough for just adding a `soft_render=True` argument to it. This is a first work that can be improved with other commits, maybe for merging this method in the `barcode` method after some cleanup. The width, height and text_distance were set using empiric print-and-retry tests so that the generated barcode looks nice to the eye (and to the eye of an Android scanner tool. !WARNING! Printing a barcode that is too large in width will result in the printer to go crazy trying to print an image that is too large for it. This may be fixed by raising an exception in the `image` method. --- examples/software_barcode.py | 8 ++++++++ setup.py | 3 ++- src/escpos/escpos.py | 23 +++++++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 examples/software_barcode.py diff --git a/examples/software_barcode.py b/examples/software_barcode.py new file mode 100644 index 0000000..8476bac --- /dev/null +++ b/examples/software_barcode.py @@ -0,0 +1,8 @@ +from escpos.printer import Usb + +# Adapt to your needs +p = Usb(0x0416, 0x5011, profile="POS-5890") + +# Some software barcodes +p.soft_barcode('code128', 'Hello') +p.soft_barcode('code39', '123456') \ No newline at end of file diff --git a/setup.py b/setup.py index c5d77f6..bd4a3ed 100755 --- a/setup.py +++ b/setup.py @@ -115,7 +115,8 @@ setup( 'pyyaml', 'argparse', 'argcomplete', - 'future' + 'future', + 'pyBarcode==0.8b1' ], setup_requires=[ 'setuptools_scm', diff --git a/src/escpos/escpos.py b/src/escpos/escpos.py index 3faba40..d23bb05 100644 --- a/src/escpos/escpos.py +++ b/src/escpos/escpos.py @@ -19,6 +19,9 @@ import qrcode import textwrap import six +import barcode +from barcode.writer import ImageWriter + from .constants import ESC, GS, NUL, QR_ECLEVEL_L, QR_ECLEVEL_M, QR_ECLEVEL_H, QR_ECLEVEL_Q from .constants import QR_MODEL_1, QR_MODEL_2, QR_MICRO, BARCODE_TYPES, BARCODE_HEIGHT, BARCODE_WIDTH from .constants import TXT_ALIGN_CT, TXT_ALIGN_LT, TXT_ALIGN_RT, BARCODE_FONT_A, BARCODE_FONT_B @@ -396,6 +399,26 @@ class Escpos(object): if function_type.upper() == "A": self._raw(NUL) + def soft_barcode(self, barcode_type, data, module_height=5, module_width=0.2, text_distance=1): + image_writer = ImageWriter() + + if barcode_type not in barcode.PROVIDED_BARCODES: + raise BarcodeTypeError( + 'Barcode type {} not supported by software barcode renderer' + .format(barcode_type)) + + barcode_class = barcode.get_barcode_class(barcode_type) + my_code = barcode_class(data, writer=image_writer) + + my_code.write("/dev/null", { + 'module_height': module_height, + 'module_width': module_width, + 'text_distance': text_distance + }) + + image = my_code.writer._image + self.image(image, impl='bitImageColumn') + def text(self, txt): """ Print alpha-numeric text From 22cf6ad00bb89128d626f647960403bf47fbd72f Mon Sep 17 00:00:00 2001 From: Romain Porte Date: Mon, 22 May 2017 20:21:35 +0200 Subject: [PATCH 2/4] Allow users to change impl for soft_barcode --- src/escpos/escpos.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/escpos/escpos.py b/src/escpos/escpos.py index d23bb05..96f8415 100644 --- a/src/escpos/escpos.py +++ b/src/escpos/escpos.py @@ -399,14 +399,18 @@ class Escpos(object): if function_type.upper() == "A": self._raw(NUL) - def soft_barcode(self, barcode_type, data, module_height=5, module_width=0.2, text_distance=1): + def soft_barcode(self, barcode_type, data, impl='bitImageColumn', + module_height=5, module_width=0.2, text_distance=1): + image_writer = ImageWriter() + # Check if barcode type exists if barcode_type not in barcode.PROVIDED_BARCODES: raise BarcodeTypeError( 'Barcode type {} not supported by software barcode renderer' .format(barcode_type)) + # Render the barcode to a fake file barcode_class = barcode.get_barcode_class(barcode_type) my_code = barcode_class(data, writer=image_writer) @@ -416,8 +420,9 @@ class Escpos(object): 'text_distance': text_distance }) + # Retrieve the Pillow image and print it image = my_code.writer._image - self.image(image, impl='bitImageColumn') + self.image(image, impl=impl) def text(self, txt): """ Print alpha-numeric text From d34871243972338846d952ff297d7d435f0a155f Mon Sep 17 00:00:00 2001 From: Romain Porte Date: Mon, 22 May 2017 20:25:51 +0200 Subject: [PATCH 3/4] PEP8 software barcode example --- examples/software_barcode.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/software_barcode.py b/examples/software_barcode.py index 8476bac..2fd3c18 100644 --- a/examples/software_barcode.py +++ b/examples/software_barcode.py @@ -1,8 +1,9 @@ from escpos.printer import Usb + # Adapt to your needs p = Usb(0x0416, 0x5011, profile="POS-5890") # Some software barcodes p.soft_barcode('code128', 'Hello') -p.soft_barcode('code39', '123456') \ No newline at end of file +p.soft_barcode('code39', '123456') From b963c5668b8279a3943a827b0f8045a2e8f851af Mon Sep 17 00:00:00 2001 From: Romain Porte Date: Sun, 11 Jun 2017 10:06:57 +0200 Subject: [PATCH 4/4] Using viivakoodi instead of pyBarcode --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index bd4a3ed..c2460da 100755 --- a/setup.py +++ b/setup.py @@ -116,7 +116,7 @@ setup( 'argparse', 'argcomplete', 'future', - 'pyBarcode==0.8b1' + 'viivakoodi>=0.8' ], setup_requires=[ 'setuptools_scm',