Datamax价签打印

本文介绍了一种使用Java实现Datamax打印机打印功能的方法。通过创建DatamaxPrinter类,利用Java AWT和Java Print Service API实现了向指定型号的Datamax打印机发送打印任务的功能,并详细展示了如何生成包含特定内容的位图图像并将其发送到打印机。
摘要由CSDN通过智能技术生成
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.ByteArrayOutputStream;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;

public class DatamaxPrinter {
	public static void main(String[] args) {
		try {
			HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
			DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
			PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor,
					pras);
			for (int i = 0; i < printService.length; i ++) {
				PrintService ps = printService[i];
				System.out.println(ps.getName());
			}

			DatamaxPrinter dp = new DatamaxPrinter("Datamax M-4206 Mark II");
			dp.print(2);
		} catch (Exception ex) {
		}
	}

	private final static String fileName = "fr0";

	private String printerName = null;

	public DatamaxPrinter(String printerName) {
		this.printerName = printerName;
	}

	/*
	 * -1:打印机错误;-2:系统错误;1:打印成功
	 */
	public int print(int quantity) {
		try {
			HashPrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
			DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
			PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
			for (int i = 0; i < printService.length; i ++) {
				PrintService ps = printService[i];
				if (ps.getName().equals(printerName)) {
					DocPrintJob job = ps.createPrintJob();
					byte[] buff = getDataBytes(quantity);
					if (buff == null) {
						return -2;
					}

					InputStream fis = new ByteArrayInputStream(buff);
					DocAttributeSet das = new HashDocAttributeSet();
					Doc doc = new SimpleDoc(fis, flavor, das);
					job.print(doc, pras);

					return 1;
				}
			}
		} catch (Exception ex) {
		}
		return -1;
	}

	private byte[] getDataBytes(int quantity) {
		try {
			byte[] buff = getImageBytes();
			if (buff == null) {
				return null;
			}
			ByteArrayOutputStream out = new ByteArrayOutputStream();

			out.write(getSTXBytes("n"));// 英制
			out.write(getSTXBytes("M0500"));
			out.write(getSTXBytes("V0"));
			out.write(getSTXBytes("SK"));
			out.write(getSTXBytes("d"));
			out.write(getSTXBytes("KcRIG0;"));
			out.write(getSTXBytes("ICB" + fileName));// 上传文件
			out.write(buff);
			out.write(getSTXBytes("L"));// Enter Label Formatting Command Mode
			out.write(getCommandBytes("D11"));// Set Dot Size Width and Height
			out.write(getCommandBytes("PK"));// 打印速度
			out.write(getCommandBytes("pG"));// 回滚速度
			out.write(getCommandBytes("SK"));// feed rate
			out.write(getCommandBytes("A2"));// 格式
			out.write(getCommandBytes("1Y1100000000000" + fileName));// 打印文件
			StringWriter ss = new StringWriter();
			new PrintWriter(ss).format("Q%04d", quantity);// 份数
			out.write(getCommandBytes(ss.toString()));
			out.write(getCommandBytes("E"));// 指令结束
			out.write(getSTXBytes("xCG" + fileName));// 删除文件
			out.write(getSTXBytes("zC"));// Pack Module 回收空间
			out.flush();
			return out.toByteArray();
		} catch (Exception ex) {
		}
		return null;
	}

	private static final int imageYShift = -5;
	private static final int imageXShift = 5;

	private byte[] getImageBytes() throws Exception {
		BufferedImage mainImage = new BufferedImage(450, 283, BufferedImage.TYPE_BYTE_BINARY);
		Graphics2D g2D = (Graphics2D) mainImage.getGraphics();

		g2D.setBackground(Color.WHITE);
		g2D.clearRect(0, 0, 450, 283);
		g2D.setPaint(Color.BLACK);

		/*
		 * 打印品名
		 */
		String name = "品名";
		Font font = new Font("黑体", Font.BOLD, 48);
		g2D.setFont(font);
		FontRenderContext context = g2D.getFontRenderContext();
		Rectangle2D bounds = font.getStringBounds(name, context);
		int w = (int) bounds.getWidth() + 1;
		int h = (int) bounds.getHeight() + 1;
		int xp = 110;

		if (w <= 339) {
			g2D.drawString(name, (int) (xp + (339 - w) / 2) + imageXShift, 22 + h - 5 + imageYShift);
		} else if (w <= 339 * 1.5) {
			BufferedImage mainImage_temp = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);
			Graphics2D g2_temp = (Graphics2D) mainImage_temp.getGraphics();
			g2_temp.setBackground(Color.WHITE);
			g2_temp.clearRect(0, 0, w, h);
			g2_temp.setPaint(Color.BLACK);
			g2_temp.setFont(font);
			g2_temp.drawString(name, 0, h - 6);
			g2D.drawImage(mainImage_temp, xp + imageXShift, 18 + imageYShift, 339, h, null);
		} else {
			font = new Font("黑体", Font.BOLD, 28);
			g2D.setFont(font);

			context = g2D.getFontRenderContext();
			bounds = font.getStringBounds(name, context);
			h = (int) bounds.getHeight() + 1;
			w = (int) bounds.getWidth() + 1;

			float fontRate = 1.15f;
			if (w <= 339 * 2) {
				fontRate = 1f;
			}
			String temp0 = name;
			String temp1 = "";
			String temp2 = "";
			for (int n = 0; n < temp0.length(); n ++) {
				temp1 += temp0.charAt(n);
				int l = g2D.getFontMetrics().stringWidth(temp1);
				if (l > 339 * fontRate) {
					temp1 = temp0.substring(0, n);
					temp2 = temp0.substring(n);
					break;
				}
			}
			w = (int) (g2D.getFontMetrics(font).stringWidth(temp1));
			BufferedImage mainImage_temp = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);
			Graphics2D g2_temp = (Graphics2D) mainImage_temp.getGraphics();
			g2_temp.setBackground(Color.WHITE);
			g2_temp.clearRect(0, 0, w, h);
			g2_temp.setPaint(Color.BLACK);
			g2_temp.setFont(font);
			g2_temp.drawString(temp1, 0, h - 3);
			if (temp2.length() > 0) {
				g2D.drawImage(mainImage_temp, xp + imageXShift, 21 + imageYShift, (int) (w / fontRate), 32, null);

				w = (int) (g2D.getFontMetrics(font).stringWidth(temp2));
				mainImage_temp = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);
				g2_temp = (Graphics2D) mainImage_temp.getGraphics();
				g2_temp.setBackground(Color.WHITE);
				g2_temp.clearRect(0, 0, w, h);
				g2_temp.setPaint(Color.BLACK);
				g2_temp.setFont(font);
				g2_temp.drawString(temp2, 0, h - 3);
				g2D.drawImage(mainImage_temp, xp + imageXShift, 21 + h + imageYShift, (int) (w / fontRate), 32, null);
			} else {
				g2D.drawImage(mainImage_temp, (int) (xp + (339 - w / fontRate) / 2) + imageXShift, 21 + imageYShift,
						(int) (w / fontRate),
						(int) (32 * 1.5), null);
			}
		}
		// 品名打印结束

		font = new Font("黑体", Font.BOLD, 17);
		g2D.setFont(font);
		g2D.drawString("见包装", 67 + imageXShift, 110 + imageYShift);// 产地
		g2D.drawString("合格 040", 67 + imageXShift, 147 + imageYShift);// 等级
		g2D.drawString("袋", 67 + imageXShift, 215 + imageYShift);// 单位

		font = new Font("monospace", Font.BOLD, 17);
		g2D.setFont(font);
		g2D.drawString("12345678", 67 + imageXShift, 177 + imageYShift);// 规格

		String mmailNO = "100";// 快讯号
		/*
		 * 打印价格
		 */
		font = new Font("微软雅黑", Font.BOLD, 80);
		bounds = font.getStringBounds("9.90", context);
		w = (int) bounds.getWidth();
		h = (int) bounds.getHeight() - 40;
		BufferedImage mainImage_temp = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_BINARY);
		Graphics2D g2_temp = (Graphics2D) mainImage_temp.getGraphics();
		g2_temp.setBackground(Color.WHITE);
		g2_temp.clearRect(0, 0, w, h);
		g2_temp.setPaint(Color.BLACK);
		g2_temp.setFont(font);
		g2_temp.drawString("9.90", (float) 1, h - 1);

		if (w < 240) {
			if (mmailNO != null && mmailNO.length() > 0) {
				g2D.drawImage(mainImage_temp, (int) (202 + (240 - w) / 2) + imageXShift, 110 + imageYShift, null);
			} else {
				g2D.drawImage(mainImage_temp, (int) (202 + (240 - w) / 2) + imageXShift, 114 + imageYShift, null);
			}
		} else {
			if (mmailNO != null && mmailNO.length() > 0) {
				g2D.drawImage(mainImage_temp, 202 + imageXShift, 110 + imageYShift, 240, h, null);
			} else {
				g2D.drawImage(mainImage_temp, 202 + imageXShift, 114 + imageYShift, 240, h, null);
			}
		}

		/*
		 * 打印条码
		 */
		String barcode = "6938498204102";
		if (mmailNO != null && mmailNO.length() > 0) {
			paint(barcode, g2D, 200 + imageXShift, 180 + imageYShift, 240, 30);
		} else {
			paint(barcode, g2D, 200 + imageXShift, 180 + 12 + imageYShift, 240, 30);
		}

		font = new Font("monospace", Font.BOLD, 15);
		g2D.setFont(font);
		String temp = "123456" + "  " + "1234" + "  " + "111" + "  "
				+ new java.text.SimpleDateFormat("yy/MM/dd HH:mm").format(new java.util.Date());
		if (mmailNO != null && mmailNO.length() > 0) {
			g2D.drawString(temp, 200 + imageXShift, 239 + imageYShift);
		} else {
			g2D.drawString(temp, 200 + imageXShift, 252 + imageYShift);
		}

		if (mmailNO != null && mmailNO.length() > 0) {
			temp = "时间: " + "2018-01-01" + " 至 " + "2018-01-02";
			font = new Font("monospace", Font.BOLD, 12);
			g2D.setFont(font);
			g2D.drawString(temp, 212 + imageXShift, 255 + imageYShift);
		}

		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ImageIO.write(mainImage, "bmp", out);
		// ImageIO.write(mainImage, "bmp", new java.io.FileOutputStream("d:/test.bmp"));
		return out.toByteArray();
	}

	private byte[] getSTXBytes(String command) {
		byte[] comms = command.getBytes();

		byte[] buffer = new byte[comms.length + 1 + 2];
		buffer[0] = (byte) 0x02;
		for (int i = 0; i < comms.length; i ++) {
			buffer[i + 1] = comms[i];
		}
		buffer[buffer.length - 1] = (byte) 0x0D;
		buffer[buffer.length - 1] = (byte) 0x0A;

		return buffer;
	}

	private byte[] getCommandBytes(String command) {
		byte[] comms = command.getBytes();
		byte[] buffer = new byte[comms.length + 2];
		for (int i = 0; i < comms.length; i ++) {
			buffer[i] = comms[i];
		}
		buffer[buffer.length - 1] = (byte) 0x0D;
		buffer[buffer.length - 1] = (byte) 0x0A;

		return buffer;
	}

	private final static String[] codeSet = { "11011001100", "11001101100", "11001100110", "10010011000", "10010001100",
			"10001001100", "10011001000",
			"10011000100",
			"10001100100", "11001001000", "11001000100", "11000100100", "10110011100", "10011011100", "10011001110",
			"10111001100", "10011101100", "10011100110",
			"11001110010", "11001011100", "11001001110", "11011100100", "11001110100", "11101101110", "11101001100",
			"11100101100", "11100100110", "11101100100",
			"11100110100", "11100110010", "11011011000", "11011000110", "11000110110", "10100011000", "10001011000",
			"10001000110", "10110001000", "10001101000",
			"10001100010", "11010001000", "11000101000", "11000100010", "10110111000", "10110001110", "10001101110",
			"10111011000", "10111000110", "10001110110",
			"11101110110", "11010001110", "11000101110", "11011101000", "11011100010", "11011101110", "11101011000",
			"11101000110", "11100010110", "11101101000",
			"11101100010", "11100011010", "11101111010", "11001000010", "11110001010", "10100110000", "10100001100",
			"10010110000", "10010000110", "10000101100",
			"10000100110", "10110010000", "10110000100", "10011010000", "10011000010", "10000110100", "10000110010",
			"11000010010", "11001010000", "11110111010",
			"11000010100", "10001111010", "10100111100", "10010111100", "10010011110", "10111100100", "10011110100",
			"10011110010", "11110100100", "11110010100",
			"11110010010", "11011011110", "11011110110", "11110110110", "10101111000", "10100011110", "10001011110",
			"10111101000", "10111100010", "11110101000",
			"11110100010", "10111011110", "10111101110", "11101011110", "11110101110", "11010000100", "11010010000",
			"11010011100", "11000111010" };

	private String encode(String str) {
		int n = 104;
		byte[] bs = str.getBytes();
		for (int i = 0; i < str.length(); i ++) {
			n += (i + 1) * (bs[i] - 32);
		}
		String vs = codeSet[n % 103];

		StringBuffer buff = new StringBuffer();
		buff.append("11010010000");
		for (int i = 0; i < str.length(); i ++) {
			buff.append(codeSet[bs[i] - 32]);
		}
		buff.append(vs);
		buff.append("1100011101011");
		return buff.toString();
	}

	public void paint(String str, Graphics2D g2d, int x, int y, int w, int h) {
		String code = encode(str);

		g2d.setBackground(Color.WHITE);
		g2d.setPaint(Color.BLACK);
		Font f = new Font("monospace", Font.BOLD, 16);
		g2d.setFont(f);
		int cw = g2d.getFontMetrics().stringWidth("0");

		int j = 0;
		int wRate = 1;
		for (int i = 0; i < code.length(); i ++) {
			if (code.charAt(i) == '1') {
				g2d.setColor(Color.BLACK);
			} else {
				g2d.setColor(Color.WHITE);
			}
			g2d.fillRect(j + x + (w - code.length() * wRate) / 2, y, wRate, h);
			j += wRate;
		}

		float xp = x + (w - code.length() * wRate) / 2;
		float yp = y + h + 15;
		float ww = (float) code.length() * wRate / (float) str.length();
		for (int i = 0; i < str.length(); i ++) {
			g2d.drawString(str.substring(i, i + 1), xp + (ww - cw) / 2, yp);
			xp += ww;
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值