重新缩放图像以进行打印

Rescale an image for printing

我需要重新缩放图像以进行打印,但我只有两个不同的结果:

我放两张图看得更清楚。第一个是我现在的结果:

第二个是我必须在页面上看到的图像,我必须重新缩放,因为它比页面大:

可能是打印问题或方法"rescale"不起作用?我把我的代码放在下面:

public int print(Graphics g, PageFormat pf, int pageIndex) 
          {
            if (pageIndex != 0)
              return NO_SUCH_PAGE;
            Graphics2D g2 = (Graphics2D) g;
            g2.setFont(new Font("Serif", Font.PLAIN, 36));
            g2.setPaint(Color.black);
            g2.scale(200, 200);                 
            g2.drawImage(createImage(temperaturePlot), null, 20, 20);
            return PAGE_EXISTS;
          }

    public BufferedImage createImage(JPanel panel) 
   {
   int w = panel.getWidth();
   int h = panel.getHeight();
   BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
   Graphics2D g = bi.createGraphics();
   panel.paint(g);

   return bi;       
   }

我只想缩放图像并查看页面上的所有值。

有什么帮助吗?谢谢!

我用新的commens改变了方法,但是没有用:

class MyPrintableGraphic implements Printable 
        {
          public int print(Graphics g, PageFormat pf, int pageIndex) 
          {
            if (pageIndex != 0)
              return NO_SUCH_PAGE;

            BufferedImage original = createImage(temperaturePlot);

            original.getScaledInstance(200, 200,BufferedImage.SCALE_DEFAULT);

            Graphics2D g2 = (Graphics2D) g;
            g2.setFont(new Font("Serif", Font.PLAIN, 36));
            g2.setPaint(Color.black);
            //BufferedImage image = createImage(temperaturePlot); 
            //Image scaled = image.getScaledInstance(200,200,Image.SCALE_SMOOTH); 
            g2.drawImage(original, 0, 0, null);
            /*BufferedImage mbi = resizeImage(createImage(temperaturePlot), 200, 200);
            g2.drawImage(mbi, null, 20, 20);*/
            return PAGE_EXISTS;
          }
        }

这是新的失败结果:

我用 getScaledInstance 缩放了我的图像

BufferedImage.getScaledInstance(width,height,BufferedImage.SCALE_TYPE) 

这应该可以完成工作。它 returns 你是一个缓冲图像,打印时具有你不想要的宽度和高度

这是一个关于如何调整图像大小并在 JFrame 中显示它的示例:

public static void main(String[] args) throws IOException {
    JFrame frame = new JFrame("Test");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setBounds(100, 100, 400, 400);

    BufferedImage original = ImageIO.read(new File("img.jpg"));
    BufferedImage resized = resizeImage(original, frame.getWidth(), frame.getHeight());

    JPanel panel = new JPanel() {
        @Override protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(resized, 0, 0, null);
        }
    };

    frame.add(panel);
    frame.setVisible(true);
}

private static BufferedImage resizeImage(BufferedImage image, int width, int height) {
    // calculate the scale factor
    double xScale = width / (double) image.getWidth();
    double yScale = height / (double) image.getHeight();
    // create the object that will contain the resized image
    BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    // resize the image
    Graphics2D g = (Graphics2D) resizedImage.getGraphics();
    g.scale(xScale, yScale);
    g.drawImage(image, 0, 0, null);
    g.dispose();
    // return the resized image
    return resizedImage;
}

这是我将帧大小设置为 400x400 的结果:

这是 600x400 帧:

如您所见,图像根据框架的大小进行调整。

尝试一下:

g2.drawImage(createImage(temperaturePlot).getScaledInstance(500, 300,BufferedImage.SCALE_SMOOTH), 85, 65, null);

我认为这可以解决您的问题。

您可以使用以下代码进行缩放

    BufferedImage imgSource = ImageIO.read(new File("images//Image3.jpg"));
    BufferedImage imgDestination = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = imgDestination.createGraphics();
    AffineTransform affinetransformation = AffineTransform.getScaleInstance(2, 2);
    g.drawRenderedImage(imgSource, affinetransformation);
    ImageIO.write(imgDestination, "JPG", new File("outImage.jpg"));