由于公司一个地方需要用到图片转换ZPL格式去打印;这里的ZPL格式是打印斑马打印机,这里面有一些小的地方是需要注意的.

由于斑马打印机的型号不一样,设置不一样,外加自身的图片格式(.jpg,.png的不一样),图片的高度和宽度也不一样,就会直接导致打印出来的东西和你理想中的效果不一样,所以这里就需要设置图片的参数和斑马打印机自身的参数等设置。

然后下面贴出来图片转换为ZPL格式的代码,也可以当一个工具类来进行使用。

public class ZPLConveterUtils {private int blackLimit = 380;private int total;private int widthBytes;private boolean compressHex = false;private static Map<Integer, String> mapCode = new HashMap<Integer, String>();{mapCode.put(1, "G");mapCode.put(2, "H");mapCode.put(3, "I");mapCode.put(4, "J");mapCode.put(5, "K");mapCode.put(6, "L");mapCode.put(7, "M");mapCode.put(8, "N");mapCode.put(9, "O");mapCode.put(10, "P");mapCode.put(11, "Q");mapCode.put(12, "R");mapCode.put(13, "S");mapCode.put(14, "T");mapCode.put(15, "U");mapCode.put(16, "V");mapCode.put(17, "W");mapCode.put(18, "X");mapCode.put(19, "Y");mapCode.put(20, "g");mapCode.put(40, "h");mapCode.put(60, "i");mapCode.put(80, "j");mapCode.put(100, "k");mapCode.put(120, "l");mapCode.put(140, "m");mapCode.put(160, "n");mapCode.put(180, "o");mapCode.put(200, "p");mapCode.put(220, "q");mapCode.put(240, "r");mapCode.put(260, "s");mapCode.put(280, "t");mapCode.put(300, "u");mapCode.put(320, "v");mapCode.put(340, "w");mapCode.put(360, "x");mapCode.put(380, "y");mapCode.put(400, "z");}public void setCompressHex(boolean compressHex) {this.compressHex = compressHex;}public void setBlacknessLimitPercentage(int percentage) {this.blackLimit = (percentage * 768 / 100);}private String headDoc() {String str = "^XA " + "^FO0,0^GFA," + total + "," + total + "," + widthBytes + ", ";return str;}private String footDoc() {String str = "^FS" + "^XZ";return str;}/*** Image to zpi string data.** @param image* @return* @throws IOException*/public String convertfromImg(BufferedImage image) throws IOException {String cuerpo = createBody(image);if (compressHex) {cuerpo = encodeHexAscii(cuerpo,20);}return headDoc() + cuerpo + footDoc();}/*    public static void main(String[] args) throws Exception {resizeImage("image/gavin2019GY-HCK-21099224.jpg","image/cc.png",810,1220);File file = new File("image/cc.png");BufferedImage orginalImage = ImageIO.read(file);ZPLConveterUtils zpl = new ZPLConveterUtils();zpl.setCompressHex(true);zpl.setBlacknessLimitPercentage(50);String content = zpl.convertfromImg(orginalImage);JSONObject jsonObject = new JSONObject();jsonObject.put("str", content);System.out.println(jsonObject);}public static void resizeImage(String srcImgPath, String distImgPath,int width, int height) throws IOException {File srcFile = new File(srcImgPath);Image srcImg = ImageIO.read(srcFile);BufferedImage buffImg = null;buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);buffImg.getGraphics().drawImage(srcImg.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0,0, null);ImageIO.write(buffImg, "JPG", new File(distImgPath));}*//*** the BufferedImage to String** @param orginalImage* @return* @throws IOException*/private String createBody(BufferedImage orginalImage) throws IOException {StringBuffer sb = new StringBuffer();Graphics2D graphics = orginalImage.createGraphics();graphics.drawImage(orginalImage, 0, 0, null);int height = orginalImage.getHeight();int width = orginalImage.getWidth();// int newWidth = width + 200;int rgb, red, green, blue, index = 0;char auxBinaryChar[] = {'0', '0', '0', '0', '0', '0', '0', '0'};// widthBytes = width / 16 ;// widthBytes = width % 8 > 0 ? (((int) (width / 8)) + 1) : width / 8;widthBytes = width % 8 > 0 ? (((int) (width / 8)) + 1) : width / 8;this.total = widthBytes * height;for (int h = 0; h < height; h++) {for (int w = 0; w < width; w++) {rgb = orginalImage.getRGB(w, h);red = (rgb >> 16) & 0x000000FF;green = (rgb >> 8) & 0x000000FF;blue = (rgb) & 0x000000FF;char auxChar = '1';int totalColor = red + green + blue;if (totalColor > blackLimit) {auxChar = '0';}auxBinaryChar[index] = auxChar;index++;if (index == 8 || w == (width - 1)) {sb.append(fourByteBinary(new String(auxBinaryChar)));auxBinaryChar = new char[]{'0', '0', '0', '0', '0', '0', '0', '0'};index = 0;}}sb.append("\n");}return sb.toString();}/*** @param binaryStr* @return*/private String fourByteBinary(String binaryStr) {// int decimal = Integer.parseInt(binaryStr, 2);int decimal = Integer.parseInt(binaryStr, 2);return decimal > 15 ? Integer.toString(decimal, 16).toUpperCase() : "0" + Integer.toString(decimal, 16).toUpperCase();}/*** @param code* @return*/private String encodeHexAscii(String code) {int maxlinea = widthBytes * 2;StringBuffer sbCode = new StringBuffer();StringBuffer sbLinea = new StringBuffer();String previousLine = null;int counter = 1;char aux = code.charAt(0);boolean firstChar = false;for (int i = 1; i < code.length(); i++) {if (firstChar) {aux = code.charAt(i);firstChar = false;continue;}if (code.charAt(i) == '\n') {if (counter >= maxlinea && aux == '0') {sbLinea.append(",");} else if (counter >= maxlinea && aux == 'F') {sbLinea.append("!");} else if (counter > 20) {int multi20 = (counter / 20) * 20;int resto20 = (counter % 20);sbLinea.append(mapCode.get(multi20));if (resto20 != 0) {sbLinea.append(mapCode.get(resto20) + aux);} else {sbLinea.append(aux);}} else {sbLinea.append(mapCode.get(counter) + aux);if (mapCode.get(counter) == null) {}}counter = 1;firstChar = true;if (sbLinea.toString().equals(previousLine)) {sbCode.append(":");} else {sbCode.append(sbLinea.toString());}previousLine = sbLinea.toString();sbLinea.setLength(0);continue;}if (aux == code.charAt(i)) {counter++;} else {if (counter > 20) {int multi20 = (counter / 20) * 20;int resto20 = (counter % 20);sbLinea.append(mapCode.get(multi20));if (resto20 != 0) {sbLinea.append(mapCode.get(resto20) + aux);} else {sbLinea.append(aux);}} else {sbLinea.append(mapCode.get(counter) + aux);}counter = 1;aux = code.charAt(i);}}return sbCode.toString();}private String encodeHexAscii(String code,int number) {int maxlinea = widthBytes * 2;StringBuffer sbCode = new StringBuffer();StringBuffer sbLinea = new StringBuffer();String previousLine = null;int counter = 1;char aux = code.charAt(0);boolean firstChar = false;for (int i = 1; i < code.length(); i++) {if (firstChar) {aux = code.charAt(i);firstChar = false;continue;}if (code.charAt(i) == '\n') {if (counter >= maxlinea && aux == '0') {sbLinea.append(",");} else if (counter >= maxlinea && aux == 'F') {sbLinea.append("!");} else if (counter > number) {int multi20 = (counter / number) * number;int resto20 = (counter % number);sbLinea.append(mapCode.get(multi20));if (resto20 != 0) {sbLinea.append(mapCode.get(resto20) + aux);} else {sbLinea.append(aux);}} else {sbLinea.append(mapCode.get(counter) + aux);if (mapCode.get(counter) == null) {}}counter = 1;firstChar = true;if (sbLinea.toString().equals(previousLine)) {sbCode.append(":");} else {sbCode.append(sbLinea.toString());}previousLine = sbLinea.toString();sbLinea.setLength(0);continue;}if (aux == code.charAt(i)) {counter++;} else {if (counter > number) {int multi20 = (counter / number) * number;int resto20 = (counter % number);sbLinea.append(mapCode.get(multi20));if (resto20 != 0) {sbLinea.append(mapCode.get(resto20) + aux);} else {sbLinea.append(aux);}} else {sbLinea.append(mapCode.get(counter) + aux);}counter = 1;aux = code.charAt(i);}}return sbCode.toString();}

注意这里再重复下打印出来的效果不一样;导致的原因有可能:

 1 : 斑马打印机型号问题

 2 :  斑马打印机的参数设置问题

   3 :  图片自身的类型和图片的高度宽度等参数信息的设置

记一次图片转换ZPL格式(ZPL格式是用于斑马打印机)相关推荐

  1. Ubuntu 把 webp 格式图片转换成 jpeg/png 格式

    据统计,目前互联网上传输的数据有65%都是图片,为了减少数据量.加速网络传输.谷歌(google)于2010年推出的新一代图片格式 -- WebP 格式. WebP 格式是一种旨在加快图片加载速度的图 ...

  2. 如何把图片转换成一个PDF格式文件

    对于JPG和PDF文件,我们并不陌生,有时候,我们需要将JPG文件中的一张图像或者某一页中的部分图像合到一个PDF文件夹中,这样就会想到将JPG 转为PDF文件,有什么神奇能够帮助实现JPG转PDF呢 ...

  3. 利用PS把多张psd格式的图片转换为一张PDF格式

    最近为公司做了一版电子样册,所有图片都是包含多图层高清晰的psd格式,要做成一个PDF文件的电子样册,发给客户看,面对这些零散的图片,本来打算利用在线合成:在线网址 https://smallpdf. ...

  4. python修改文件格式为jpg_python将.ppm格式图片转换成.jpg格式文件的方法

    python将.ppm格式图片转换成.jpg格式文件的方法 将.ppm格式的图片转换成.jpg格式的图像,除了通过软件转换,还可以使用python脚本直接转换,so easy!!! from PIL ...

  5. 位图和矢量图格式有什么区别?如何一键把图片转换成矢量图?

    我们在做图片设计的时候经常会说到位图和矢量图格式,有时候在进行图片编辑时的素材往往尺寸较小,在进行放大或缩小后会降低图片的清晰度,这个时候经常会有人建议说把位图格式的图片转换成矢量图格式的图片会提高设 ...

  6. python制作ico图标_python使用PythonMagic k将jpg图片转换成ico图片的方法

    python使用PythonMagic k将jpg图片转换成ico图片的方法 发布时间:2017-06-27 08:00 来源:互联网 当前栏目:网页设计教程 这篇文章主要介绍了python使用Pyt ...

  7. 发送ZPL指令到斑马打印机,并监控打印成功或者失败的状态信息

    Visual C# 入门 本文共分为两个部分: 第一部分:介绍如何与Zebar进行连接,把ZPL指令或者模板文件发送到斑马打印机进行打印. 第二部分:介绍如何接收Zebar进行打印之后如何得到斑马打印 ...

  8. java png 转jpg_怎么用java将png图片转换成jpg格式的图片

    png是一种背景透明格式的图片,大量用于网络上,保真性很好,JPG是压缩图片,占用空间少.但有一些失真,所以在将png图片转换成jpg图片之后,肯定是有一些差异的,毕竟这是两种不同的格式. 下面用一个 ...

  9. python 怎么将数组转为列表_图片转换成pdf格式怎么操作?什么软件能将图片转为pdf?...

    伙伴们好,你们知道如何把图片转为pdf格式吗?前一阵子我参加了一个家居行业大会,在会议上拍摄了不少会议照片,包括主持人讲话.嘉宾出席.观众提问.产品推广等环节都拍摄了不同的角度.拍摄好后,需要传送给写 ...

最新文章

  1. 使用sqlite保存数据返回主键
  2. 2008_12_24_星期三
  3. jtoken判断是否包含键_Redis列表键(linkedlist/ziplist)的介绍
  4. 三星Galaxy S20:如何开启黑暗模式
  5. 天才程序员的传奇人生:项目被总统抢走,在瞄准镜下写完代码后被捕入狱
  6. html checkbox 多选 根据数据库来显示选取和未选取,前端框架(2)DIV多选复选框框的封装和MySql数据库存取...
  7. php网站入门鹿泉银山,银山门传说与银山门古石洞的考证
  8. gfsk调制频谱_gfsk调制方式
  9. 制作pdf文档书签,自动生成or根据目录生成
  10. 苹果数据线不能充电_去掉耳机和充电器,以后数据线苹果也不会送了
  11. C语言 求两个数最小公倍数和最大公因数
  12. vim keymap
  13. 用“等待-通知”机制优化循环等待
  14. 【通讯录教程】Excel表格批量手机号码导入安卓和苹果手机的通讯录,下面教你方法
  15. 微信小程序 - 基本原理
  16. vs中/EHa、/EHs、/EHsc的区别
  17. 红帽环境+docker安装
  18. 关于人机智能若干问题的思考
  19. 简单shell命令学习(一)
  20. java创建文件目录_java创建目录或文件夹的方法?

热门文章

  1. COMSOL多物理场/FDTD时域有限差分/ RSoft光电器件仿真设计“ 几十种案例解析,助您掌握光电器件仿真模拟
  2. U盘启动制作教程/实例 新手制作启动盘必看! [20081120]
  3. 通达信交易dll接口协议
  4. redis秒杀代码案例
  5. 恢弘志士之气,不宜妄自菲薄
  6. 雨中重装徒步攀登清凉峰
  7. 友善之臂 mini2440 linux led 驱动代码,友善之臂mini2440的LEDdriver驱动分析及测试程序...
  8. 调试血泪经验之uart/ttl/rs232电平转换问题
  9. 【Matlab指纹识别】指纹识别匹配门禁系统【含GUI源码 587期】
  10. 数字相控阵Matlab仿真