简体   繁体   English

Java - 无法打印图像(到纸张/打印机)

[英]Java - Can't print image (to paper/printer)

I am trying to print an Image using the following code, but the document simply stays in the print job queue, and refuses to print. 我正在尝试使用以下代码打印图像,但文档只是保留在打印作业队列中,并拒绝打印。 In the (windows) print job queue I get: 在(Windows)打印作业队列中,我得到:

DocumentName: Printing an image Status: [Nothing] Pages : 1, Size: 2.1Mb. DocumentName:打印图像状态: [Nothing] 页数 :1, 大小: 2.1Mb。

This does not happen with other applications using the same printer (word, etc). 使用相同打印机(word等)的其他应用程序不会发生这种情况。

Could anyone kindly show me where is my mistake? 有谁可以告诉我我的错误在哪里? Thanks. 谢谢。

public static void main(String[] args) {
    //new Painter();

    MediaTracker tracker = new MediaTracker(new JPanel());

    try {
        Image img = ImageIO.read(new File(
            "C:\\Users\\David\\Desktop\\print.jpg"));
        tracker.addImage(img, 1);
        tracker.waitForAll();
        print(img);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

private static void print(final Image img) {
    PrinterJob printjob = PrinterJob.getPrinterJob();
    printjob.setJobName("Print");

    ImgPrinter printable = new ImgPrinter(img);

    try {
        System.out.println("Printing.");
        printable.printPage();
    } catch (PrinterException ex) {
        System.out.println("NO PAGE FOUND." + ex);
    }
}

private static class ImgPrinter implements Printable {

    Image img;

    public ImgPrinter(Image img) {
        this.img = img;
    }

    public int print(Graphics pg, PageFormat pf, int pageNum) {
        if (pageNum != 0) {
            return Printable.NO_SUCH_PAGE;
        }

        //BufferedImage bufferedImage = new BufferedImage(img.getWidth(null),
        //img.getHeight(null), BufferedImage.TYPE_INT_RGB);
        //bufferedImage.getGraphics().drawImage(img, 0, 0, null);

        Graphics2D g2 = (Graphics2D) pg;
        g2.translate(pf.getImageableX(), pf.getImageableY());
        g2.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);
        return Printable.PAGE_EXISTS;
    }

    public void printPage() throws PrinterException {
        PrinterJob job = PrinterJob.getPrinterJob();
        boolean ok = job.printDialog();
        if (ok) {
            job.setJobName("TEST JOB");
            job.setPrintable(this);
            job.print();
        }
    }
}

Screenshot of problem: 问题截图:

This happens with both hardware and software printers(XPS Writer, CutePDF, Canon printer). 硬件和软件打印机(XPS Writer,CutePDF,Canon打印机)都会发生这种情况。 The hardware shows "preparing.." on it's screen forever, and I think it wasted all it's ink, I'm not sure. 硬件永远在它的屏幕上显示“准备......”,我认为它浪费了所有的墨水,我不确定。 If so, this code has been expensive to test.... 如果是这样,这段代码测试成本很高....

None of these printers give an issue when printing from a word document or otherwise. 从word文档或其他方式打印时,这些打印机都不会出现问题。

Edit: Can somebody suggest a software printer he or she has been successful with? 编辑:有人可以建议他或她成功使用的软件打印机吗?

Click here for the Image I am trying to print . 单击此处查看我要打印的图像

Click here to see the print queue . 单击此处查看打印队列

I just ran a quick test, and it works fine for me. 我刚刚进行了快速测试,它对我来说很好。 I was able to print a png image. 我能够打印一个png图像。

Chances are there is something wrong with your printer. 您的打印机可能有问题。

Did you try printing a Word Document using the Word's print option. 您是否尝试使用Word的打印选项打印Word文档?

Are there multiple printers assigned to your machine? 是否有多台打印机分配给您的机器? You could try restarting your printer? 您可以尝试重新启动打印机吗? Restart you machine? 重启你的机器?

You could implement a print dialog box to open up. 您可以实现打印对话框以打开。 That way you can select the printer. 这样你就可以选择打印机了。 See this link here. 在此处查看此链接。 The code shows how to open up the print dialog in swing. 该代码显示了如何打开打印对话框。

 public void printPage() throws PrinterException
        {
            PrinterJob job = PrinterJob.getPrinterJob();
            boolean ok = job.printDialog();
            if (ok) {
                job.setJobName("TEST JOB");
                job.setPrintable(this);
                job.print();
            }
        }

This way you can make sure a printer has been selected correctly. 这样您就可以确保正确选择了打印机。

The other thing you can use to make sure the image does not get distorted Instead of this line 您可以使用另一件事来确保图像不会变形而不是这一行

g2.drawImage(bufferedImage, 0, 0, (int) pf.getWidth(), (int) pf.getHeight(), null);

use the following line in the inner class 在内部类中使用以下行

g2.drawImage(img, 0, 0, img.getWidth(null), img.getHeight(null), null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM