13 ZINT_RSS_EXPANDED_CODE = 31
14 ZINT_BINARY = "zint" # If available in PATH
16 POSTSCRIPT = 'postscript'
18 TEST_FILE = "../../../src/com/google/zxing/oned/rss/RSSExpandedBlackBox3TestCase.java"
22 subprocess.Popen("zint", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
24 print >> sys.stderr, "zint not installed. Go to http://www.zint.org.uk/ and install it"
28 def check_postscript():
33 GENERATORS.append(ZINT)
34 if check_postscript():
35 GENERATORS.append(POSTSCRIPT)
37 def generate_image_zint(image_filename, barcode):
38 braces_barcode = barcode.replace("(","[").replace(")","]")
39 os.system('%s -o %s -b %s -d "%s"' % (ZINT_BINARY, image_filename, ZINT_RSS_EXPANDED_CODE, braces_barcode.replace('"','\\"')))
41 def generate_image_postscript(image_filename, barcode):
42 hashname = "cache/%s.png" % hashlib.new("md5", barcode).hexdigest()
43 # If it exists in the cache, don't download it
44 if os.path.exists(hashname):
45 content = open(hashname).read()
46 open(image_filename,'w').write(content)
49 # We tried use the Python, Perl and LaTeX bindings without success :-(
50 baseurl = "http://www.terryburton.co.uk/barcodewriter/generator/"
51 encoded = urllib.urlencode({
52 "encoder" : "rssexpanded",
55 "translate_x" : "50", "translate_y" : "50",
56 "scale_x" : "2", "scale_y" : "2",
57 "rotate" : "0", "submit" : "Make Barcode"
59 web_urlobject = urllib2.urlopen(baseurl, data = encoded)
60 web_content = web_urlobject.read()
61 png_url = web_content.split('">PNG</a>')[0].split('<a href="')[-1]
62 if not png_url.startswith("tmp"):
63 raise Exception("There was an error processing barcode %s in postscript" % barcode)
64 full_url = baseurl + png_url
65 png_content = urllib2.urlopen(full_url).read()
66 open(hashname,'w').write(png_content)
67 open(image_filename,'w').write(png_content)
69 def generate_image(image_filename, barcode, generator):
71 generate_image_zint(image_filename, barcode)
72 elif generator == POSTSCRIPT:
73 generate_image_postscript(image_filename, barcode)
75 raise Exception("Unknown generator: %s" % generator)
77 def extract_barcodes():
78 for line in open("generation.config"):
79 content = line.split("#")[0].strip()
82 pos = content[1:].find('-')
83 exceptions = content[1:pos+1].split(",")
84 barcode = content[pos + 2:]
85 yield exceptions, barcode
89 if __name__ == '__main__':
92 for image_to_delete in glob.glob("*.png"):
93 os.remove(image_to_delete)
95 for text_to_delete in glob.glob("*.txt"):
96 os.remove(text_to_delete)
98 for generator in GENERATORS:
99 for exceptions, barcode in extract_barcodes():
100 if generator in exceptions:
104 image_filename = str(counter) + ".png"
105 text_filename = str(counter) + ".txt"
106 open(text_filename, "w").write(barcode)
107 generate_image(image_filename, barcode, generator)
109 print "Error with generator %s and barcode %s" % (generator, barcode)
110 traceback.print_exc()
113 open(TEST_FILE,'w').write("""package com.google.zxing.oned.rss;
115 import com.google.zxing.BarcodeFormat;
116 import com.google.zxing.MultiFormatReader;
117 import com.google.zxing.common.AbstractBlackBoxTestCase;
119 public class RSSExpandedBlackBox3TestCase extends AbstractBlackBoxTestCase {
121 public RSSExpandedBlackBox3TestCase() {
122 super("test/data/blackbox/rssexpanded-3", new MultiFormatReader(), BarcodeFormat.RSS_EXPANDED);
123 addTest(%(number)s, %(number)s, 0.0f);
124 addTest(%(number)s, %(number)s, 180.0f);
127 """ % {"number" : counter})