【问题标题】:Get Java Printer GraphicsDevice获取 Java 打印机 GraphicsDevice
【发布时间】:2012-02-27 22:07:50
【问题描述】:

JavaDocs 表明 GraphicsEnvironment 支持“屏幕和打印机设备”。

我可以看到如何获取有关屏幕设备的信息,但我似乎无法找到有关如何获取有关打印机设备的信息的信息。

基本上,在打印时,我希望能够使用设备 GraphicsConfiguration 创建兼容的缓冲图像。

想要这样做的主要原因是:

  1. 我们希望在页面的第一个请求时缓冲页面请求,并在页面的后续请求中简单地重新绘制缓冲区(因为页面包含许多图像和一些复杂的渲染 - 而不是浪费时间绘制每个页面请求,我们想使用缓冲区)
  2. 保持打印机的高分辨率输出。我们发现,如果我们直接在打印机图形上下文中绘制,我们会获得比尝试使用相同大小的缓冲图像时高得多的输出量。

我尝试搜索 JavaDocs 和 Google,但没有成功。

有什么建议吗??

干杯

根据其他想法更新

根据其他想法,建议尝试使用类似...

GraphicsConfiguration conf = ((Graphics2D) graphics).getDeviceConfiguration();
assert conf.getDevice().getType() == GraphicsDevice.TYPE_PRINTER;
System.out.println("Device: " + conf.getDevice().getIDstring());

final AffineTransform trans = conf.getDefaultTransform();
double dpi = trans.transform(new Point2D.Float(72, 0), null).getX();
System.out.println(dpi + " DPI");

Rectangle bounds = conf.getBounds();
System.out.println("page size: " + bounds.width + "x" + bounds.height);
// now you could do
buffer = conf.createCompatibleImage(bounds.width, bounds.height);
// verify values, you wouldn’t do this in production code:

这实际上会生成一个BufferedImage,它将被转换为打印机 DPI

为了让测试更容易,我写了一个简单的print方法...

public void print(Graphics2D g2d, double width, double height) {
    Font font = g2d.getFont();
    font = font.deriveFont(64f);
    g2d.setFont(font);
    FontMetrics fm = g2d.getFontMetrics();
    String text = "Hello World!";
    double x = (width - fm.stringWidth(text)) / 2;
    double y = (height - fm.getHeight()) / 2;
    g2d.drawString(text, (float) x, (float) y);
}

我可以使用打印机 Graphics 上下文或其他 Graphics 上下文。

现在很明显,当打印到 A4 页面 @ 72dpi 时,生成的图像尺寸为 595x841,在 600dpi(上面的示例中已报告),这导致图像为 4970x7029。好的,这很好,我只需要在绘制到目标打印机Graphics 上下文时缩小图像,使用类似...

g2d.drawImage(buffer, 0, 0, (int) pageFormat.getImageableWidth(), (int) pageFormat.getImageableWidth(), null);

(这是测试,所以不要在质量相关的问题上跳到我这里)...

(左边正常,右边BufferedImage)...好吧,那不行

然后我想,我可以将AffineTransform 比例应用于缓冲区Graphics 上下文,使用类似...

double scale = dpi / 72d;
AffineTransform scaleAT = AffineTransform.getScaleInstance(scale, scale);
g2d.setTransform(scaleAT);
print(g2d, pageFormat.getImageableWidth(), pageFormat.getImageableWidth());

这意味着我不需要在我们拥有的基础现有绘制例程中应用任何“打印模式”类型转换...

这导致...

但是等一下,这里出了什么问题?

所以我回去看看所有的测量结果......

GraphicsConfiguration conf = ((Graphics2D) graphics).getDeviceConfiguration();
//...
Rectangle bounds = conf.getBounds();
buffer = conf.createCompatibleImage(bounds.width, bounds.height);

报告的图像尺寸为 4960x7015,但它应该是 4970x7029...但是等等,可想象的区域呢...在 600dpi 时应该是 3768x5827...所以不能依赖。

即使对此进行了纠正,结果仍然没有使图像在位置和质量上都符合预期...

【问题讨论】:

  • 这个问题是关于大小来自 Java Graphics2D 或输出到硬件打印机???
  • @mKorbel 也不是。就像你可以创建一个兼容的图像来渲染到屏幕上一样,我想用打印机做同样的事情。 Asically,如果您创建一个缓冲图像并将其绘制到打印机的图形上下文中,它会以(我认为)72dpi 呈现。就好像您直接绘制图形一样,您获得了打印机的 dpi
  • 谢谢,aaaach 现在对我来说问题很清楚了 :-)
  • 认真的吗?为什么这个问题会引起反对票?有些人想扩展推理,以便我有机会纠正问题...?

标签: java graphics printing


【解决方案1】:

Graphics2D 提供了一种方法来获取链接到GraphicsDeviceGraphicsConfiguration。缺少的是信息,当您可以安全地假设 Graphics 在打印上下文中是 Graphics2D 时。

例如以下程序适用于我的设置:

import java.awt.*;
import java.awt.geom.*;
import java.awt.image.BufferedImage;
import java.awt.print.*;
import javax.print.*;

public class PrintDev implements Printable {
  public static void main(String[] args) throws PrintException {
    final DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
    PrintService ps=PrintServiceLookup.lookupDefaultPrintService();
    System.out.println(ps.getName());
    ps.createPrintJob().print(new SimpleDoc(new PrintDev(), flavor, null), null);
  }
  @Override
  public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
      throws PrinterException {
    GraphicsConfiguration conf = ((Graphics2D)graphics).getDeviceConfiguration();
    assert conf.getDevice().getType()==GraphicsDevice.TYPE_PRINTER;
    System.out.println("Device: "+conf.getDevice().getIDstring());
    final AffineTransform trans = conf.getDefaultTransform();
    System.out.println(trans.transform(new Point2D.Float(72,0),null).getX()+" DPI");
    Rectangle bounds = conf.getBounds();
    System.out.println("page size: "+bounds.width+"x"+bounds.height);
    // now you could do
    BufferedImage bi=conf.createCompatibleImage(bounds.width, bounds.height);
    // verify values, you wouldn’t do this in production code:
    try { trans.invert(); }
    catch(NoninvertibleTransformException ex){ return NO_SUCH_PAGE; }
    Point2D p=trans.transform(new Point2D.Float(bounds.width, bounds.height),null);
    System.out.printf("page in inches: %.2fx%.2f%n", p.getX()/72, p.getY()/72);
    return NO_SUCH_PAGE;
  }
}

【讨论】:

  • 我按喇叭,我试过了,但没有用。我会重新测试以确保
  • 实际上,几个月前我尝试过类似的事情,但没有成功。仍在测试中
  • 为了完整起见,它适用于Windows 7 32Bitjdk 1.7.0_40...
  • Windows 7 x64、Java 6、7 和 8 - 仍在测试中;)
  • 这是否意味着它可以在这些系统上运行?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-24
  • 1970-01-01
  • 2013-05-01
  • 2014-12-23
相关资源
最近更新 更多