View Javadoc

1   /**
2    * 
3    */
4   package de.tivsource.page.helper.barcode;
5   
6   import java.awt.Color;
7   import java.awt.Graphics2D;
8   import java.awt.geom.AffineTransform;
9   import java.awt.image.BufferedImage;
10  import java.io.File;
11  import java.io.FileOutputStream;
12  import java.io.IOException;
13  import java.io.OutputStream;
14  
15  import org.krysalis.barcode4j.impl.pdf417.PDF417Bean;
16  import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
17  import org.krysalis.barcode4j.output.bitmap.BitmapEncoder;
18  import org.krysalis.barcode4j.output.bitmap.BitmapEncoderRegistry;
19  import org.krysalis.barcode4j.tools.UnitConv;
20  
21  /**
22   * Klassen zum Testen ob die BArcode Erstellung koreckt funktioniert.
23   * @author Marc Michele
24   *
25   */
26  public class TestBarcode {
27  
28  	/**
29  	 * @param args
30  	 */
31  	public static void main(String[] args) {
32          try {
33          	TestBarcode app = new TestBarcode();
34              File outputFile = new File("out.png");
35              app.generate(outputFile);
36          } catch (Exception e) {
37              e.printStackTrace();
38              System.exit(-1);
39          }
40  	}
41  
42  	
43  
44      private void generate(File outputFile) throws IOException {
45          String msg = "7125f3be-8d8d-4a78-809e-4ae236b825d1222";
46  
47          //Create the barcode bean
48          PDF417Bean bean = new PDF417Bean();
49  
50          bean.setErrorCorrectionLevel(2);
51          bean.setColumns(2);
52  
53          final int dpi = 200;
54  
55          //Configure the barcode generator
56          bean.setModuleWidth(UnitConv.in2mm(10.0f / dpi)); //makes a dot/module exactly eight pixels
57          bean.doQuietZone(false);
58         // bean.setShape(SymbolShapeHint.FORCE_RECTANGLE);
59  
60          boolean antiAlias = false;
61          int orientation = 0;
62          //Set up the canvas provider to create a monochrome bitmap
63          BitmapCanvasProvider canvas = new BitmapCanvasProvider(
64                  dpi, BufferedImage.TYPE_BYTE_BINARY, antiAlias, orientation);
65  
66          //Generate the barcode
67          bean.generateBarcode(canvas, msg);
68  
69          //Signal end of generation
70          canvas.finish();
71  
72          //Get generated bitmap
73          BufferedImage symbol = canvas.getBufferedImage();
74  
75          int width = symbol.getWidth();
76          int height = symbol.getHeight();
77  
78          //Add padding
79          int padding = 2;
80          width += 2 * padding;
81          height += 3 * padding;
82  
83          BufferedImage bitmap = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
84          Graphics2D g2d = (Graphics2D)bitmap.getGraphics();
85          g2d.setBackground(Color.white);
86          g2d.setColor(Color.black);
87          g2d.clearRect(0, 0, bitmap.getWidth(), bitmap.getHeight());
88          
89  
90          //Place the barcode symbol
91          AffineTransform symbolPlacement = new AffineTransform();
92          symbolPlacement.translate(padding, padding);
93          g2d.drawRenderedImage(symbol, symbolPlacement);
94  
95          //Add text lines (or anything else you might want to add)
96  
97          g2d.dispose();
98  
99          //Encode bitmap as file
100         String mime = "image/png";
101         OutputStream out = new FileOutputStream(outputFile);
102         try {
103             final BitmapEncoder encoder = BitmapEncoderRegistry.getInstance(mime);
104             encoder.encode(bitmap, out, mime, dpi);
105         } finally {
106             out.close();
107         }
108     }
109 	
110 }