栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Java中设计要在300 dpi打印机上打印的图像

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Java中设计要在300 dpi打印机上打印的图像

Java的打印API基本上是在假设所有操作均以72 dpi的速度进行工作的。这意味着您可以以此为基础来进行不同测量之间的转换…

这只是意味着您需要开始值和目标测量…

// The number of CMs per Inchpublic static final double CM_PER_INCH = 0.393700787d;// The number of Inches per CMspublic static final double INCH_PER_CM = 2.545d;// The number of Inches per mm'spublic static final double INCH_PER_MM = 25.45d;public static double pixelsToCms(double pixels, double dpi) {    return inchesToCms(pixels / dpi);}public static double cmsToPixel(double cms, double dpi) {    return cmToInches(cms) * dpi;}public static double cmToInches(double cms) {    return cms * CM_PER_INCH;}public static double inchesToCms(double inch) {    return inch * INCH_PER_CM;}

因此,为了以10mmx10mm的分辨率打印图像,您需要将其转换为每英寸的像素

double point = cmsToPixel(1, 72);

您可能还需要考虑缩小图像尺寸以适合可打印区域。

举个例子…

  • 使图像适合打印区域
  • 使PrinterJob对象适合BufferedImage的特定打印格式

更新测试结果

所以我做了一些测试(代码如下)…

首先,我设置一个

PrintRequestAttributeSet
仅请求能够支持300x300 dpi的打印服务…

PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();aset.add(new PrinterResolution(300, 300, PrinterResolution.DPI));aset.add(new MediaPrintableArea(0, 0, 150, 100, MediaPrintableArea.MM));

打印时,我

Printable
通过了425.20 x 283.46像素的可成像区域,相当于15.dpi x 10.02厘米@
72dpi(大约)。这就是Java的工作方式,它的基本打印API始终在72dpi的假设下工作。

所以。如果准备10 x 50毫米@ 72 DPI的图像,则图像尺寸为28.346 x 141.732像素,可以轻松放入可成像区域(425.20 x
283.46)。

但是,如果使用300 dpi,则会得到118.11 x 590.551像素的图像,这将使我们陷入麻烦,需要缩小图像的尺寸…

实际上,这可能是理想的,您必须执行一些测试才能找出答案。

import java.awt.Color;import java.awt.EventQueue;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.geom.Line2D;import java.awt.geom.Rectangle2D;import java.awt.image.BufferedImage;import java.awt.print.PageFormat;import java.awt.print.Printable;import java.awt.print.PrinterException;import java.awt.print.PrinterJob;import javax.print.attribute.HashPrintRequestAttributeSet;import javax.print.attribute.PrintRequestAttributeSet;import javax.print.attribute.standard.MediaPrintableArea;import javax.print.attribute.standard.PrinterResolution;public class TestHiResPrinting {    public static void main(String[] args) {        EventQueue.invokeLater(new Runnable() { @Override public void run() {     PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();     aset.add(new PrinterResolution(300, 300, PrinterResolution.DPI));     aset.add(new MediaPrintableArea(0, 0, 150, 100, MediaPrintableArea.MM));     PrinterJob pj = PrinterJob.getPrinterJob();     pj.setPrintable(new PrintTask());     if (pj.printDialog(aset)) {         try {  pj.print(aset);         } catch (PrinterException ex) {  ex.printStackTrace();         }     } }        });    }    // The number of CMs per Inch    public static final double CM_PER_INCH = 0.393700787d;    // The number of Inches per CMs    public static final double INCH_PER_CM = 2.545d;    // The number of Inches per mm's    public static final double INCH_PER_MM = 25.45d;        public static double pixelsToCms(double pixels, double dpi) {        return inchesToCms(pixels / dpi);    }        public static double cmsToPixel(double cms, double dpi) {        return cmToInches(cms) * dpi;    }        public static double cmToInches(double cms) {        return cms * CM_PER_INCH;    }        public static double inchesToCms(double inch) {        return inch * INCH_PER_CM;    }    public static class PrintTask implements Printable {        private BufferedImage img;        public PrintTask() { double width = cmsToPixel(1, 72); double height = cmsToPixel(5, 72); img = new BufferedImage((int) Math.round(width), (int) Math.round(height), BufferedImage.TYPE_INT_ARGB); Graphics2D g2d = img.createGraphics(); g2d.setColor(Color.RED); g2d.draw(new Rectangle2D.Double(0, 0, width - 1, height - 1)); g2d.draw(new Line2D.Double(0, 0, width, height)); g2d.draw(new Line2D.Double(0, height, width, 0)); g2d.dispose();        }        @Override        public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { int result = NO_SUCH_PAGE; if (pageIndex < 2) {     Graphics2D g2d = (Graphics2D) graphics;     double width = pageFormat.getImageableWidth();     double height = pageFormat.getImageableHeight();     System.out.println("Page width = " + width + " = " + pixelsToCms(width, 72));     System.out.println("Page height = " + height + " = " + pixelsToCms(height, 72));     g2d.translate((int) pageFormat.getImageableX(),          (int) pageFormat.getImageableY());     double x = cmsToPixel(1, 72);     double y = cmsToPixel(1, 72);     System.out.println("Draw At " + x + "x" + y);     g2d.drawRect(0, 0, (int)width - 1, (int)height - 1);     g2d.drawImage(img, (int)x, (int)y, null);     result = PAGE_EXISTS; } return result;        }    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/496777.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号