簡體   English   中英

如何在Java中使用GW EPL命令在TSC打印機上打印圖像

[英]How to print image on TSC Printer using GW EPL Command with java

我正在嘗試使用GW EPL命令和java在TSC打印機上打印圖像。 它使用以下命令GWp1,p2,p3,p4,這里的數據必須以字節為單位。我已經將圖像轉換為字符串,現在很困惑如何進行..

public class Example {

    @SuppressWarnings("resource")
    public static void main(String[] args) {
        File file = new File("C:/Users/user/Downloads/Logo.jpg");

        try {
            FileInputStream imageInFile = new FileInputStream(file);
            byte imageData[] = new byte[(int)file.length()];
            imageInFile.read(imageData);
            String imageDataString = encodeImage(imageData);
            System.out.println(imageDataString);

            System.out.println("Image Successfully Manipulated!");


            Configuration cfg = new Configuration();
             try {
             Template template = cfg.getTemplate("src/Test.ftl");  // Reading from Template path



                            Map<String, Object> data = new HashMap<String, Object>();
                            data.put("imageDataString", ""+imageDataString);


                            // Console output
                            Writer out = new OutputStreamWriter(System.out);
                            template.process(data, out);
                            out.flush();

                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                            OutputStreamWriter writer = new OutputStreamWriter(baos);
                            template.process(data, writer);
                            writer.close();

                            byte[] savedData = baos.toByteArray();

                          // Reading Text file  & Printing Logic     

                            ByteArrayInputStream bais = new ByteArrayInputStream(savedData);
                            DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
                            DocAttributeSet das = new HashDocAttributeSet();
                            Doc mydoc = new SimpleDoc(bais, flavor, das);
                            PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
                            aset.add(OrientationRequested.PORTRAIT); 
                            @SuppressWarnings("unused")
                            PrinterJob pj = PrinterJob.getPrinterJob(); 
                            PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, aset);
                            PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
                            for (int i = 0; i < services.length; i++) 
                            {
                              System.out.println(services[i].getName());
                            }
                            if(services.length == 0) 
                            {
                                if(defaultService == null) 
                                {
                                 //no printer found
                                } 
                                else {
                                  //print using default
                                   DocPrintJob job = defaultService.createPrintJob();
                                    job.print(mydoc, aset);
                                    }
                             } 
                            else {   
                                  PrintService service = ServiceUI.printDialog(null, 200, 200, services, defaultService, flavor, aset);
                                  if (service != null)
                                  {
                                  DocPrintJob job = service.createPrintJob();
                                  job.print(mydoc, aset);
                                  }
                                 } 



              }  catch ( Exception e)
                 {
                  e.printStackTrace();           
                 }    

        } catch (FileNotFoundException e) {
            System.out.println("Image not found" + e);
        } catch (IOException ioe) {
            System.out.println("Exception while reading the Image " + ioe);
        }

    }

    public static String encodeImage(byte[] imageByteArray){        
        return Base64.encodeBase64URLSafeString(imageByteArray);        
    }

    public static byte[] decodeImage(String imageDataString) {      
        return Base64.decodeBase64(imageDataString);
    }

}


src/Test.ftl - Template 
Q799,B080+000
q831
rN
S4
D7
ZT
JF
OD,P
R24,0
N


X555,56,2,780,714
GW595,450,"${imageDataString}"
P1

您可以創建一個prn文件並使用以下命令:

Runtime.getRuntime().exec("copy /c <prn-file path> <printer network path>");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM