调整热敏打印机上要打印的图像的大小

rkue9o1l  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(1263)

我想在热敏打印机上打印一个图像,但是当我使用这个代码时,图像会变小。如何更改要打印的图像的大小?

  1. public void printPhoto() throws Exception {
  2. PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
  3. pras.add(new Copies(1));
  4. pras.size();
  5. PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.PNG, pras);
  6. if (pss.length == 0)
  7. throw new RuntimeException("No printer services available.");
  8. PrintService ps = findPrintService("POS-58", pss);
  9. System.out.println("Printing to " + ps);
  10. DocPrintJob job = ps.createPrintJob();
  11. FileInputStream fin = new FileInputStream("D:\\QRCODE\\test.png");
  12. Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.PNG, null);
  13. job.print(doc, pras);
  14. fin.close();
  15. }

有人能帮我吗?这是我们学校的项目。谢谢。

f8rj6qna

f8rj6qna1#

我把代码改成这个

  1. private static BufferedImage image;
  2. public void PrintLogo(){
  3. try {
  4. image = ImageIO.read(new File("C:\\Users\\ADMIN\\Desktop\\logothermal.png"));
  5. System.out.println(image.getWidth() + "x" + image.getHeight());
  6. PrinterJob pj = PrinterJob.getPrinterJob();
  7. PageFormat pf = pj.defaultPage();
  8. Paper paper = pf.getPaper();
  9. // 86X54mm
  10. double width = fromCMToPPI(7.6);
  11. double height = fromCMToPPI(3.4);
  12. paper.setSize(width, height);
  13. paper.setImageableArea(
  14. fromCMToPPI(0.1),
  15. fromCMToPPI(0.1),
  16. width - fromCMToPPI(0.1),
  17. height - fromCMToPPI(0.1));
  18. pf.setOrientation(PageFormat.PORTRAIT);
  19. pf.setPaper(paper);
  20. PageFormat validatePage = pj.validatePage(pf);
  21. System.out.println("Valid- " + dump(validatePage));
  22. pj.setPrintable(new MyPrintable(), validatePage);
  23. try {
  24. pj.print();
  25. } catch (PrinterException ex) {
  26. ex.printStackTrace();
  27. }
  28. } catch (IOException exp) {
  29. exp.printStackTrace();
  30. }
  31. }
  32. public static double fromPPItoCM(double dpi) {
  33. return dpi / 72 / 0.393700787;
  34. }
  35. public static double fromCMToPPI(double cm) {
  36. return toPPI(cm * 0.393700787);
  37. }
  38. public static double toPPI(double inch) {
  39. return inch * 72d;
  40. }
  41. public static String dump(Paper paper) {
  42. StringBuilder sb = new StringBuilder(64);
  43. sb.append(paper.getWidth()).append("x").append(paper.getHeight())
  44. .append("/").append(paper.getImageableX()).append("x").
  45. append(paper.getImageableY()).append(" - ").append(paper
  46. .getImageableWidth()).append("x").append(paper.getImageableHeight());
  47. return sb.toString();
  48. }
  49. public static String dump(PageFormat pf) {
  50. Paper paper = pf.getPaper();
  51. return dump(paper);
  52. }
  53. public static class MyPrintable implements Printable {
  54. @Override
  55. public int print(Graphics graphics, PageFormat pageFormat,
  56. int pageIndex) throws PrinterException {
  57. System.out.println(pageIndex);
  58. int result = NO_SUCH_PAGE;
  59. if (pageIndex < 1) {
  60. Graphics2D g2d = (Graphics2D) graphics;
  61. System.out.println("[Print] " + dump(pageFormat));
  62. double width = pageFormat.getImageableWidth();
  63. double height = pageFormat.getImageableHeight()/2;
  64. System.out.println("Print Size = " + fromPPItoCM(width) + "x" +
  65. fromPPItoCM(height));
  66. g2d.translate((int) pageFormat.getImageableX(),
  67. (int) pageFormat.getImageableY());
  68. Image scaled = null;
  69. if (width > height) {
  70. scaled = image.getScaledInstance((int)Math.round(width), -1,
  71. Image.SCALE_SMOOTH);
  72. } else {
  73. scaled = image.getScaledInstance(-1, (int)Math.round(height),
  74. Image.SCALE_SMOOTH);
  75. }
  76. g2d.drawImage(scaled, 0, 0, null);
  77. result = PAGE_EXISTS;
  78. }
  79. return result;
  80. }
  81. }
展开查看全部

相关问题