Resize a picture to fit a JLabel

后端 未结 8 524
不思量自难忘°
不思量自难忘° 2020-11-29 01:56

I\'m trying to make a picture fit a JLabel. I wish to reduce the picture dimensions to something more appropriate for my Swing JPanel.

I tried with setPreferredSiz

相关标签:
8条回答
  • 2020-11-29 02:21

    Outline

    Here are the steps to follow.

    • Read the picture as a BufferedImage.
    • Resize the BufferedImage to another BufferedImage that's the size of the JLabel.
    • Create an ImageIcon from the resized BufferedImage.

    You do not have to set the preferred size of the JLabel. Once you've scaled the image to the size you want, the JLabel will take the size of the ImageIcon.

    Read the picture as a BufferedImage

    BufferedImage img = null;
    try {
        img = ImageIO.read(new File("strawberry.jpg"));
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    Resize the BufferedImage

    Image dimg = img.getScaledInstance(label.getWidth(), label.getHeight(),
            Image.SCALE_SMOOTH);
    

    Make sure that the label width and height are the same proportions as the original image width and height. In other words, if the picture is 600 x 900 pixels, scale to 100 X 150. Otherwise, your picture will be distorted.

    Create an ImageIcon

    ImageIcon imageIcon = new ImageIcon(dimg);
    
    0 讨论(0)
  • You can try it:

    ImageIcon imageIcon = new ImageIcon(new ImageIcon("icon.png").getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));
    label.setIcon(imageIcon);
    

    Or in one line:

    label.setIcon(new ImageIcon(new ImageIcon("icon.png").getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT)));
    

    The execution time is much more faster than File and ImageIO.

    I recommend you to compare the two solutions in a loop.

    0 讨论(0)
  • 2020-11-29 02:26

    i have done the following and it worked perfectly

    try {
            JFileChooser jfc = new JFileChooser();
            jfc.showOpenDialog(null);
            File f = jfc.getSelectedFile();
            Image bi = ImageIO.read(f);
            image1.setText("");
            image1.setIcon(new ImageIcon(bi.getScaledInstance(int width, int width, int width)));
    
        } catch (Exception e) {
        } 
    
    0 讨论(0)
  • 2020-11-29 02:27

    The best and easy way for image resize using Java Swing is:

    jLabel.setIcon(new ImageIcon(new javax.swing.ImageIcon(getClass().getResource("/res/image.png")).getImage().getScaledInstance(200, 50, Image.SCALE_SMOOTH)));
    

    For better display, identify the actual height & width of image and resize based on width/height percentage

    0 讨论(0)
  • 2020-11-29 02:28
    public void selectImageAndResize(){    
        int returnVal = jFileChooser.showOpenDialog(this); //open jfilechooser
        if (returnVal == jFileChooser.APPROVE_OPTION) {    //select image
            File file = jFileChooser.getSelectedFile();    //get the image
            BufferedImage bi;
            try {
                //
                //transforms selected file to buffer
                //
                bi=ImageIO.read(file);  
                ImageIcon iconimage = new ImageIcon(bi);
    
                //
                //get image dimensions
                //
                BufferedImage bi2 = new BufferedImage(iconimage.getIconWidth(), iconimage.getIconHeight(), BufferedImage.TYPE_INT_ARGB); 
                Graphics g = bi.createGraphics();
                iconimage.paintIcon(null, g, 0,0);
                g.dispose();
    
                //
                //resize image according to jlabel
                //
                BufferedImage resizedimage=resize(bi,jLabel2.getWidth(), jLabel2.getHeight()); 
                ImageIcon resizedicon=new ImageIcon(resizedimage);
                jLabel2.setIcon(resizedicon);
            } catch (Exception ex) {
                System.out.println("problem accessing file"+file.getAbsolutePath());
            }
        }
        else {
            System.out.println("File access cancelled by user.");
        }
    }
    
    0 讨论(0)
  • 2020-11-29 02:35

    Assign your image to a string. Eg image Now set icon to a fixed size label.

    image.setIcon(new javax.swing.ImageIcon(image.getScaledInstance(50,50,WIDTH)));
    
    0 讨论(0)
提交回复
热议问题