181 |
ilm |
1 |
package org.openconcerto.modules.label.graphicspl;
|
|
|
2 |
|
|
|
3 |
import java.awt.Color;
|
|
|
4 |
import java.awt.Graphics;
|
|
|
5 |
import java.awt.Graphics2D;
|
|
|
6 |
import java.awt.image.BufferedImage;
|
|
|
7 |
import java.awt.print.PageFormat;
|
|
|
8 |
import java.awt.print.Printable;
|
|
|
9 |
import java.awt.print.PrinterException;
|
|
|
10 |
import java.awt.print.PrinterJob;
|
|
|
11 |
import java.io.ByteArrayInputStream;
|
|
|
12 |
import java.io.File;
|
|
|
13 |
import java.io.IOException;
|
|
|
14 |
import java.nio.charset.StandardCharsets;
|
|
|
15 |
import java.nio.file.Files;
|
|
|
16 |
|
|
|
17 |
import javax.imageio.ImageIO;
|
|
|
18 |
import javax.xml.parsers.DocumentBuilder;
|
|
|
19 |
import javax.xml.parsers.DocumentBuilderFactory;
|
|
|
20 |
import javax.xml.parsers.ParserConfigurationException;
|
|
|
21 |
|
|
|
22 |
import org.w3c.dom.Document;
|
|
|
23 |
import org.w3c.dom.Element;
|
|
|
24 |
import org.xml.sax.SAXException;
|
|
|
25 |
|
|
|
26 |
public class GraphicsPL {
|
183 |
ilm |
27 |
private String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><graphicspl height=\"400\" width=\"600\"/>";
|
|
|
28 |
private Document doc;
|
181 |
ilm |
29 |
private File imgDir;
|
|
|
30 |
|
|
|
31 |
public static void main(String[] args) throws Exception {
|
|
|
32 |
GraphicsPL g = new GraphicsPL();
|
|
|
33 |
g.load(new File("Template/Labels/test.graphicspl"));
|
|
|
34 |
BufferedImage img = g.createImage(10);
|
|
|
35 |
ImageIO.write(img, "png", new File("gpl.png"));
|
|
|
36 |
String zpl = g.getZPL();
|
|
|
37 |
System.out.println(zpl);
|
|
|
38 |
Printable p = g.createPrintable();
|
|
|
39 |
PrinterJob job = PrinterJob.getPrinterJob();
|
|
|
40 |
job.setPrintable(p);
|
|
|
41 |
boolean ok = job.printDialog();
|
|
|
42 |
if (ok) {
|
|
|
43 |
job.print();
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
public Printable createPrintable() {
|
|
|
49 |
final Element root = this.doc.getDocumentElement();
|
|
|
50 |
int dpi = 300;
|
|
|
51 |
if (root.hasAttribute("dpi")) {
|
|
|
52 |
dpi = Integer.parseInt(root.getAttribute("dpi"));
|
|
|
53 |
}
|
|
|
54 |
float printRatio = 1f;
|
|
|
55 |
if (root.hasAttribute("printratio")) {
|
|
|
56 |
printRatio = Float.parseFloat(root.getAttribute("printratio"));
|
|
|
57 |
}
|
|
|
58 |
return createPrintable(dpi, printRatio);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
public Printable createPrintable(int dpi, float printRatio) {
|
|
|
62 |
return new Printable() {
|
|
|
63 |
|
|
|
64 |
@Override
|
|
|
65 |
public int print(Graphics graphics, PageFormat pf, int pageIndex) throws PrinterException {
|
|
|
66 |
if (pageIndex > 0) {
|
|
|
67 |
return NO_SUCH_PAGE;
|
|
|
68 |
}
|
|
|
69 |
final Element root = GraphicsPL.this.doc.getDocumentElement();
|
|
|
70 |
final int width = Math.round(printRatio * Integer.parseInt(root.getAttribute("width")));
|
|
|
71 |
final int height = Math.round(printRatio * Integer.parseInt(root.getAttribute("height")));
|
|
|
72 |
final Graphics2D g2d = (Graphics2D) graphics;
|
|
|
73 |
float ratio = (printRatio * dpi) / 72f;
|
|
|
74 |
try {
|
|
|
75 |
final BufferedImage img = createImage(ratio);
|
|
|
76 |
g2d.drawImage(img, (int) Math.round(pf.getImageableX()), (int) Math.round(pf.getImageableY()), width, height, null);
|
|
|
77 |
} catch (ParserConfigurationException | SAXException | IOException e) {
|
183 |
ilm |
78 |
e.printStackTrace();
|
181 |
ilm |
79 |
throw new PrinterException(e.getMessage());
|
|
|
80 |
}
|
|
|
81 |
return PAGE_EXISTS;
|
|
|
82 |
}
|
|
|
83 |
};
|
|
|
84 |
}
|
|
|
85 |
|
183 |
ilm |
86 |
public String getZPL() throws IOException {
|
181 |
ilm |
87 |
final ZPLRenderer renderer = new ZPLRenderer();
|
|
|
88 |
renderer.render(this);
|
|
|
89 |
return renderer.getZPL();
|
|
|
90 |
}
|
|
|
91 |
|
183 |
ilm |
92 |
public BufferedImage createImage(float ratio) throws ParserConfigurationException, SAXException, IOException {
|
181 |
ilm |
93 |
final Element root = this.doc.getDocumentElement();
|
|
|
94 |
final int width = Math.round(ratio * Integer.parseInt(root.getAttribute("width")));
|
|
|
95 |
final int height = Math.round(ratio * Integer.parseInt(root.getAttribute("height")));
|
|
|
96 |
final BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
|
|
|
97 |
final Graphics2D graphics = (Graphics2D) img.getGraphics();
|
|
|
98 |
graphics.setColor(Color.WHITE);
|
|
|
99 |
graphics.fillRect(0, 0, width, height);
|
|
|
100 |
final Graphics2DRenderer renderer = new Graphics2DRenderer(graphics, ratio);
|
|
|
101 |
renderer.render(this);
|
|
|
102 |
graphics.dispose();
|
|
|
103 |
return img;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
public Document getDocument() {
|
|
|
107 |
return this.doc;
|
|
|
108 |
}
|
|
|
109 |
|
183 |
ilm |
110 |
public void load(File file) throws ParserConfigurationException, SAXException, IOException {
|
181 |
ilm |
111 |
this.xml = new String(Files.readAllBytes(file.toPath()));
|
|
|
112 |
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
|
113 |
// factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
|
|
|
114 |
final DocumentBuilder builder = factory.newDocumentBuilder();
|
|
|
115 |
final ByteArrayInputStream input = new ByteArrayInputStream(this.xml.getBytes(StandardCharsets.UTF_8));
|
|
|
116 |
this.doc = builder.parse(input);
|
|
|
117 |
this.doc.getDocumentElement().normalize();
|
|
|
118 |
this.imgDir = file.getParentFile();
|
|
|
119 |
}
|
|
|
120 |
|
183 |
ilm |
121 |
public void load(String str, File imageDir) throws ParserConfigurationException, SAXException, IOException {
|
|
|
122 |
this.xml = str;
|
|
|
123 |
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
|
|
124 |
// factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
|
|
|
125 |
final DocumentBuilder builder = factory.newDocumentBuilder();
|
|
|
126 |
final ByteArrayInputStream input = new ByteArrayInputStream(this.xml.getBytes(StandardCharsets.UTF_8));
|
|
|
127 |
this.doc = builder.parse(input);
|
|
|
128 |
this.doc.getDocumentElement().normalize();
|
|
|
129 |
this.imgDir = imageDir;
|
|
|
130 |
}
|
|
|
131 |
|
181 |
ilm |
132 |
public File getImageDir() {
|
|
|
133 |
return this.imgDir;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
public void setImageDir(File dir) {
|
|
|
137 |
this.imgDir = dir;
|
|
|
138 |
}
|
|
|
139 |
|
|
|
140 |
}
|