How do I draw an image in the shape of a parallelogram?

后端 未结 2 533
栀梦
栀梦 2020-12-22 06:25

So, I know that images can be cropped, shrunk, and expanded, but can you adapt an image into the shape of a parallelogram? I\'m using Java Swing to draw images. I was thinki

相关标签:
2条回答
  • 2020-12-22 06:35

    Just an example using a Java image processing framework.

    Output:

    enter image description here

    public class SkewExample extends JFrame{
        MarvinImagePlugin skew = MarvinPluginLoader.loadImagePlugin("org.marvinproject.image.transform.skew");
    
        public SkewExample(){
            super("Skew Example");
            // Layout
            setLayout(new GridLayout(6,1));
    
            // Load Image
            MarvinImage image = MarvinImageIO.loadImage("./res/chamaleon.jpg");
            skew.setAttribute("skew", "Horizontal");
    
            // Process the image multiple times with different angle.
            for(int i=1; i<=6; i++){
                add(new JLabel(new ImageIcon(skew(image, i*7).getBufferedImage())));
            }
            setSize(340,880);
            setVisible(true);
        }
        private MarvinImage skew(MarvinImage imageIn, int angle){
            skew.setAttribute("SkewAngle", angle);
            MarvinImage ret = new MarvinImage(imageIn.getWidth(),imageIn.getHeight());
            ret.fillRect(0, 0,imageIn.getWidth(),imageIn.getHeight(), new Color(238,238,238));
            ret.update();
            skew.process(imageIn, ret);
            ret.update();
            return ret;
        }
        public static void main(String[] args) {
            new SkewExample().setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        }
    }
    
    0 讨论(0)
  • 2020-12-22 06:37

    You could use AffineTransform.getShearInstance.

    This example uses a AffineTransformOp to "filter" the original image

    skewd

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.EventQueue;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.GridLayout;
    import java.awt.RenderingHints;
    import java.awt.geom.AffineTransform;
    import java.awt.image.AffineTransformOp;
    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;
    import javax.swing.UIManager;
    import javax.swing.UnsupportedLookAndFeelException;
    
    public class SkewImage {
    
        public static void main(String[] args) {
            new SkewImage();
        }
    
        public SkewImage() {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                    } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    }
    
                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setLayout(new BorderLayout());
                    frame.add(new TestPane());
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                }
            });
        }
    
        public class TestPane extends JPanel {
    
            public TestPane() {
                setLayout(new GridLayout(1, 2));
                try {
                    BufferedImage original = ImageIO.read(new File("C:\\hold\\thumbnails\\Megatokyo_707___Torn_by_crusaderky.jpg"));
                    BufferedImage skew = new BufferedImage(original.getWidth(), original.getHeight(), BufferedImage.TYPE_INT_ARGB);
    
                    // Adjust the image width if we use a negative skew...
                    double skewX = 0.3d;
                    double x = (skewX < 0) ? -skewX * original.getHeight() : 0;
                    AffineTransform at = AffineTransform.getTranslateInstance(x, 0);
                    at.shear(skewX, 0);
                    AffineTransformOp op = new AffineTransformOp(at,
                            new RenderingHints(RenderingHints.KEY_INTERPOLATION,
                                    RenderingHints.VALUE_INTERPOLATION_BICUBIC));
                    skew = op.filter(original, null);
    
                    add(new JLabel(new ImageIcon(original)));
                    add(new JLabel(new ImageIcon(skew)));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题