Image resizing and displaying in a JPanel or a JLabel without loss of quality

后端 未结 1 1859
闹比i
闹比i 2020-11-29 11:39

I\'m developing a java program to capture employee images at the time of registration using a webcam. I can obtain the picture without any problem, and save it in my C: driv

相关标签:
1条回答
  • 2020-11-29 12:29

    Of course you can resize the image there are many different ways like Image#getScaledInstance(int width,int height,int hints), but this has its perils.

    The main problem being:

    Image.getScaledInstance() does not return a finished, scaled image. It leaves much of the scaling work for a later time when the image pixels are used.

    I would not recommend using it but here is a nice example.

    Alternatively you can use this method:

    import javax.swing.ImageIcon;
    import java.awt.image.BufferedImage;
    import java.awt.Image;
    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.io.File;
    import javax.imageio.ImageIO;
    import java.awt.RenderingHints;
    
    public class ImgUtils {
    
    public BufferedImage scaleImage(int WIDTH, int HEIGHT, String filename) {
        BufferedImage bi = null;
        try {
            ImageIcon ii = new ImageIcon(filename);//path to image
            bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = (Graphics2D) bi.createGraphics();
            g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY));
            g2d.drawImage(ii.getImage(), 0, 0, WIDTH, HEIGHT, null);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return bi;
    }
    
    }
    

    you'd use it like:

    final BufferedImage img=new ImgUtils().scaleImage(200,200,"c:/test.jpg");
    //create label with image as background
    JLabel label=new JLabel(new ImageIcon((Image)img));
    

    UPDATE:

    Here is a small example I made:

    enter image description here

    import java.awt.BorderLayout;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.RenderingHints;
    import java.awt.image.BufferedImage;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.SwingUtilities;
    
    public class JavaApplication117 {
    
        //change this to your own
        static String filename="c:/test.jpg";
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new JavaApplication117().createAndShowUI();
                }
            });
        }
    
        private void createAndShowUI() {
            JFrame frame = new JFrame("Test");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            initComponents(frame);
    
            frame.setResizable(false);
            frame.pack();
            frame.setVisible(true);
        }
    
        private void initComponents(JFrame frame) {
            final BufferedImage img = new ImgUtils().scaleImage(200, 200, filename);
            //create label with image as background
            JLabel label = new JLabel(new ImageIcon((Image) img));
    
            frame.getContentPane().add(label, BorderLayout.CENTER);
        }
    }
    
    class ImgUtils {
    
        public BufferedImage scaleImage(int WIDTH, int HEIGHT, String filename) {
            BufferedImage bi = null;
            try {
                ImageIcon ii = new ImageIcon(filename);//path to image
                bi = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
                Graphics2D g2d = (Graphics2D) bi.createGraphics();
                g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY));
                g2d.drawImage(ii.getImage(), 0, 0, WIDTH, HEIGHT, null);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
            return bi;
        }
    }
    

    References:

    • resizing image java getScaledInstance
    0 讨论(0)
提交回复
热议问题