java - 以编程方式调整大小并以高分辨率打印图像
问题描述
最近我一直在尝试打印图像。在 Photoshop 中,我可以通过给出以毫米为单位的测量值和 dpi 值来调整(缩小)图像的大小(我的打印机声称它可以处理高达 2400 dpi,所以我将其设置为该值)。当我使用 Photoshop 打印时,这会以正确的尺寸提供惊人的清晰效果。
然而,这一切都是手动完成的,并且对于大量图像来说是相当劳动密集型的,所以我希望能够以编程方式做到这一点
到目前为止我得到的结果包括图像的巨大截断版本或小(但甚至不接近所需尺寸)并且质量仍然很差(好像它确实调整了大小但保持低 dpi)
我不确定我是否朝着正确的方向前进。我的代码有问题吗?我应该尝试完全不同的东西吗?
我已经尝试过这篇文章中的方法(并尝试改变它们): java setting resolution and print size for an Image
private static class MyPrintJob{
static PrinterJob myJob;
static double dpi;
static double dpCm;
static Paper myPaper;
static PageFormat myPageFormat;
static double cardHeightInCm;
static double cardWidthInCm;
static ArrayList<BufferedImage> imageList;
MyPrintJob(){
myJob = PrinterJob.getPrinterJob();
dpi = 2400;
dpCm = dpi/2.54;
myPaper = new Paper();
myPaper.setSize(21.3 * dpCm, 29.7 * dpCm);
myPaper.setImageableArea(0, 0, 21.3 * dpCm, 29.7 * dpCm);
myPageFormat = myJob.defaultPage();//new PageFormat();
myPageFormat.setPaper(myPaper);
imageList = new ArrayList<>();
cardHeightInCm = 8;
cardWidthInCm = 6;
}
static void resizeForDpi(BufferedImage input, Graphics2D g, double dpi){
BufferedImage theImage = input;
AffineTransform theAT = g.getTransform();
double theScaleFactor = (72d / dpi);
g.scale(theScaleFactor, theScaleFactor);
g.drawRenderedImage(theImage, null);
g.setTransform(theAT);
}
void execute(){
myJob.setPrintable(new PrintableJob(), myPageFormat);
if (myJob.printDialog()) {
try {
HashPrintRequestAttributeSet set = new HashPrintRequestAttributeSet();
PrinterResolution pr = new PrinterResolution((int) (dpCm), (int) (dpCm), ResolutionSyntax.DPCM);//was dpi
set.add(pr);
myJob.setJobName("Jobname");
myJob.print(set);
} catch (PrinterException e) {
}
}
}
static class PrintableJob implements Printable{
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
Graphics2D g2 = (Graphics2D) graphics;
int totalPages = 1;
if (pageIndex < totalPages) {
g2.setColor(Color.LIGHT_GRAY);
System.out.println("dpcm: " + dpCm + "dpi: " + dpi);
System.out.println("should draw rect");
// Draw Page Header
//g2.fillRect(0, 0, (int)(dpCm), (int)(dpCm));
BufferedImage printCard = resizeToCard(activeCard.myImage);
resizeForDpi(printCard,g2,2400);
g2.drawImage(printCard,20,20,(int)(cardWidthInCm*dpCm),(int)(cardHeightInCm*dpCm),null);
saveImageForDebug(printCard,"printCard");
return PAGE_EXISTS;
} else {
return NO_SUCH_PAGE;
}
}
}
}
解决方案
推荐阅读
- javascript - 我可以用一个 onPress 提交多个 tcomb 表单吗?
- html - 元素从页面顶部流出
- python - 如果我在一个线程中收听 websocket 并在另一个线程中运行函数,则可能会丢失消息
- sql - 每天选择第一个条目
- java - 对 AlertDialog 的高度行为感到困惑
- scala - 获取总和并选择最大键值对而不在 spark scala 中并行化
- swift - 通用协议上的 Swift 4 扩展函数,它以通用函数作为参数
- html - 有没有办法用css3和html5的新实现隐藏div的一部分?
- javascript - 在foreach循环中使用索引作为javascript中的变量
- r - 使用数据框名称作为模型表中的列