BarcodeUtils.java
4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright Codecrew Ry
*
* All rights reserved.
*
* This license applies to any software containing a notice placed by the
* copyright holder. Such software is herein referred to as the Software.
* This license covers modification, distribution and use of the Software.
*
* Any distribution and use in source and binary forms, with or without
* modification is not permitted without explicit written permission from the
* copyright owner.
*
* A non-exclusive royalty-free right is granted to the copyright owner of the
* Software to use, modify and distribute all modifications to the Software in
* future versions of the Software.
*
*/
package fi.codecrew.moya.utilities;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.krysalis.barcode4j.BarcodeDimension;
import org.krysalis.barcode4j.BarcodeGenerator;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.impl.code39.Code39Bean;
import org.krysalis.barcode4j.impl.upcean.EAN13Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
public class BarcodeUtils {
public static InputStream getBarcode(BarcodeGenerator generator, String message) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
BitmapCanvasProvider canvas = new BitmapCanvasProvider(
out, "image/png", 150, BufferedImage.TYPE_BYTE_BINARY, false, 0);
canvas.establishDimensions(new BarcodeDimension(200, 15));
generator.generateBarcode(canvas, message);
canvas.finish();
ByteArrayInputStream istream = new ByteArrayInputStream(out.toByteArray());
return istream;
}
public static InputStream getBarcode(String message) throws IOException {
return BarcodeUtils.getBarcode(new Code128Bean(), message);
}
public static InputStream getBarcodeEAN(String message) throws IOException {
return BarcodeUtils.getBarcode(new EAN13Bean(), message);
}
public static InputStream getBarcodeCode39(String message) throws IOException {
return BarcodeUtils.getBarcode(new Code39Bean(), message);
}
// public void barcode4j() throws FileNotFoundException, Exception {
// long start = new Date().getTime();
//
// File outFile = new File("/tmp/rairai2.pdf");
// PDF pdf = new PDF(new FileOutputStream(outFile));
// System.out.println("pre CreateCode: " + (new Date().getTime() - start));
//
// BarcodeGenerator bean = new DataMatrixBean();
//
// ByteArrayOutputStream out = new ByteArrayOutputStream();
// System.out.println("created: " + (new Date().getTime() - start));
//
// BitmapCanvasProvider canvas = new BitmapCanvasProvider(
// out, "image/png", 150, BufferedImage.TYPE_BYTE_BINARY, false, 0);
//
// bean.generateBarcode(canvas, "Foobar Rairai");
// canvas.finish();
//
// ByteArrayInputStream istream = new
// ByteArrayInputStream(out.toByteArray());
//
// Image img = new Image(pdf, istream, ImageType.PNG);
// Page page = new Page(pdf, A4.PORTRAIT);
// img.drawOn(page);
// System.out.println("post draw: " + (new Date().getTime() - start));
//
// pdf.flush();
// System.out.println("post flush: " + (new Date().getTime() - start));
//
// }
// public String asdasd() throws FileNotFoundException, Exception {
// long start = new Date().getTime();
// JBarcode code = JBarcodeFactory.getInstance().createCode128();
// System.out.println("pre CreateCode: " + (new Date().getTime() - start));
//
// BufferedImage barcode = code.createBarcode("Foobar Rairai");
// System.out.println("created: " + (new Date().getTime() - start));
// ImageIO.write(barcode, "JPEG", new File("/tmp/rairai.jpeg"));
// System.out.println("To File: " + (new Date().getTime() - start));
//
// File out = new File("/tmp/rairai2.pdf");
// PDF pdf = new PDF(new FileOutputStream(out));
//
// System.out.println("Pre stream: " + (new Date().getTime() - start));
//
// ByteArrayOutputStream jpegstream = new ByteArrayOutputStream();
// ImageIO.write(barcode, "JPEG", jpegstream);
// System.out.println("post stream: " + (new Date().getTime() - start));
//
// ByteArrayInputStream istream = new
// ByteArrayInputStream(jpegstream.toByteArray());
// System.out.println("post istream: " + (new Date().getTime() - start));
//
// Page page = new Page(pdf, A4.PORTRAIT);
//
// Image img = new Image(pdf, istream, ImageType.JPEG);
// img.drawOn(page);
// System.out.println("post draw: " + (new Date().getTime() - start));
//
// pdf.flush();
// System.out.println("post flush: " + (new Date().getTime() - start));
//
// return "";
// }
//
// public static void main(String[] args) {
// BarcodeUtils bb = new BarcodeUtils();
// try {
// bb.barcode4j();
// } catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// }
}