- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我想用 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/
我有一个代理类,它接收请求并将请求发送到另一台服务器,获取响应并将其定向回原始请求者。我使用套接字连接到服务器并使用 Printwriter 对其进行写入。像这样的事情 private Pri
我想使用 Java Swing 为医疗商店开发一个独立的应用程序。强制要求在没有打印对话框的情况下单击一下即可打印小尺寸(219 毫米至 140 毫米)的纸张。他们需要间歇性地将报告从不同的打印机打印
我是一名学生,需要创建一个 silten 打印功能,希望能够打印 PDF。这个需要基于Java。 我在 Google 上搜索并找到了一个无需对话框即可打印的代码。但如果源是 .txt 文件,它就会正确
我正在寻找 POS 打印的解决方案。 场景是: 一家餐厅目前有一个 POS 系统启动并运行,他们从店内的 iPad 上接受订单,并有一个网络设置来处理订单并在厨房的热敏打印机上自动打印出来(很酷,对吧
尝试使用以下代码示例,它在 WinForm 应用程序中运行良好,但在 VSTO 中运行不佳。是否有某种允许访问的权限? 可以设置默认打印机,但不能获取或设置打印机设置。 从插件中获取以下异常: Sys
我必须通过蓝牙将字体文件发送到我的打印机 Zebra RW420。我正在使用 Zebra Windows Mobile SDK,但无法找到任何方式将其发送和存储在打印机上。我可以通过 Label Vi
我需要创建一个“粉碎”的虚拟打印机 基本上这是我的问题。我有一个软件程序需要在保存文件之前“打印”文件。我希望能够打印到我的碎纸机,以便它保存文档,但实际上我不想打印文档。所以我需要打印到一个程序,该
我在吃 Argox 标签打印机时遇到了麻烦,只是仍然无法向她发送任何内容。型号为 Argox OS214 tt,ANPP,接受 PPLB。 使用通用类连接串行设备,我将她用于多个财务打印机和秤,附后。
我正在编写一些在 org.eclipse.swt.printing.Printer 上打印的代码。所以第一步是看看我如何测试它,但似乎架构不允许我定义自己的打印机,因为 PrinterData 和 P
我有一个标签列表,数据如下。 ['id', 'Version', 'chip_name', 'xversion', 'device', 'opt_param', 'place_effort'][1,
我正在开发一个应用程序,在该应用程序中,我通过 IP 地址和端口号从我的手机向 WiFi 打印机发送文件,这些文件是 .txt、.png、.jpg、.doc。它们应该从打印机打印出来。我尝试了以下代码
我正在尝试使用以下代码打印图像,但文档只是停留在打印作业队列中,拒绝打印。在 (windows) 打印作业队列中,我得到: DocumentName: Printing an image Status
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Print preview ZPL II commands using .NET WinForm befor
我有什么 我目前正在编写一个程序,它接受一个指定的文件并对其执行一些操作。目前它打开它,和/或将它附加到电子邮件并将其邮寄到指定地址。 文件可以是以下格式:Excel、Excel Report、Wor
我将这台 Zebra ZM400 打印机连接到网络 (192.168.1.50)。我正在尝试直接从 PHP 将内容推送到这台打印机。 这就是我的想法,但我无法做到这一点。我尝试了 file_put_c
我想显示一个列表,其中包含设备可通过 AirPrint 访问的所有打印机。 我使用 UIPrinterPickerController 让它工作。 是否有以自定义方式显示此 PickerControl
我想将任何办公文件传输到 Wi-ifi 打印机。我完全不知道如何开始。 发现没有用于无线打印的公共(public) API。 谁能分享一些意见? 提前致谢! 最佳答案 您可以首先扫描 WiFi 设备并
有什么方法可以让我在蓝牙热敏打印机上打印收据,因为我真的很难在 flutter 上找到解决方案?任何事情都有帮助,我真的很感激这些答案 最佳答案 我试过 esc_pos_bluetooth 包,但它不
为了在我的 mac 上模拟 ZPL 打印机,我在互联网上搜索了几天。最后,我有一个解决方案可以在这里发布,这样其他用户可能会发现它有帮助。我想在这里发布我的解决方案 Emulate Zebra pri
是否有 ZPL 命令来简单地重启 Zebra 打印机?到目前为止,我只能找到 ~JR 命令,这对我来说看起来不像我正在寻找的东西。我只需要一种方法来重新启动打印机,而无需重置其任何配置。 最佳答案 以
我是一名优秀的程序员,十分优秀!