使用JasperReport生成图像,然后尝试在Zebra打印机GC420t上打印该图像。生成图像但不打印。我已经仔细检查了连接和端口。我已经阅读了this SO link和the calibration一个,但没有任何效果。
代码:
public void generateReport(Map<String, Object> parameters, List<Label> labels)
throws JRException, IOException, ConnectionException, ZebraPrinterLanguageUnknownException{
// TODO Auto-generated method stub
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(labels);
System.out.println(" Wait !!");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource);
if(jasperPrint != null && jasperPrint.getPages()!=null && jasperPrint.getPages().size()>=0){
FileOutputStream fos = new FileOutputStream("C:\\Users\\desktop\\Labels.png");
//JasperExportManager.exportReportToPdfStream(jasperPrint, fos);
BufferedImage rendered_image = null;
rendered_image = (BufferedImage) JasperPrintManager.printPageToImage(jasperPrint, 0, 1.6f);
ImageIO.write(rendered_image, "png", fos);
Connection thePrinterConn = new DriverPrinterConnection("GC420t");
try{
for (DiscoveredPrinterDriver printer : UsbDiscoverer.getZebraDriverPrinters()){
System.out.println(printer);
}
thePrinterConn.open();
if(zPrinter==null){
zPrinter = ZebraPrinterFactory.getInstance(thePrinterConn);
}
PrinterStatus printerStatus = zPrinter.getCurrentStatus();
if(printerStatus.isReadyToPrint){
System.out.println("Ready to print !!");
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
DocAttributeSet das = new HashDocAttributeSet();
FileInputStream fis = new FileInputStream("C:\\Users\\desktop\\Labels.png");
Doc mydoc = new SimpleDoc(fis, flavor, das);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(OrientationRequested.PORTRAIT);
@SuppressWarnings("unused")
PrinterJob pj = PrinterJob.getPrinterJob();
PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, aset);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
for (int i = 0; i < services.length; i++){
System.out.println(services[i].getName());
}
if(services.length == 0){
if(defaultService == null){
//no printer found
}
else{
//print using default
DocPrintJob job = defaultService.createPrintJob();
try{
job.print(mydoc, aset);
}catch (PrintException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
else{
PrintService service = ServiceUI.printDialog(null, 200, 200, services, defaultService, flavor, aset);
if (service != null){
DocPrintJob job = service.createPrintJob();
try{
job.print(mydoc, aset);
}catch(PrintException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//ZebraImageI image = ZebraImageFactory.getImage("C:\\Users\\desktop\\Labels.png");
}
else{
System.out.println("Something went wrong");
}
}finally{
thePrinterConn.close();
}
System.out.println("Report generated !!");
}
}
我读了EPL 2 manual并将图像转换为二进制图形数据以便立即打印。
代码:
private byte[] getEplGraphics(int top, int left, BufferedImage bufferedImage) throws IOException {
ByteArrayOutputStream fs = new ByteArrayOutputStream();
//int canvasWidth = bufferedImage.getWidth();
// loop from top to bottom
System.out.println(bufferedImage.getHeight());
System.out.println(bufferedImage.getWidth());
int maxY = bufferedImage.getHeight() + (64- bufferedImage.getHeight()%64);
int maxX = bufferedImage.getWidth() + (64- bufferedImage.getWidth()%64);
System.out.println(maxX);
System.out.println(maxY);
int p3 = maxX / 8;
int p4 = maxY/ 8;
int len = 0;
String gw = "N\nGW0,0," + p3 + "," + p4 + ",";
fs.write(gw.getBytes());
for (int y = 0; y < maxY; ++y) {
// from left to right
for (int x = 0; x < maxX;) {
byte abyte = 0;
// get 8 bits together and write to memory
for (int b = 0; b < 8; ++b, ++x) {
// set 1 for white,0 for black
int dot = 1;
// pixel still in width of bitmap,
// check luminance for white or black, out of bitmap set to white
if (x < bufferedImage.getWidth() && y < bufferedImage.getHeight()) {
int c = bufferedImage.getRGB(x, y);
int red = (c & 0x00ff0000) >> 16;
int green = (c & 0x0000ff00) >> 8;
int blue = c & 0x000000ff;
Color color = new Color(red, green, blue);
int luminance = (int) ((color.getRed() * 0.3) + (color.getGreen() * 0.59)
+ (color.getBlue() * 0.11));
dot = luminance > 127 ? 1 : 0;
}
abyte |= (byte) (dot << (7 - b)); // shift left,
// then OR together to get 8 bits into a byte
}
// System.out.print( (char)(abyte + 48 ) );
// write here
len++;
fs.write(abyte);
}
}
System.out.println("GW Length::"+len);
// Assign memory position here
// fs.write('\n');
fs.write("\nP1".getBytes());
fs.flush();
// System.out.println(fs);
return fs.toByteArray();
}
将图像转换为二进制图形数据后,它不会打印数据。
如何让打印机打印图像?
答案 0 :(得分:1)
使用jasper-reports,渲染图像,将图像转换为EPL并发送到zebra打印机,通常不是在热敏打印机上打印的正确解决方案。这种代码不仅速度较慢,而且您的分辨率也较低(例如条码可能会产生问题)
您有两个标准选项
使用打印机协议并发送文本文件。
安装打印机驱动程序并使用JRPrintServiceExporter
使用打印机协议
这是我通常使用的(主要是为了完美地打印代码条,没有图像,但是直接命令打印代码栏)。您不会为此使用jasper-reports,而是设置txt
文件(您可以在案例zebra-designer中使用设计程序),然后使用freemarker之类的库来替换/在直接协议文件中插入动态数据。完成后,您可以直接将其发送到打印机,例如通过串口(也可以使用蓝牙 - 串口适配器无线)
使用打印机驱动程序
在此解决方案中,您需要安装正确的打印机驱动程序,然后使用此驱动程序发送打印作业。在用于发送打印机作业的jasper-report中,您使用以下代码:
PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
//set page size etc if you need to
PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
//set print service attributes
JRPrintServiceExporter exporter = new JRPrintServiceExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
SimplePrintServiceExporterConfiguration expConfig = new SimplePrintServiceExporterConfiguration();
String printerName = "myPrinterName"; //Check the name of printer
PrintService service = Printerlookup.getPrintservice(printerName, Boolean.TRUE, Boolean.TRUE);
expConfig.setPrintService(service);
expConfig.setPrintRequestAttributeSet(printRequestAttributeSet);
expConfig.setPrintServiceAttributeSet(printServiceAttributeSet);
expConfig.setDisplayPageDialog(Boolean.FALSE);
exporter.setConfiguration(expConfig);
exporter.exportReport();
如果您的打印方式不正确,则调试方法为export to pdf,然后使用pdf中的打印对话框打印到打印机(请记住您正在使用驱动程序,因此可以在对话框中选择它)
使用pdf
打印正确! - 废话,这很奇怪,但你有一个解决方法(导出到pdf并打印)
打印不正确! - 废话,驱动程序不能正常工作(联系供应商),当他们正在工作时尝试不同的图像类型(我会尝试.bmp
),检查打印机对话框中的所有设置(这些你可以设置稍后到printRequestAttributeSet
)。
我不在乎,我实际上只对将png转换为EPL2语言感兴趣
请更新问题,删除jasper,然后显示png
图片,显示预期输出并显示当前输出,这是一些可以帮助您解决此问题的代码,但请务必先确定您是否真的没有关心:
Use ZEBRA SDK请参阅printImage
命令
ZebraPrinter printer = ZebraPrinterFactory.getInstance(connection);
printer.printImage("sample.jpg", x, y);
Using the EPL2 GW command它在C#中,但语言相似
How to convert image to PCX请参阅code.zip文件ToPCX.java