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

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

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

public  void printPhoto() throws Exception  {

PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
pras.size();
PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.PNG, pras);

if (pss.length == 0)
  throw new RuntimeException("No printer services available.");
PrintService ps = findPrintService("POS-58", pss);
System.out.println("Printing to " + ps);
DocPrintJob job = ps.createPrintJob();

FileInputStream fin = new FileInputStream("D:\\QRCODE\\test.png");
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.PNG, null);

job.print(doc, pras);
fin.close();
}

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

f8rj6qna

f8rj6qna1#

我把代码改成这个

private static BufferedImage image;

public void  PrintLogo(){
    try {
        image = ImageIO.read(new File("C:\\Users\\ADMIN\\Desktop\\logothermal.png"));

        System.out.println(image.getWidth() + "x" + image.getHeight());

        PrinterJob pj = PrinterJob.getPrinterJob();
            PageFormat pf = pj.defaultPage();
            Paper paper = pf.getPaper();
 //                        86X54mm
            double width = fromCMToPPI(7.6);
            double height = fromCMToPPI(3.4);
            paper.setSize(width, height);
            paper.setImageableArea(
                            fromCMToPPI(0.1),
                            fromCMToPPI(0.1),
                            width - fromCMToPPI(0.1),
                            height - fromCMToPPI(0.1));
            pf.setOrientation(PageFormat.PORTRAIT);
            pf.setPaper(paper);
            PageFormat validatePage = pj.validatePage(pf);
            System.out.println("Valid- " + dump(validatePage));
            pj.setPrintable(new MyPrintable(), validatePage);
            try {
                pj.print();
            } catch (PrinterException ex) {
                ex.printStackTrace();
            }

    } catch (IOException exp) {
        exp.printStackTrace();
    }
}

public static double fromPPItoCM(double dpi) {
    return dpi / 72 / 0.393700787;
}

public static double fromCMToPPI(double cm) {
    return toPPI(cm * 0.393700787);
}

public static double toPPI(double inch) {
    return inch * 72d;
}

public static String dump(Paper paper) {
    StringBuilder sb = new StringBuilder(64);
    sb.append(paper.getWidth()).append("x").append(paper.getHeight())
                    .append("/").append(paper.getImageableX()).append("x").
                    append(paper.getImageableY()).append(" - ").append(paper

.getImageableWidth()).append("x").append(paper.getImageableHeight());
    return sb.toString();
}

public static String dump(PageFormat pf) {
    Paper paper = pf.getPaper();
    return dump(paper);
}

public static class MyPrintable implements Printable {

    @Override
    public int print(Graphics graphics, PageFormat pageFormat,
                                     int pageIndex) throws PrinterException {
        System.out.println(pageIndex);
        int result = NO_SUCH_PAGE;
        if (pageIndex < 1) {
            Graphics2D g2d = (Graphics2D) graphics;
            System.out.println("[Print] " + dump(pageFormat));
            double width = pageFormat.getImageableWidth();
            double height = pageFormat.getImageableHeight()/2;

            System.out.println("Print Size = " + fromPPItoCM(width) + "x" + 
        fromPPItoCM(height));
            g2d.translate((int) pageFormat.getImageableX(),
                            (int) pageFormat.getImageableY());
            Image scaled = null;
            if (width > height) {
                scaled = image.getScaledInstance((int)Math.round(width), -1, 
Image.SCALE_SMOOTH);
            } else {
                scaled = image.getScaledInstance(-1, (int)Math.round(height), 
Image.SCALE_SMOOTH);
            }
            g2d.drawImage(scaled, 0, 0, null);
            result = PAGE_EXISTS;
        }
        return result;
    }

}

相关问题