OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 17 Rev 180
Line 21... Line 21...
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;
Line 47... Line 49...
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) {
Line 75... Line 77...
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);
Line 99... Line 103...
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
Line 330... Line 334...
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 = "";
Line 344... Line 349...
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
    }
348
 
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
    }
-
 
441
 
349
}
442
}