OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 17 Rev 180
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
9
 * language governing permissions and limitations under the License.
10
 * 
10
 * 
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.ql;
14
 package org.openconcerto.ql;
15
 
15
 
16
import java.awt.image.BufferedImage;
16
import java.awt.image.BufferedImage;
17
import java.io.BufferedReader;
17
import java.io.BufferedReader;
18
import java.io.ByteArrayOutputStream;
18
import java.io.ByteArrayOutputStream;
19
import java.io.DataOutputStream;
19
import java.io.DataOutputStream;
20
import java.io.File;
20
import java.io.File;
21
import java.io.IOException;
21
import java.io.IOException;
22
import java.io.InputStreamReader;
22
import java.io.InputStreamReader;
23
import java.net.InetAddress;
23
import java.net.InetAddress;
24
import java.net.Socket;
24
import java.net.Socket;
25
import java.net.UnknownHostException;
25
import java.net.UnknownHostException;
-
 
26
import java.nio.charset.StandardCharsets;
26
 
27
 
27
import javax.imageio.ImageIO;
28
import javax.imageio.ImageIO;
28
 
29
 
29
public class QLPrinter {
30
public class QLPrinter {
-
 
31
    private static final int ESC = 0x1B;
30
    private String host;
32
    private String host;
31
    private boolean highQuality;
33
    private boolean highQuality;
32
    private boolean paperCutAuto;
34
    private boolean paperCutAuto;
33
    private int printWidth;
35
    private int printWidth;
34
    private int port = 515;
36
    private int port = 515;
35
    private int timeout = 10000;
37
    private int timeout = 10000;
36
 
38
 
37
    public QLPrinter(String hostname) {
39
    public QLPrinter(String hostname) {
38
        if (hostname == null || hostname.trim().length() == 0) {
40
        if (hostname == null || hostname.trim().length() == 0) {
39
            throw new IllegalArgumentException("Bad HostName : " + hostname);
41
            throw new IllegalArgumentException("Bad HostName : " + hostname);
40
        }
42
        }
41
        this.host = hostname;
43
        this.host = hostname;
42
        this.paperCutAuto = true;
44
        this.paperCutAuto = true;
43
        this.printWidth = 62;
45
        this.printWidth = 62;
44
    }
46
    }
45
 
47
 
46
    public void setHighQuality(boolean b) {
48
    public void setHighQuality(boolean b) {
47
        this.highQuality = b;
49
        this.highQuality = b;
48
    }
50
    }
49
 
51
 
50
    /**
52
    /**
51
     * Set print width (in milimeters)
53
     * Set print width (in milimeters)
52
     * */
54
     */
53
    public void setPrintWidth(int mm) {
55
    public void setPrintWidth(int mm) {
54
        this.printWidth = mm;
56
        this.printWidth = mm;
55
    }
57
    }
56
 
58
 
57
    public void setPaperCutAuto(boolean b) {
59
    public void setPaperCutAuto(boolean b) {
58
        this.paperCutAuto = b;
60
        this.paperCutAuto = b;
59
    }
61
    }
60
 
62
 
61
    public void print(File f) throws IOException {
63
    public void print(File f) throws IOException {
62
        BufferedImage img = ImageIO.read(f);
64
        BufferedImage img = ImageIO.read(f);
63
        print(img);
65
        print(img);
64
    }
66
    }
65
 
67
 
66
    public void print(BufferedImage img) throws IOException {
68
    public void print(BufferedImage img) throws IOException {
67
        final byte data[] = getContent(img);
69
        final byte data[] = getContent(img);
68
        print(data);
70
        print(data);
69
    }
71
    }
70
 
72
 
71
    private byte[] getContent(BufferedImage img) {
73
    private byte[] getContent(BufferedImage img) {
72
        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
74
        ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
73
        int nbLines = img.getHeight();
75
        int nbLines = img.getHeight();
74
        int width = img.getWidth();
76
        int width = img.getWidth();
75
 
77
 
76
        // Header: 0x00 : 200 fois
78
        // Header: 0x00 : 200 fois
77
        for (int i = 0; i < 200; i++) {
79
        for (int i = 0; i < 200; i++) {
78
            out.write(0x00);
80
            out.write(0x00);
79
        }
81
        }
80
        // Init 0x1B 0x40
-
 
81
        out.write(0x1B);
-
 
82
        out.write(0x40);
-
 
-
 
82
 
83
        // ?? : 0x1B 0x69 0x61 0x01
83
        // Select ESCP/P Mode : 0x1B 0x69 0x61 0x01
84
        out.write(0x1B);
84
        out.write(ESC);
85
        out.write(0x69);
85
        out.write(0x69);
86
        out.write(0x61);
86
        out.write(0x61);
87
        out.write(0x01);
87
        out.write(0x01);
-
 
88
        // Init 0x1B 0x40
-
 
89
        out.write(ESC);
-
 
90
        out.write(0x40);
-
 
91
 
88
        // Page Length
92
        // Page Length
89
        out.write(0x1B);
93
        out.write(ESC);
90
        out.write(0x69);
94
        out.write(0x69);
91
        out.write(0x7A);
95
        out.write(0x7A);
92
 
96
 
93
        out.write(-58);
97
        out.write(-58);
94
        out.write(10);
98
        out.write(10);
95
        out.write(this.printWidth);
99
        out.write(this.printWidth);
96
        out.write(0);
100
        out.write(0);
97
 
101
 
98
        out.write(nbLines);
102
        out.write(nbLines);
99
        out.write(nbLines >> 8);
103
        out.write(nbLines >> 8);
100
        out.write(0);
104
        out.write(0);
101
        out.write(0);
105
        out.write(0);
102
        out.write(0);
106
        out.write(0);
103
        out.write(0);
107
        out.write(0);
104
        // Paper Cut
108
        // Paper Cut : ESC i
105
        out.write(0x1B);
109
        out.write(ESC);
106
        out.write(0x69);
110
        out.write(0x69);
107
        out.write(0x4D);
111
        out.write(0x4D);
108
        if (this.paperCutAuto) {
112
        if (this.paperCutAuto) {
109
            out.write(0x40);
113
            out.write(0x40);
110
        } else {
114
        } else {
111
            out.write(0x00);
115
            out.write(0x00);
112
        }
116
        }
113
 
117
 
114
        // ?? : 0x1B 0x69 0x41 0x01
118
        // ?? : 0x1B 0x69 0x41 0x01 :ESC i A
115
        out.write(0x1B);
119
        out.write(ESC);
116
        out.write(0x69);
120
        out.write(0x69);
117
        out.write(0x41);
121
        out.write(0x41);
118
        out.write(0x01);
122
        out.write(0x01);
119
        // Set Mode
123
        // Set Mode : ESC i K
120
        out.write(0x1B);
124
        out.write(ESC);
121
        out.write(0x69);
125
        out.write(0x69);
122
        out.write(0x4B);
126
        out.write(0x4B);
123
        if (!this.highQuality) {
127
        if (!this.highQuality) {
124
            out.write(0x08);
128
            out.write(0x08);
125
        } else {
129
        } else {
126
            out.write(0x48);
130
            out.write(0x48);
127
        }
131
        }
128
        // Set Margin
132
        // Set Margin
129
        out.write(0x1B);
133
        out.write(ESC);
130
        out.write(0x69);
134
        out.write(0x69);
131
        out.write(0x64);
135
        out.write(0x64);
132
        out.write(0x00);
136
        out.write(0x00);
133
        out.write(0x00);
137
        out.write(0x00);
134
        // Set Compression
138
        // Set Compression
135
        out.write(0x4D);
139
        out.write(0x4D);
136
        out.write(0x02);
140
        out.write(0x02);
137
 
141
 
138
        // Lines
142
        // Lines
139
        for (int i = 0; i < nbLines; i++) {
143
        for (int i = 0; i < nbLines; i++) {
140
            int[] pixels = new int[width];
144
            int[] pixels = new int[width];
141
            img.getRGB(0, i, width, 1, pixels, 0, 4);
145
            img.getRGB(0, i, width, 1, pixels, 0, 4);
142
            try {
146
            try {
143
 
147
 
144
                final byte[] encodedLine = encodeLine(pixels);
148
                final byte[] encodedLine = encodeLine(pixels);
145
                if (encodedLine.length > 1) {
149
                if (encodedLine.length > 1) {
146
                    out.write(0x67);
150
                    out.write(0x67);
147
                    out.write(0);
151
                    out.write(0);
148
                    out.write(encodedLine.length);
152
                    out.write(encodedLine.length);
149
                }
153
                }
150
                out.write(encodedLine);
154
                out.write(encodedLine);
151
 
155
 
152
            } catch (IOException e) {
156
            } catch (IOException e) {
153
                e.printStackTrace();
157
                e.printStackTrace();
154
            }
158
            }
155
        }
159
        }
156
        // END
160
        // END
157
        out.write(0x1A);
161
        out.write(0x1A);
158
 
162
 
159
        return out.toByteArray();
163
        return out.toByteArray();
160
    }
164
    }
161
 
165
 
162
    private byte[] encodeLine(int[] pixels) throws IOException {
166
    private byte[] encodeLine(int[] pixels) throws IOException {
163
        ByteArrayOutputStream out = new ByteArrayOutputStream(pixels.length);
167
        ByteArrayOutputStream out = new ByteArrayOutputStream(pixels.length);
164
        byte[] bytesToEncode = new byte[pixels.length / 8];
168
        byte[] bytesToEncode = new byte[pixels.length / 8];
165
        int index = 0;
169
        int index = 0;
166
        for (int i = 0; i < bytesToEncode.length; i++) {
170
        for (int i = 0; i < bytesToEncode.length; i++) {
167
            int points = 0;
171
            int points = 0;
168
            for (int j = 0; j < 8; j++) {
172
            for (int j = 0; j < 8; j++) {
169
                int c = pixels[pixels.length - index - 1];
173
                int c = pixels[pixels.length - index - 1];
170
                int r = (c & 0x00ff0000) >> 16;
174
                int r = (c & 0x00ff0000) >> 16;
171
                int g = (c & 0x0000ff00) >> 8;
175
                int g = (c & 0x0000ff00) >> 8;
172
                int b = c & 0x000000ff;
176
                int b = c & 0x000000ff;
173
 
177
 
174
                int grayScale = (int) (21.2671 * r + 71.5160 * g + 7.2169 * b);
178
                int grayScale = (int) (21.2671 * r + 71.5160 * g + 7.2169 * b);
175
                boolean isBlack = grayScale < 12000;
179
                boolean isBlack = grayScale < 12000;
176
 
180
 
177
                points = points * 2;
181
                points = points * 2;
178
                if (isBlack) {
182
                if (isBlack) {
179
                    points++;
183
                    points++;
180
                }
184
                }
181
                index++;
185
                index++;
182
            }
186
            }
183
            bytesToEncode[i] = (byte) points;
187
            bytesToEncode[i] = (byte) points;
184
 
188
 
185
        }
189
        }
186
 
190
 
187
        boolean emptyLine = true;
191
        boolean emptyLine = true;
188
        for (int i = 0; i < bytesToEncode.length; i++) {
192
        for (int i = 0; i < bytesToEncode.length; i++) {
189
 
193
 
190
            if (bytesToEncode[i] != 0) {
194
            if (bytesToEncode[i] != 0) {
191
                emptyLine = false;
195
                emptyLine = false;
192
                break;
196
                break;
193
            }
197
            }
194
        }
198
        }
195
 
199
 
196
        if (emptyLine) {
200
        if (emptyLine) {
197
 
201
 
198
            out.write(0x5A);
202
            out.write(0x5A);
199
        } else {
203
        } else {
200
 
204
 
201
            ByteArrayOutputStream outTemp = new ByteArrayOutputStream();
205
            ByteArrayOutputStream outTemp = new ByteArrayOutputStream();
202
            byte last = bytesToEncode[0];
206
            byte last = bytesToEncode[0];
203
            outTemp.write(last);
207
            outTemp.write(last);
204
            boolean sameByteMode = false;
208
            boolean sameByteMode = false;
205
            for (int i = 1; i < bytesToEncode.length; i++) {
209
            for (int i = 1; i < bytesToEncode.length; i++) {
206
                byte b = bytesToEncode[i];
210
                byte b = bytesToEncode[i];
207
                if (b == last) {
211
                if (b == last) {
208
                    if (sameByteMode) {
212
                    if (sameByteMode) {
209
                        // On a le meme octet que ceux précédents
213
                        // On a le meme octet que ceux précédents
210
                        sameByteMode = true;
214
                        sameByteMode = true;
211
                        outTemp.write(b);
215
                        outTemp.write(b);
212
                    } else {
216
                    } else {
213
                        if (outTemp.size() > 1) {
217
                        if (outTemp.size() > 1) {
214
                            // On encode en raw
218
                            // On encode en raw
215
                            send(out, outTemp.toByteArray(), sameByteMode);
219
                            send(out, outTemp.toByteArray(), sameByteMode);
216
                            // Nouvelle serie
220
                            // Nouvelle serie
217
                            outTemp = new ByteArrayOutputStream();
221
                            outTemp = new ByteArrayOutputStream();
218
                            sameByteMode = false;
222
                            sameByteMode = false;
219
                        } else {
223
                        } else {
220
                            sameByteMode = true;
224
                            sameByteMode = true;
221
                        }
225
                        }
222
                        outTemp.write(b);
226
                        outTemp.write(b);
223
                    }
227
                    }
224
                } else {
228
                } else {
225
                    if (sameByteMode) {
229
                    if (sameByteMode) {
226
                        // On envoie la serie d'octets identiques
230
                        // On envoie la serie d'octets identiques
227
                        send(out, outTemp.toByteArray(), sameByteMode);
231
                        send(out, outTemp.toByteArray(), sameByteMode);
228
                        // Nouvelle serie
232
                        // Nouvelle serie
229
                        outTemp = new ByteArrayOutputStream();
233
                        outTemp = new ByteArrayOutputStream();
230
                        sameByteMode = false;
234
                        sameByteMode = false;
231
                        outTemp.write(b);
235
                        outTemp.write(b);
232
                    } else {
236
                    } else {
233
                        sameByteMode = false;
237
                        sameByteMode = false;
234
                        outTemp.write(b);
238
                        outTemp.write(b);
235
 
239
 
236
                    }
240
                    }
237
                }
241
                }
238
                if (outTemp.size() > 120) {
242
                if (outTemp.size() > 120) {
239
                    // On envoie la serie
243
                    // On envoie la serie
240
                    send(out, outTemp.toByteArray(), sameByteMode);
244
                    send(out, outTemp.toByteArray(), sameByteMode);
241
                    // Nouvelle serie
245
                    // Nouvelle serie
242
                    outTemp = new ByteArrayOutputStream();
246
                    outTemp = new ByteArrayOutputStream();
243
                    sameByteMode = false;
247
                    sameByteMode = false;
244
 
248
 
245
                }
249
                }
246
                last = b;
250
                last = b;
247
            }
251
            }
248
            if (outTemp.size() > 0) {
252
            if (outTemp.size() > 0) {
249
                // On envoie la serie
253
                // On envoie la serie
250
                send(out, outTemp.toByteArray(), sameByteMode);
254
                send(out, outTemp.toByteArray(), sameByteMode);
251
            }
255
            }
252
 
256
 
253
        }
257
        }
254
        return out.toByteArray();
258
        return out.toByteArray();
255
    }
259
    }
256
 
260
 
257
    private void send(ByteArrayOutputStream out, byte[] byteArray, boolean sameByteMode) throws IOException {
261
    private void send(ByteArrayOutputStream out, byte[] byteArray, boolean sameByteMode) throws IOException {
258
        if (sameByteMode) {
262
        if (sameByteMode) {
259
            out.write(257 - byteArray.length);
263
            out.write(257 - byteArray.length);
260
            out.write(byteArray[0]);
264
            out.write(byteArray[0]);
261
        } else {
265
        } else {
262
            out.write(byteArray.length - 1);
266
            out.write(byteArray.length - 1);
263
            out.write(byteArray);
267
            out.write(byteArray);
264
        }
268
        }
265
    }
269
    }
266
 
270
 
267
    private String getNewJobId() {
271
    private String getNewJobId() {
268
        String id = String.valueOf((int) Math.floor(Math.random() * 999));
272
        String id = String.valueOf((int) Math.floor(Math.random() * 999));
269
        while (id.length() < 3) {
273
        while (id.length() < 3) {
270
            id = "0" + id;
274
            id = "0" + id;
271
        }
275
        }
272
        return id;
276
        return id;
273
    }
277
    }
274
 
278
 
275
    public void print(byte data[]) throws UnknownHostException, IOException {
279
    public void print(byte data[]) throws UnknownHostException, IOException {
276
        final Socket socket = new Socket(InetAddress.getByName(this.host), this.port);
280
        final Socket socket = new Socket(InetAddress.getByName(this.host), this.port);
277
        socket.setSoTimeout(this.timeout);
281
        socket.setSoTimeout(this.timeout);
278
        final String queue = "lpr";
282
        final String queue = "lpr";
279
        final String jobid = getNewJobId();
283
        final String jobid = getNewJobId();
280
        if (socket != null) {
284
        if (socket != null) {
281
            String myHostName;
285
            String myHostName;
282
            try {
286
            try {
283
                myHostName = InetAddress.getLocalHost().getHostName();
287
                myHostName = InetAddress.getLocalHost().getHostName();
284
            } catch (Exception e) {
288
            } catch (Exception e) {
285
                myHostName = "Host";
289
                myHostName = "Host";
286
            }
290
            }
287
            final String user = "Java";
291
            final String user = "Java";
288
            String cfa = getCFA(myHostName, user, "Label", jobid);
292
            String cfa = getCFA(myHostName, user, "Label", jobid);
289
 
293
 
290
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
294
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
291
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
295
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
292
 
296
 
293
            // Init
297
            // Init
294
            out.write(0x02);
298
            out.write(0x02);
295
            out.writeBytes(queue + "\n");
299
            out.writeBytes(queue + "\n");
296
            out.flush();
300
            out.flush();
297
            if (in.read() != 0) {
301
            if (in.read() != 0) {
298
                throw new IOException("Error printing on queue");
302
                throw new IOException("Error printing on queue");
299
            }
303
            }
300
 
304
 
301
            // Control file
305
            // Control file
302
            out.write(0x02);
306
            out.write(0x02);
303
            out.writeBytes(String.valueOf(cfa.length()) + " ");
307
            out.writeBytes(String.valueOf(cfa.length()) + " ");
304
            out.writeBytes("cfA" + jobid + user + "\n");
308
            out.writeBytes("cfA" + jobid + user + "\n");
305
            out.flush();
309
            out.flush();
306
            if (in.read() != 0) {
310
            if (in.read() != 0) {
307
                throw new IOException("Error sending start of control file");
311
                throw new IOException("Error sending start of control file");
308
            }
312
            }
309
            out.writeBytes(cfa);
313
            out.writeBytes(cfa);
310
            out.writeByte(0x00);
314
            out.writeByte(0x00);
311
            out.flush();
315
            out.flush();
312
            if (in.read() != 0) {
316
            if (in.read() != 0) {
313
                throw new IOException("Error sending control file");
317
                throw new IOException("Error sending control file");
314
            }
318
            }
315
 
319
 
316
            // Data
320
            // Data
317
            out.write(0x03);
321
            out.write(0x03);
318
            out.writeBytes(String.valueOf(data.length) + " ");
322
            out.writeBytes(String.valueOf(data.length) + " ");
319
            out.writeBytes("dfA" + jobid + user + "\n");
323
            out.writeBytes("dfA" + jobid + user + "\n");
320
            out.flush();
324
            out.flush();
321
            if (in.read() != 0) {
325
            if (in.read() != 0) {
322
                throw new IOException("Error sending start of data");
326
                throw new IOException("Error sending start of data");
323
            }
327
            }
324
            out.write(data);
328
            out.write(data);
325
            out.writeByte(0x00);
329
            out.writeByte(0x00);
326
            out.flush();
330
            out.flush();
327
            if (in.read() != 0) {
331
            if (in.read() != 0) {
328
                throw new IOException("Error sending end of data");
332
                throw new IOException("Error sending end of data");
329
            }
333
            }
330
            out.flush();
334
            out.flush();
331
 
335
 
332
            out.close();
336
            out.close();
333
            in.close();
337
            in.close();
334
            socket.close();
338
            socket.close();
-
 
339
            System.out.println("QLPrinter.print() done");
335
        }
340
        }
336
    }
341
    }
337
 
342
 
338
    private String getCFA(String myHostName, String user, String documentName, String jobid) {
343
    private String getCFA(String myHostName, String user, String documentName, String jobid) {
339
        String cfA = "";
344
        String cfA = "";
340
        cfA += ("H" + myHostName + "\n");
345
        cfA += ("H" + myHostName + "\n");
341
        cfA += ("P" + user + "\n");
346
        cfA += ("P" + user + "\n");
342
        cfA += ("J" + documentName + "\n");
347
        cfA += ("J" + documentName + "\n");
343
        cfA += ("ldfA" + jobid + user + "\n");
348
        cfA += ("ldfA" + jobid + user + "\n");
344
        cfA += ("UdfA" + jobid + myHostName + "\n");
349
        cfA += ("UdfA" + jobid + myHostName + "\n");
345
        cfA += ("N" + documentName + "\n");
350
        cfA += ("N" + documentName + "\n");
346
        return cfA;
351
        return cfA;
347
    }
352
    }
-
 
353
 
-
 
354
    /**
-
 
355
     * Print 2D barcode (DataMatrix)
-
 
356
     * 
-
 
357
     * @param dotsPerCell (3 - 10)
-
 
358
     * @param barcode : barcode (ascii)
-
 
359
     * @throws IOException
-
 
360
     * 
-
 
361
     */
-
 
362
    public void printDataMatrixBarCode2D(int dotsPerCell, String barcode) throws IOException {
-
 
363
        if (barcode.length() > (144 * 144)) {
-
 
364
            throw new IllegalArgumentException("barcode too long (max : 144x144:20736)");
-
 
365
        }
-
 
366
 
-
 
367
        ByteArrayOutputStream out = new ByteArrayOutputStream();
-
 
368
        {
-
 
369
            // Header: 0x00 : 200 fois
-
 
370
            for (int i = 0; i < 200; i++) {
-
 
371
                out.write(0x00);
-
 
372
            }
-
 
373
 
-
 
374
        }
-
 
375
 
-
 
376
        // Select ESCP/P Mode : 0x1B 0x69 0x61 0x00
-
 
377
        out.write(ESC);
-
 
378
        out.write(0x69);
-
 
379
        out.write(0x61);
-
 
380
        out.write(0x00); // mode ESC/P standard
-
 
381
        // Init 0x1B 0x40
-
 
382
        out.write(ESC);
-
 
383
        out.write(0x40);
-
 
384
 
-
 
385
        // ESC i D
-
 
386
        out.write(ESC);
-
 
387
        out.write(0x69);
-
 
388
        out.write(0x44);
-
 
389
        //
-
 
390
        out.write(dotsPerCell);
-
 
391
        out.write(0); // square
-
 
392
        out.write(0); // vertical size : auto
-
 
393
        out.write(0); // horizontal size : auto
-
 
394
        for (int i = 0; i < 5; i++) {
-
 
395
            out.write(0); // reserved
-
 
396
        }
-
 
397
        // data
-
 
398
        out.write(barcode.getBytes(StandardCharsets.US_ASCII));
-
 
399
        out.write('\\');
-
 
400
        out.write('\\');
-
 
401
        out.write('\\');
-
 
402
        // Page feed
-
 
403
        out.write(0x0C);
-
 
404
 
-
 
405
        out.flush();
-
 
406
 
-
 
407
        final byte[] byteArray = out.toByteArray();
-
 
408
        print(byteArray);
-
 
409
    }
-
 
410
 
-
 
411
    public void print(String string) throws IOException {
-
 
412
        ByteArrayOutputStream bOut = new ByteArrayOutputStream();
-
 
413
        // Select ESCP/P Mode : 0x1B 0x69 0x61 0x00
-
 
414
        bOut.write(ESC);
-
 
415
        bOut.write(0x69);
-
 
416
        bOut.write(0x61);
-
 
417
        bOut.write(0x0); // mode ESC/P standard
-
 
418
        // Init 0x1B 0x40
-
 
419
        bOut.write(ESC);
-
 
420
        bOut.write(0x40);
-
 
421
 
-
 
422
        // Init
-
 
423
        bOut.write(ESC);
-
 
424
        bOut.write(0x40);
-
 
425
        // French characters
-
 
426
        bOut.write(ESC);
-
 
427
        bOut.write(0x52);
-
 
428
        bOut.write(0x01);
-
 
429
        // Normal
-
 
430
        bOut.write(ESC);
-
 
431
        bOut.write(0x21);
-
 
432
        bOut.write(0);// Default
-
 
433
        bOut.write(string.getBytes(StandardCharsets.US_ASCII));
-
 
434
        bOut.write(0x0A);// Retour a la ligne
-
 
435
        // Page feed
-
 
436
        bOut.write(0x0C);
-
 
437
 
-
 
438
        print(bOut.toByteArray());
-
 
439
 
-
 
440
    }
348
 
441
 
349
}
442
}