Image magick java

前端 未结 5 1061
误落风尘
误落风尘 2020-12-08 03:07

How can I modify image from java through ImageMagick? Is there any way of doing it?

5条回答
  •  误落风尘
    2020-12-08 03:37

    Resizing an image (using the easiest method) within the J2SE.

    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    import javax.imageio.ImageIO;
    import java.net.URL;
    
    class ResizeImage {
    
        public static void main(String[] args) throws Exception {
            URL url = new URL("http://pscode.org/media/citymorn2.jpg");
            final BufferedImage bi = ImageIO.read(url);
    
            Runnable r = new Runnable() {
                public void run() {
                    JLabel unresize = new JLabel(new ImageIcon(bi));
    
                    int width = (int)(bi.getWidth()*.75);
                    int height = (int)(bi.getHeight()*.75);
    
                    BufferedImage bi1 = new BufferedImage(width, height, 
                        BufferedImage.TYPE_INT_RGB);
                    Graphics g1 = bi1.getGraphics();
                    g1.drawImage( bi, 0, 0, width, height, null );
                    JLabel easyResize = new JLabel(new ImageIcon(bi1));
    
                    JPanel p = new JPanel();
                    p.add( unresize );
                    p.add( easyResize );
    
                    JOptionPane.showMessageDialog(null, p);
                }
            };
            SwingUtilities.invokeLater(r);
        }
    }
    

提交回复
热议问题