Exemple #1
0
    def _createBarcode(self):
        if self.barcode["value"] == "":
            return
        if self.barcode["type"] == BARCODE_ANY:
            logger.warning("Usando %s por defecto" % self.typeToName(BARCODE_128))
            self.barcode["type"] = BARCODE_128

        type_ = self.typeToName(self.barcode["type"])
        value_ = self.barcode["value"]
        bg_ = self.barcode["bg"]
        fg_ = self.barcode["fg"]
        if not isinstance(self.barcode["bg"], str):
            bg_ = QColor(self.barcode["bg"]).name()

        if not isinstance(self.barcode["fg"], str):
            fg_ = QColor(self.barcode["fg"]).name()

        margin_ = self.barcode["margin"] / 10
        bar_ = None
        render_options = {
            'module_width': 0.2,
            'module_height': 5,  # 15
            'text_distance': 1.0,  # 5.0
            'background': bg_.lower(),
            'foreground': fg_.lower(),
            'write_text': self.barcode["text"],
            'font_size': 10,
            'text': value_,
            'quiet_zone': margin_,  # 6.5
        }

        try:
            import barcode
            from barcode.writer import ImageWriter
            from PIL.ImageQt import ImageQt
            barC = barcode.get_barcode_class(type_.lower())
            bar_ = barC(u'%s' % value_, writer=ImageWriter())
            b = bar_.render(render_options)
            qim = ImageQt(b)
            self.p = QPixmap.fromImage(qim)
        except Exception:
            print(traceback.format_exc())
            self.barcode["valid"] = False
            self.p = None

        if self.p:
            # Escalar
            if self.barcode["scale"] != 1.0:
                wS_ = self.barcode["x"] * self.barcode["scale"]
                hS_ = self.barcode["y"] * self.barcode["scale"]
                self.p = self.p.scaled(wS_, hS_)

            self.barcode["x"] = self.p.width()
            self.barcode["y"] = self.p.height()

            # FALTA: res , cut y rotation

            self.barcode["valid"] = True
        else:
            self.barcode["valid"] = False
Exemple #2
0
    def _createBarcode(self):
        if self.barcode["value"] == "":
            return
        if self.barcode["type"] == BARCODE_ANY:
            logger.warning("Usando %s por defecto" %
                           self.typeToName(BARCODE_128))
            self.barcode["type"] = BARCODE_128

        type_ = self.typeToName(self.barcode["type"])
        value_ = self.barcode["value"]
        bg_ = self.barcode["bg"]
        fg_ = self.barcode["fg"]
        if not isinstance(self.barcode["bg"], str):
            bg_ = QColor(self.barcode["bg"]).name()

        if not isinstance(self.barcode["fg"], str):
            fg_ = QColor(self.barcode["fg"]).name()

        margin_ = self.barcode["margin"] / 10
        bar_ = None
        render_options = {
            'module_width': 0.2,
            'module_height': 5,  # 15
            'text_distance': 1.0,  # 5.0
            'background': bg_.lower(),
            'foreground': fg_.lower(),
            'write_text': self.barcode["text"],
            'font_size': 10,
            'text': value_,
            'quiet_zone': margin_,  # 6.5
        }

        try:
            from barcode.writer import ImageWriter
            from PIL.ImageQt import ImageQt
            barC = barcode.get_barcode_class(type_.lower())
            bar_ = barC(u'%s' % value_, writer=ImageWriter())
            b = bar_.render(render_options)
            qim = ImageQt(b)
            self.p = QPixmap.fromImage(qim)
        except Exception:
            print(traceback.format_exc())
            self.barcode["valid"] = False
            self.p = None

        if self.p:
            # Escalar
            if self.barcode["scale"] != 1.0:
                wS_ = self.barcode["x"] * self.barcode["scale"]
                hS_ = self.barcode["y"] * self.barcode["scale"]
                self.p = self.p.scaled(wS_, hS_)

            self.barcode["x"] = self.p.width()
            self.barcode["y"] = self.p.height()

            # FALTA: res , cut y rotation

            self.barcode["valid"] = True
        else:
            self.barcode["valid"] = False
Exemple #3
0
    def _createBarcode(self):
        if self.barcode["value"] == "":
            return
        if self.barcode["type"] == BARCODE_ANY:
            logger.warning("Usando %s por defecto" % self.typeToName(BARCODE_128))
            self.barcode["type"] = BARCODE_128

        type_ = self.typeToName(self.barcode["type"])
        value_ = self.barcode["value"]
        bg_ = self.barcode["bg"]
        fg_ = self.barcode["fg"]
        if not isinstance(self.barcode["bg"], str):
            bg_ = QColor(self.barcode["bg"]).name()

        if not isinstance(self.barcode["fg"], str):
            fg_ = QColor(self.barcode["fg"]).name()

        margin_ = self.barcode["margin"] / 10
        
        render_options = {}
        render_options['module_width'] = 0.6
        render_options['module_height'] = 10
        render_options['background'] = bg_.lower()
        render_options['foreground'] = fg_.lower()
        render_options['font_size'] = 8
        render_options['write_text'] = self.barcode["text"]
        render_options['text_distance'] = 35
        render_options['quiet_zone'] = margin_ 
        if self.barcode["text"]:
            render_options['text'] = value_  
        else:
            render_options['text'] = " "
            
        
        
        
        
        
        import barcode
        from barcode.writer import ImageWriter
        from PyQt5.QtSvg import QSvgRenderer

        barC = barcode.get_barcode_class(type_.lower())
        try:
            bar_ = barC(u'%s' % value_)
        except Exception:
            bar_ = barC('000000000000')
            
        svg = bar_.render(render_options)
        xml_svg = load2xml(svg.decode("utf-8"))
        svg_w = (3.779 * float(xml_svg.get("width")[0:6])) 
        svg_h = (3.779 * float(xml_svg.get("height")[0:6]))
        self.p = QPixmap(svg_w, svg_h)
        render = QSvgRenderer(svg)
        self.p.fill(QtCore.Qt.transparent)
        painter = Qt.QPainter(self.p)
        render.render(painter, QRectF(0,0,svg_w * 3.4 , svg_h * 3.4))

        if self.p.isNull():
            self.barcode["valid"] = False
        else:
            
            if self.barcode["scale"] != 1.0:
                wS_ = self.barcode["x"] * self.barcode["scale"]
                hS_ = self.barcode["y"] * self.barcode["scale"]
                self.p = self.p.scaled(wS_, hS_)

            self.barcode["x"] = self.p.width()
            self.barcode["y"] = self.p.height()

            self.barcode["valid"] = True