将图片缩放到适合窗口大小_Java


使用Map传出两个最大值和最小值

参数中 int conWidth,int conHeight 分别是存放图片窗口的长和宽
源码:
    /**
     * scaleImg(); 图像缩放方法,将图片缩放到适合控件的大小
     * ImageIcon image为显示的图片
     * int conWidth,int conHeight 分别是存放图片窗口的的长和宽
     */
    public static Map<String, Integer> scaleImage(ImageIcon image, double conWidth, double conHeight) {

        Map<String, Integer> imgWidthAndHeight = new HashMap<String, Integer>();

        //默认的边框间距
        final double SMALL_SCALE = 0.95;

        double imgWidth = image.getIconWidth();
        double imgHeight = image.getIconHeight();
        //原图的宽长比
        double imgRatio = imgWidth/imgHeight;
        //最终输出宽和长
        double reImgWidth = 0;
        double reImgHeight = 0;


        //若原图的宽小于控件宽
        if(imgWidth < conWidth){
            if(imgHeight < conHeight){
                reImgWidth = conWidth*SMALL_SCALE;
                reImgHeight = reImgWidth/imgRatio;
            }
            else {
                reImgHeight = conHeight*SMALL_SCALE;
                reImgWidth = reImgHeight*imgRatio;
            }
        }
        //若原图的宽大于控件宽
        else {
            if(imgHeight < conHeight){
                reImgWidth = conWidth*SMALL_SCALE;
                reImgHeight = reImgWidth/imgRatio;
            }
            //若原图的长宽同时大于控件的长宽,最复杂的情况
            else {
                //控件的长比宽大
                double conRatio = conWidth/conHeight;

                if (imgRatio < conRatio){
                    reImgHeight = conHeight*SMALL_SCALE;
                    reImgWidth = reImgHeight*imgRatio;
                }
                else {
                    reImgWidth = conWidth*SMALL_SCALE;
                    reImgHeight = reImgWidth/imgRatio;
                }
            }
        }




        imgWidthAndHeight.put("width", (int) reImgWidth);
        imgWidthAndHeight.put("height", (int) reImgHeight);

        return imgWidthAndHeight;
    }

使用方法:

//图片等比缩放函数
            Map<String, Integer> imgWidthAndHeight = ImageProcess.scaleImage(image,IMG_WIDTH,IMG_HEIGHT);
            scaleWidth = imgWidthAndHeight.get("width");
            scaleHeight= imgWidthAndHeight.get("height");


可以使用Java Swing中的JLabel和ImageIcon来显示图片,并将其添加到JFrame或JPanel中。为了使图片随着窗口大小的改变而改变,您可以使用ComponentListener来监听组件大小的改变事件,然后根据新的组件大小重新缩放图像。 以下是一个示例代码: ```java import java.awt.Component; import java.awt.Dimension; import java.awt.Image; import java.awt.event.ComponentAdapter; import java.awt.event.ComponentEvent; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class ImageResizeExample { private static BufferedImage image; private static JLabel label; public static void main(String[] args) { JFrame frame = new JFrame("Image Resize Example"); JPanel panel = new JPanel(); try { // Load the image from file image = ImageIO.read(new File("image.jpg")); } catch (IOException e) { e.printStackTrace(); } // Create a label with the image label = new JLabel(new ImageIcon(image)); // Add the label to the panel panel.add(label); // Add the panel to the frame frame.add(panel); // Add a component listener to the label label.addComponentListener(new ComponentAdapter() { @Override public void componentResized(ComponentEvent e) { // Get the new size of the label Dimension size = label.getSize(); // Scale the image to fit the new size Image scaledImage = image.getScaledInstance(size.width, size.height, Image.SCALE_SMOOTH); // Update the label with the scaled image label.setIcon(new ImageIcon(scaledImage)); } }); // Set the size and show the frame frame.setSize(400, 400); frame.setVisible(true); } } ``` 在这个例子中,我们使用了JPanel和JLabel来显示图片,并在JLabel上添加了一个ComponentListener来监听大小改变事件。当JLabel的大小发生改变时,我们重新缩放原始图像,并将其设置为JLabel的新图标。这样,随着窗口大小改变,图像也会自动缩放
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值