使图像适合打印区域 [英] Fit image into the printing area

查看:132
本文介绍了使图像适合打印区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用Java打印图像。所以我实现了 print() Printable 接口的方法。但是打印机总是只打印一些原始图像。如何使图像适合打印区域(A4),这样打印机能够打印整个图像,而不是它们的一部分?现在我的代码如下所示:

I need to print images in Java. So I implemented print() method of Printable interface. But printer always prints only some piece of original image. How I can cause image to fit printing area (A4), so printer be able to print whole image, not piece of them? Now my code looks like this:

public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {


    if (pageIndex >= images.size()) {
        return Printable.NO_SUCH_PAGE;
    }
    RenderedImage image;


    Graphics2D graphics2D = (Graphics2D) graphics;

    image = new NullOpImage((RenderedImage) images.get(pageIndex), null, null, OpImage.OP_IO_BOUND);


    double x = (pageFormat.getImageableWidth() - image.getWidth())/2 + pageFormat.getImageableX();
    double y = (pageFormat.getImageableHeight() - image.getHeight())/2 + pageFormat.getImageableY();


    graphics2D.drawRenderedImage(image, AffineTransform.getTranslateInstance(x, y));

    return PAGE_EXISTS;
}


推荐答案

您需要缩放图像向下以适应可用区域。

You need to scale the image down to fit the available area.

private int currentPage = -1;
private Image cachedScaledImage = null;

public double getScaleFactor(int iMasterSize, int iTargetSize) {
    double dScale = 1;
    if (iMasterSize > iTargetSize) {
        dScale = (double) iTargetSize / (double) iMasterSize;
    } else {
        dScale = (double) iTargetSize / (double) iMasterSize;
    }
    return dScale;
}

public double getScaleFactorToFit(BufferedImage img, Dimension size) {
    double dScale = 1;
    if (img != null) {
        int imageWidth = img.getWidth();
        int imageHeight = img.getHeight();
        dScale = getScaleFactorToFit(new Dimension(imageWidth, imageHeight), size);
    }
    return dScale;
}

public double getScaleFactorToFit(Dimension original, Dimension toFit) {
    double dScale = 1d;
    if (original != null && toFit != null) {
        double dScaleWidth = getScaleFactor(original.width, toFit.width);
        double dScaleHeight = getScaleFactor(original.height, toFit.height);

        dScale = Math.min(dScaleHeight, dScaleWidth);
    }
    return dScale;
}

public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {

    if (pageIndex >= images.size()) {
        return Printable.NO_SUCH_PAGE;
    }
    RenderedImage image;


    Graphics2D graphics2D = (Graphics2D) graphics;

    int width = (int)Math.round(pageFormat.getImageableWidth());
    int height = (int)Math.round(pageFormat.getImageableHeight());

    if (currentPage != pageIndex || cachedScaledImage == null) {
        currentPage = pageIndx;    

        image = new NullOpImage((RenderedImage) images.get(pageIndex), null, null, OpImage.OP_IO_BOUND);

        BufferedImage imageCopy = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = imageCopy.createGraphics();
        g2d.drawImage(imageCopy, 0, 0, null);
        g2d.dispose();

        double scaleFactor = getScaleFactorToFit(new Dimension(image.getWidth(), image.getHeight()), new Dimension(width, height));

        int imageWidth = (int)Math.round(image.getWidth() * scaleFactor);
        int imageHeight = (int)Math.round(image.getHeight() * scaleFactor);

        double x = ((pageFormat.getImageableWidth() - imageWidth) / 2) + pageFormat.getImageableX();
        double y = ((pageFormat.getImageableHeight() - imageHeight) / 2) + pageFormat.getImageableY();

        cachedScaledImage = imageCopy.getScaledInstance(imageWidth, imageHeight, Image.SCALE_SMOOTH);

    }

    graphics2D.drawRenderedImage(cachedScaledImage, AffineTransform.getTranslateInstance(x, y));

    return PAGE_EXISTS;
}

这篇关于使图像适合打印区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆