1.添加zebra_api的jar包到自己的项目中
- 当然也得有ZebraNativeUsbAdapter_32.dll jdk使用的是64位使用ZebraNativeUsbAdapter_64.dll
2.连接打印机
import com.zebra.sdk.comm.*;
import com.zebra.sdk.graphics.ZebraImageFactory;
import com.zebra.sdk.graphics.ZebraImageI;
import com.zebra.sdk.printer.PrinterStatus;
import com.zebra.sdk.printer.ZebraPrinter;
import com.zebra.sdk.printer.ZebraPrinterFactory;
import com.zebra.sdk.printer.ZebraPrinterLanguageUnknownException;
import com.zebra.sdk.printer.discovery.*;
public class printHandler {
private static ZebraPrinter zebraPrinter = null;
//单次打印只给一个对象,防止打印机线程堵塞
public static void initPrinter()throws Exception{
if(null==zebraPrinter){
DiscoveredPrinter[] printers = null;
Connection connection = null;
ByteArrayOutputStream baos = null;
printers = UsbDiscoverer.getZebraUsbPrinters(new ZebraPrinterFilter());
connection = printers[0].getConnection();
System.out.println(connection.toString());
connection.open();
zebraPrinter = ZebraPrinterFactory.getInstance(connection);
System.out.println("获取到实例---"+zebraPrinter.toString());
}
}
3.使用zebraPrinter.printImage()进行打印
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageOutputStream imageOutput = ImageIO.createImageOutputStream(byteArrayOutputStream);
ImageIO.write(image, "jpg", imageOutput);
//这里的image需要是BufferedImage类型的,需要自己构造
InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
ZebraImageI image2 = ZebraImageFactory.getImage((inputStream));
System.out.println("--开始打印--");
zebraPrinter.printImage(image2, 0, 0, 450,320, false);
System.out.println("--打印完成--");
- 当然也可以用java自带的进行打印,创建打印任务
public static void JPGPrint(InputStream inputStream,float width,float height) throws PrintException {
try { // 设置打印格式,如果未确定类型,可选择autosense
DocFlavor flavor = DocFlavor.INPUT_STREAM.JPEG; // 设置打印参数
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(1)); //份数 //
//aset.add(MediaSize.ISO.A0); //纸张
aset.add(new MediaPrintableArea(0f,0f,width, height, Size2DSyntax.MM));
//aset.add(Finishings.STAPLE);//装订
aset.add(Sides.ONE_SIDED);//单双面 // 定位打印服务
PrintService printService = null;
PrintService[] printServices = PrinterJob.lookupPrintServices();
if(printServices == null || printServices.length == 0) {
System.out.print("打印失败,未找到可用打印机,请检查。");
return ;
} //匹配指定打印机
for (int i = 0;i < printServices.length; i++) {
//System.out.println(printServices[i].getName());
if (printServices[i].getName().matches(".*ZDesigner.*")) {
printService = printServices[i];
break;
}
}
if(printService==null){
System.out.print("打印失败,未找到打印机,请检查。");
return ;
}
Doc doc = new SimpleDoc(inputStream, flavor, null);
DocPrintJob job = printService.createPrintJob(); // 创建打印作业
job.print(doc, aset);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
- 使用BufferedImage构图
public static void print(){
try{
画图
int imageWidth=120;
int imageHeight=125;
BufferedImage bufferedImage =new BufferedImage(imageWidth,imageHeight, BufferedImage.TYPE_INT_RGB);
Graphics graphics = bufferedImage.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, imageWidth, imageHeight);
graphics.setColor(Color.BLACK);
graphics.drawString("测试1", 2, 20);
graphics.drawString("测试2", 2, 40);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ImageOutputStream imageOutput = ImageIO.createImageOutputStream(byteArrayOutputStream);
ImageIO.write(bufferedImage, "jpg", imageOutput);
InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
JPGPrint(inputStream,76,58);
// FileOutputStream fos = new FileOutputStream("D:/b.jpg");
// byte[] b = new byte[1024];
// while ((inputStream.read(b)) != -1) {
// fos.write(b);// 写入数据
// }
// inputStream.close();
// fos.close();
}catch(Exception e){
e.printStackTrace();
}
}
再丢个盘
链接:https://pan.baidu.com/s/18ziv1xHIfe6cizFQ1lLdFQ?pwd=8lr7
提取码:8lr7
--来自百度网盘超级会员V3的分享
api下载链接:
https://download.csdn.net/download/weixin_44830864/87713551https://download.csdn.net/download/weixin_44830864/87713551dll包下载链接:
https://download.csdn.net/download/weixin_44830864/87719203https://download.csdn.net/download/weixin_44830864/87719203