首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用CPCL在Zebra打印机上打印PCX图像

使用CPCL在Zebra打印机上打印PCX图像
EN

Stack Overflow用户
提问于 2014-01-25 05:32:20
回答 1查看 2.5K关注 0票数 3

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

在我看来,文档上的示例相当晦涩难懂。

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

希望能有所帮助。

EN

回答 1

Stack Overflow用户

发布于 2014-01-25 05:32:20

代码语言:javascript
复制
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) {
                }
            }
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21342869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档