gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:00:03 35 4
gpt4 key购买 nike

我想用 Java 制作图像并在 300dpi 标签打印机上将其打印在尺寸为 150 x 100 毫米的标签上。我如何制作图像,以便在位置 (10,10)(以毫米为单位)处准确打印一条线(或任何类型的元素),并且该线在位置 (10,50) 处结束?

换句话说:我的挑战不是如何制作一条线(我使用的是 Graphics2D、bufferedImage),而是如何能够准确地说出这条线在标签上的位置(以毫米为单位)。

有什么想法吗?

最佳答案

Java 的打印 API 基本上假设一切都以 72 dpi 完成。这意味着您可以将其用作转换为不同测量值的基础...

这仅意味着您需要并开始衡量值(value)和目标...

// 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;

/**
* Converts the given pixels to cm's based on the supplied DPI
* @param pixels
* @param dpi
* @return
*/
public static double pixelsToCms(double pixels, double dpi) {
return inchesToCms(pixels / dpi);
}

/**
* Converts the given cm's to pixels based on the supplied DPI
* @param cms
* @param dpi
* @return
*/
public static double cmsToPixel(double cms, double dpi) {
return cmToInches(cms) * dpi;
}

/**
* Converts the given cm's to inches
* @param cms
* @return
*/
public static double cmToInches(double cms) {
return cms * CM_PER_INCH;
}

/**
* Converts the given inches to cm's
* @param inch
* @return
*/
public static double inchesToCms(double inch) {
return inch * INCH_PER_CM;
}

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

double point = cmsToPixel(1, 72);

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

一些例子...

更新测试结果

所以我做了一些测试(要遵循的代码)...

首先,我设置了一个 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.03 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;

/**
* Converts the given pixels to cm's based on the supplied DPI
*
* @param pixels
* @param dpi
* @return
*/
public static double pixelsToCms(double pixels, double dpi) {
return inchesToCms(pixels / dpi);
}

/**
* Converts the given cm's to pixels based on the supplied DPI
*
* @param cms
* @param dpi
* @return
*/
public static double cmsToPixel(double cms, double dpi) {
return cmToInches(cms) * dpi;
}

/**
* Converts the given cm's to inches
*
* @param cms
* @return
*/
public static double cmToInches(double cms) {
return cms * CM_PER_INCH;
}

/**
* Converts the given inches to cm's
*
* @param inch
* @return
*/
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;
}

}
}

关于java - 如何在 java 中设计要在 300 dpi 打印机上打印的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18975595/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com