使用Java桌面应用程序打印CR80(智能卡大小)时出现问题。名为Fargo HDP5000的卡片打印机。打印机规格为300dpi。当我使用java getScaledInstance
或imgScalr
库调整/重新缩放图像到卡片尺寸(53.98mm x 85.6fmm)时,它可以正常打印。但结果变得很差或像素化。Java graphics2d使用卡片打印机将高分辨率图像打印到CR80卡片
但是,当我尝试使用Windows打印对话框打印高分辨率图像时,结果非常好而且清晰。
任何解决方案如何在这样的小媒体中使用Java应用程序打印高分辨率图像? 谢谢
这里是我的代码:
try {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintService(printService);
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(OrientationRequested.PORTRAIT);
aset.add(new PrinterResolution(300, 300, PrinterResolution.DPI));
aset.add(new MediaPrintableArea(0.0f, 0.0f, 53.98f, 85.6f, MediaSize.MM));
if (duplex) {
aset.add(Sides.DUPLEX);
mDuplex = true;
} else {
aset.add(Sides.ONE_SIDED);
}
printJob.setPrintable(this);
printJob.print(aset);
} catch (Exception PrintException) {
Helper.errorHandling(TAG, "printDemo", PrintException.toString());
}
实现:
@Override
public int print(Graphics g, PageFormat pageFormat, int pageNumber) {
if (pageNumber > 1) {
System.out.println("Print job has been sent to spooler.");
return Printable.NO_SUCH_PAGE;
}
Graphics2D graphics2D = (Graphics2D) g;
graphics2D.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
final int printerDPI = 300;
double pageWidth = pageFormat.getImageableWidth() * printerDPI/POINTS_PER_INCH; //PPI = 72
double pageHeight = pageFormat.getImageableHeight() * printerDPI/POINTS_PER_INCH;
if (pageNumber == 0) {
final int x= 0;
final int y= 0;
final int FIXED_WIDTH = 504;
final int FIXED_HEIGHT = 808;
BufferedImage bimg;
try {
bimg = ImageIO.read(new File(filenamePath));
} catch (Exception e) {
Helper.errorHandling(e.toString());
bimg = null;
}
if (bimg == null) {
return;
}
int w = bimg.getWidth();
int h = bimg.getHeight();
int destW = (int) (w * 0.3) + x;
int destH = (int) (h * 0.3) + y;
if (w > pageWidth) {
destW = pageWidth;
}
if (h > pageHeight) {
destH = pageHeight;
}
Image img;
if (w != FIXED_WIDTH && h != FIXED_HEIGHT) {
img = bimg.getScaledInstance(FIXED_WIDTH, FIXED_HEIGHT, Image.SCALE_SMOOTH);
// or
img = Scalr.resize(bimg, Scalr.Method.ULTRA_QUALITY, FIXED_WIDTH, FIXED_HEIGHT);
graphics2D.drawImage(img, x, y, destW, destH, 0, 0, w, h, null);
} else {
graphics2D.drawImage(bimg, x, y, destW, destH, 0, 0, w, h, null);
}
return (Printable.PAGE_EXISTS);
} else {
return (NO_SUCH_PAGE);
}
}
这里的一对“我如何在Java中调整图像大小”(https://stackoverflow.com/questions/答案244164/how-can-i-resize-an-image-using-java/4528136#4528136) - 什么似乎是问题.... –