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

栏目: Java · 发布时间: 5年前

内容简介:翻译自:https://stackoverflow.com/questions/46094778/zebra-printer-gc420t-not-printing-an-image-epl-2-gw
使用JasperReport生成图像,然后尝试在Zebra打印机 GC420t 上打印该图像.生成图像但不打印.我已经仔细检查了连接和端口.我读过 this SO linkthe 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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Joy of X

The Joy of X

Niall Mansfield / UIT Cambridge Ltd. / 2010-7-1 / USD 14.95

Aimed at those new to the system seeking an overall understanding first, and written in a clear, uncomplicated style, this reprint of the much-cited 1993 classic describes the standard windowing syste......一起来看看 《The Joy of X》 这本书的介绍吧!

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具