java – Zebra打印机GC420t无法打印图像EPL 2 GW

使用JasperReport生成图像,然后尝试在Zebra打印机 GC420t
上打印该图像.生成图像但不打印.我已经仔细检查了连接和端口.我读过 this SO link
和 the calibration

,但没有任何作用.

码:

public void generateReport(Map<String, Object> parameters, List<Label> labels)
            throws JRException, IOException, ConnectionException, ZebraPrinterLanguageUnknownException{
        // TODO Auto-generated method stub
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(labels);
        System.out.println(" Wait !!");
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource);
        if(jasperPrint != null && jasperPrint.getPages()!=null && jasperPrint.getPages().size()>=0){
            FileOutputStream fos = new FileOutputStream("C://Users//desktop//Labels.png");
            //JasperExportManager.exportReportToPdfStream(jasperPrint, fos);
            BufferedImage rendered_image = null;
            rendered_image = (BufferedImage) JasperPrintManager.printPageToImage(jasperPrint, 0, 1.6f);
            ImageIO.write(rendered_image, "png", fos);
            Connection thePrinterConn = new DriverPrinterConnection("GC420t");
            try{
                for (DiscoveredPrinterDriver printer : UsbDiscoverer.getZebraDriverPrinters()){
                    System.out.println(printer);
                }
                thePrinterConn.open();
                if(zPrinter==null){
                    zPrinter = ZebraPrinterFactory.getInstance(thePrinterConn);
                }
                PrinterStatus printerStatus = zPrinter.getCurrentStatus();
                if(printerStatus.isReadyToPrint){
                    System.out.println("Ready to print !!");
                    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
                    DocAttributeSet das = new HashDocAttributeSet();
                    FileInputStream fis = new FileInputStream("C://Users//desktop//Labels.png");
                    Doc mydoc = new SimpleDoc(fis, 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();
                            try{
                                job.print(mydoc, aset);
                            }catch (PrintException e){
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        }
                    }
                    else{
                         PrintService service = ServiceUI.printDialog(null, 200, 200, services, defaultService, flavor, aset);
                         if (service != null){
                             DocPrintJob job = service.createPrintJob();
                             try{
                                 job.print(mydoc, aset);
                             }catch(PrintException e){
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                             }
                        }
                   }
                   //ZebraImageI image = ZebraImageFactory.getImage("C://Users//desktop//Labels.png");
                }
                else{
                    System.out.println("Something went wrong");
                }
            }finally{
                thePrinterConn.close();
            }
            System.out.println("Report generated !!");
        }
    }

我阅读了 EPL 2 manual
并将图像转换为二进制图形数据以立即打印.

码:

private byte[] getEplGraphics(int top, int left, BufferedImage bufferedImage) throws IOException {
        ByteArrayOutputStream fs = new ByteArrayOutputStream();

        //int canvasWidth = bufferedImage.getWidth();
        // loop from top to bottom
        System.out.println(bufferedImage.getHeight());
        System.out.println(bufferedImage.getWidth());

        int maxY = bufferedImage.getHeight() + (64- bufferedImage.getHeight()%64);
        int maxX = bufferedImage.getWidth() + (64- bufferedImage.getWidth()%64);
        System.out.println(maxX);
        System.out.println(maxY);
        int p3 = maxX / 8;
        int p4 = maxY/ 8;
        int len = 0;
        String gw = "N/nGW0,0," + p3 + "," + p4 + ",";
        fs.write(gw.getBytes());
        for (int y = 0; y < maxY; ++y) {
            // from left to right
            for (int x = 0; x < maxX;) {
                byte abyte = 0;
                // get 8 bits together and write to memory

                for (int b = 0; b < 8; ++b, ++x) {
                    // set 1 for white,0 for black
                    int dot = 1;

                    // pixel still in width of bitmap,
                    // check luminance for white or black, out of bitmap set to white

                    if (x < bufferedImage.getWidth() && y < bufferedImage.getHeight()) {
                        int c = bufferedImage.getRGB(x, y);
                        int red = (c & 0x00ff0000) >> 16;
                        int green = (c & 0x0000ff00) >> 8;
                        int blue = c & 0x000000ff;
                        Color color = new Color(red, green, blue);
                        int luminance = (int) ((color.getRed() * 0.3) + (color.getGreen() * 0.59)
                                + (color.getBlue() * 0.11));
                        dot = luminance > 127 ? 1 : 0;

                    }

                    abyte |= (byte) (dot << (7 - b)); // shift left,
                    // then OR together to get 8 bits into a byte
                }

                // System.out.print( (char)(abyte + 48 ) );
                // write here
                len++;
                fs.write(abyte);
            }
        }
        System.out.println("GW Length::"+len);
        // Assign memory position here
        // fs.write('/n');
        fs.write("/nP1".getBytes());
        fs.flush();
        // System.out.println(fs);
        return fs.toByteArray();
    }

将图像转换为二进制图形数据后,它不会打印数据.

如何让打印机打印图像?

使用jasper-reports,渲染图像,将图像转换为EPL并发送到zebra打印机,通常不是在热敏打印机上打印的正确解决方案.这种代码不仅速度较慢,而且您的分辨率也较低(例如条形码可能会产生问题)

您有两个标准选项

>使用打印机协议并发送文本文件.

>安装打印机驱动程序并使用 JRPrintServiceExporter

使用打印机协议

这就是我通常使用的(主要是为了获得完美的代码条打印,没有图像,但直接命令打印代码栏).您不会为此使用jasper-reports,而是设置您的txt文件(您可以在案例 zebra-designer
中使用设计程序),然后使用像 freemarker
这样的库来替换/插入直接协议文件中的动态数据.完成后,您可以直接将其发送到打印机,例如通过串口(也可以使用蓝牙 – 串行适配器无线)

用打印机驱动

在此解决方案中,您需要安装正确的打印机驱动程序,然后使用此驱动程序发送打印作业.在jasper-report发送打印机作业时,您使用以下代码:

PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
//set page size etc if you need to
PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
//set print service attributes
JRPrintServiceExporter exporter = new JRPrintServiceExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
SimplePrintServiceExporterConfiguration expConfig = new SimplePrintServiceExporterConfiguration();
String printerName = "myPrinterName"; //Check the name of printer
PrintService service = Printerlookup.getPrintservice(printerName, Boolean.TRUE, Boolean.TRUE);
expConfig.setPrintService(service);
expConfig.setPrintRequestAttributeSet(printRequestAttributeSet);
expConfig.setPrintServiceAttributeSet(printServiceAttributeSet);
expConfig.setDisplayPageDialog(Boolean.FALSE);
exporter.setConfiguration(expConfig);
exporter.exportReport();

如果您的打印方式不正确,则调试方法是 export to pdf
,然后使用pdf中的打印对话框打印到打印机(请记住您正在使用驱动程序,因此您可以在对话框中选择它)

随着pdf

>它打印正确! – 废话,这很奇怪,但你有一个解决方法(导出到PDF和打印)

>它打印不正确! – 废话,驱动程序不能正常工作(联系供应商),当他们正在工作时尝试不同的图像类型(我会尝试.bmp),检查打印机对话框中的所有设置(这些你可以设置为以后printRequestAttributeSet).

I don’t care, I’m actually only interested in converting png to EPL2 language

请更新问题,删除jasper,然后显示png图像,显示预期输出并显示当前输出,这是一些可以帮助您解决此问题的代码,但首先要确保您真的不关心:

Use ZEBRA SDK
参见printImage命令

ZebraPrinter printer = ZebraPrinterFactory.getInstance(connection);
printer.printImage("sample.jpg", x, y);

Using the EPL2 GW command
它在C#中,但语言类似

How to convert image to PCX
请参阅code.zip文件ToPCX.java

翻译自:https://stackoverflow.com/questions/46094778/zebra-printer-gc420t-not-printing-an-image-epl-2-gw

原文 

https://codeday.me/bug/20190111/503211.html

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。

PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处:Harries Blog™ » java – Zebra打印机GC420t无法打印图像EPL 2 GW

赞 (0)
分享到:更多 ()

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址