【问题标题】:How can increase only image width in java如何在java中只增加图像宽度
【发布时间】:2018-12-02 12:59:04
【问题描述】:
    try
    {
        BufferedImage original = ImageIO.read(OriginalImage);
        BufferedImage resized = new BufferedImage(width, null, original.getType());
        Graphics2D g2 = resized.createGraphics();
        g2.drawImage(original, 0, 0, width, null, null);
        g2.dispose();
        ImageIO.write(resized, format, resizedImage);
    }

我想只增加图像宽度,高度自动按宽度像素调整。

【问题讨论】:

  • 是的..我想增加图像宽度和高度纵横比自动保持宽度。示例:我有图像 900X600 像素。我只是增加宽度 50px 像(950)高度会自动调整 631。
  • 1) 为了尽快获得更好的帮助,请发帖 minimal reproducible exampleShort, Self Contained, Correct Example。 2) 例如,获取图像的一种方法是热链接到在this Q&A 中看到的图像。 3) 上面的代码与 Swing 无关。不要添加不相关的标签! 4) 不要在 cmets 中添加额外的信息。取而代之的是edit question.5) “高度自动调节” 为什么?这是一个简单的计算。
  • 我通过手动计算纵横比找到解决方案。

标签: java image image-processing awt image-resizing


【解决方案1】:

使用方法Image.getScaledInstance,如下例所示

import java.awt.BorderLayout;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

public class ImageTest {

    private static final int WIDTH_INCREMENT = 100;
    public static void main(String[] args) throws Exception {
        BufferedImage image = ImageIO.read(new File("D:\\DDownloads\\Download.png"));
        int targetWidth = image.getWidth() + WIDTH_INCREMENT;
        // compute height using the aspect ratio
        int targetHeight = (int) ((double) targetWidth * image.getHeight()) / image.getWidth();
        Image target = image.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH);
        // also you can use -1 as new height
        // image.getScaledInstance(targetWidth, -1, Image.SCALE_SMOOTH);
        // in this case the method computes the correct width by himself
        BufferedImage toWrite = new BufferedImage(
            targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = toWrite.createGraphics();
        g2.drawImage(target, 0, 0, targetWidth, targetHeight, null);
        g2.dispose();
        ImageIO.write(toWrite, "png", new File("D:\\\\DDownloads\\\\converted.png"));
        // next code is to show the both original and converted image on the screen

        // Use invokeLater ofr correct initialization of Swing components
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                JFrame frm = new JFrame("Images");
                frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frm.add(new JLabel(new ImageIcon(image)), BorderLayout.NORTH);
                frm.add(new JLabel(new ImageIcon(target)), BorderLayout.SOUTH);
                frm.pack();
                frm.setLocationRelativeTo(null); // center the window
                frm.setVisible(true);
            }
        });
    }
}

【讨论】:

  • 注意:来自Image.getScaledInstance(w,h,hints)".. 如果宽度或高度为负数,则替换一个值以保持原始图像的纵横比尺寸。” ;)
  • @AndrewThompson 非常感谢。但在这种情况下,我认为最好向作者展示如何使用纵横比计算目标高度。
猜你喜欢
  • 2015-01-17
  • 2019-12-06
  • 2023-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-04
  • 1970-01-01
相关资源
最近更新 更多