示例#1
0
 def draw(self):
     if 0:
         assert isinstance(self.canv, pdfgen.canvas.Canvas)
     if (self.filename, self.tiff_page, self.bbox) in self.cache:
         img = self.cache[(self.filename, self.tiff_page, self.bbox)]
     else:
         img = StringIO.StringIO(
             image.get_pbm(
                 image.get_a1_from_tiff(
                     self.filename, self.tiff_page,
                     self.rotated if self.rotated else False)))
         img = Image.open(img).crop(self.bbox)
         self.cache[(self.filename, self.bbox)] = img
     self.canv.drawInlineImage(img, 0, 0, self.width, self.height)
     self.canv.setStrokeColorRGB(0.6, 0.6, 0.6)
     self.canv.line(0, 0, self.available_width, 0)
     self.canv.line(0, self.height, self.available_width, self.height)
示例#2
0
 def draw(self):
     if 0:
         assert isinstance(self.canv, pdfgen.canvas.Canvas)
     if (self.filename, self.tiff_page, self.bbox) in self.cache:
         img = self.cache[(self.filename, self.tiff_page, self.bbox)]
     else:
         img = StringIO.StringIO(
             image.get_pbm(
                 image.get_a1_from_tiff(self.filename, self.tiff_page, self.rotated if self.rotated else False)
             )
         )
         img = Image.open(img).crop(self.bbox)
         self.cache[(self.filename, self.bbox)] = img
     self.canv.drawInlineImage(img, 0, 0, self.width, self.height)
     self.canv.setStrokeColorRGB(0.6, 0.6, 0.6)
     self.canv.line(0, 0, self.available_width, 0)
     self.canv.line(0, self.height, self.available_width, self.height)
示例#3
0
def scan(surface, matrix, x, y, width, height, btype="CODE128", kfill=False):
    x0, y0 = matrix.transform_point(x, y)
    x1, y1 = matrix.transform_point(x + width, y + height)

    # Bounding box ...
    x = min(x0, x1)
    y = min(y0, y1)
    width = max(x0, x1) - x
    height = max(y0, y1) - y

    x, y, width, height = int(x), int(y), int(width), int(height)
    # Round the width to multiple of 4 pixel, so that the stride will
    # be good ... hopefully
    width = width - width % 4 + 4

    # a1 surface for kfill algorithm
    a1_surface = cairo.ImageSurface(cairo.FORMAT_A1, width, height)

    cr = cairo.Context(a1_surface)
    cr.set_operator(cairo.OPERATOR_SOURCE)
    cr.set_source_surface(surface, -x, -y)
    cr.paint()

    if kfill:
        pxpermm = (matrix[0] + matrix[3]) / 2
        barwidth = pxpermm * defs.code128_barwidth
        barwidth = int(round(barwidth))

        if barwidth <= 3:
            return

        if barwidth > 6:
            barwidth = 6

        image.kfill_modified(a1_surface, barwidth)

    pbm = image.get_pbm(a1_surface)
    tmp = tempfile.mktemp(suffix='.png', prefix='sdaps-zbar-')
    f = open(tmp, 'wb')
    f.write(pbm)
    f.close()

    # Is the /dev/stdin sufficiently portable?
    proc = subprocess.Popen(
        ['zbarimg', '-q', '-Sdisable',
         '-S%s.enable' % btype.lower(), tmp],
        stdout=subprocess.PIPE)
    stdout, stderr = proc.communicate()
    os.unlink(tmp)

    # The following can be used to look at the images
    #rgb_surface = cairo.ImageSurface(cairo.FORMAT_RGB24, width, height)
    #cr = cairo.Context(rgb_surface)
    #cr.set_operator(cairo.OPERATOR_SOURCE)
    #cr.set_source_rgba(1, 1, 1, 1)
    #cr.paint()
    #cr.set_operator(cairo.OPERATOR_OVER)
    #cr.set_source_rgba(0, 0, 0, 1)
    #cr.mask_surface(a1_surface)
    #global b_count
    #rgb_surface.write_to_png("/tmp/barcode-%03i.png" % b_count)
    #b_count += 1

    if proc.returncode == 4:
        return None

    assert (proc.returncode == 0)
    barcode = stdout.split(b'\n')[0]
    assert barcode.split(b':', 1)[0].replace(
        b'-', b'').lower() == btype.lower().encode('ascii')

    return barcode.split(b':', 1)[1].decode('utf-8')
示例#4
0
文件: barcode.py 项目: sdaps/sdaps
def scan(surface, matrix, x, y, width, height, btype="CODE128", kfill=False):
    x0, y0 = matrix.transform_point(x, y)
    x1, y1 = matrix.transform_point(x + width, y + height)

    # Bounding box ...
    x = min(x0, x1)
    y = min(y0, y1)
    width = max(x0, x1) - x
    height = max(y0, y1) - y

    x, y, width, height = int(x), int(y), int(width), int(height)
    # Round the width to multiple of 4 pixel, so that the stride will
    # be good ... hopefully
    width = width - width % 4 + 4

    # a1 surface for kfill algorithm
    a1_surface = cairo.ImageSurface(cairo.FORMAT_A1, width, height)

    cr = cairo.Context(a1_surface)
    cr.set_operator(cairo.OPERATOR_SOURCE)
    cr.set_source_surface(surface, -x, -y)
    cr.paint()

    if kfill:
        pxpermm = (matrix[0] + matrix[3]) / 2
        barwidth = pxpermm * defs.code128_barwidth
        barwidth = int(round(barwidth))

        if barwidth <= 3:
            return

        if barwidth > 6:
            barwidth = 6

        image.kfill_modified(a1_surface, barwidth)

    pbm = image.get_pbm(a1_surface)
    tmp = tempfile.mktemp(suffix='.png', prefix='sdaps-zbar-')
    f = open(tmp, 'wb')
    f.write(pbm)
    f.close()

    # Is the /dev/stdin sufficiently portable?
    proc = subprocess.Popen(['zbarimg', '-q', '-Sdisable', '-S%s.enable' % btype.lower(), tmp], stdout=subprocess.PIPE)
    stdout, stderr = proc.communicate()
    os.unlink(tmp)

    # The following can be used to look at the images
    #rgb_surface = cairo.ImageSurface(cairo.FORMAT_RGB24, width, height)
    #cr = cairo.Context(rgb_surface)
    #cr.set_operator(cairo.OPERATOR_SOURCE)
    #cr.set_source_rgba(1, 1, 1, 1)
    #cr.paint()
    #cr.set_operator(cairo.OPERATOR_OVER)
    #cr.set_source_rgba(0, 0, 0, 1)
    #cr.mask_surface(a1_surface)
    #global b_count
    #rgb_surface.write_to_png("/tmp/barcode-%03i.png" % b_count)
    #b_count += 1

    if proc.returncode == 4:
        return None

    assert(proc.returncode == 0)
    barcode = stdout.split(b'\n')[0]
    assert barcode.split(b':', 1)[0].replace(b'-', b'').lower() == btype.lower().encode('ascii')

    return barcode.split(b':', 1)[1].decode('utf-8')