【问题标题】:Printing PCX images on Zebra Printers using CPCL使用 CPCL 在 Zebra 打印机上打印 PCX 图像
【发布时间】:2014-02-16 01:10:40
【问题描述】:

我花了很多时间来了解如何在 Zebra 打印机(通过网络)上使用 CPCL 打印 PCX 图像,而无需将图像下载到打印机。

在我看来,文档中的示例非常模糊。

我附上了一个示例类来展示如何简单地打印图像。 它需要在你的类路径中有一个“zebra.pcx”图像。

希望对你有帮助。

【问题讨论】:

    标签: java zebra-printers pcx


    【解决方案1】:
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.Socket;
    
    public class PrintZebraPCXImage {
    
        public static void main(String[] args) throws Exception {
            PrintZebraPCXImage instance = new PrintZebraPCXImage();
            instance.print("192.168.1.133", 6101);
        }
    
        public void print(String address, int port) throws Exception {
            Socket socket = null;
            DataOutputStream stream = null;
    
            socket = new Socket(address, port);
    
            try {
                stream = new DataOutputStream(socket.getOutputStream());
                ByteArrayOutputStream bos = readFileToString(this.getClass().getClassLoader().getResourceAsStream("zebra.pcx"));
    
                stream.writeBytes("! 0 200 200 300 1\r\n");
                stream.writeBytes("PCX 20 0\r\n");
    
                stream.write(bos.toByteArray());
                stream.writeBytes("PRINT\r\n");
    
            } finally {
                if (stream != null) {
                    stream.close();
                }
                if (socket != null) {
                    socket.close();
                }
            }
        }
    
        public ByteArrayOutputStream readFileToString(InputStream is) {
            InputStreamReader isr = null;
            ByteArrayOutputStream bos = null;
            try {
                isr = new InputStreamReader(is);
                bos = new ByteArrayOutputStream();
    
                byte[] buffer = new byte[2048];
                int n = 0;
                while (-1 != (n = is.read(buffer))) {
                    bos.write(buffer, 0, n);
                }
    
                return bos;
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                if (isr != null) {
                    try {
                        isr.close();
                    } catch (IOException e) {
                    }
                }
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-03-04
      • 1970-01-01
      • 2014-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多