如何以平行四边形的形状绘制图像?

时间:2014-07-08 00:30:42

标签: java swing image-processing graphics

因此,我知道图像可以裁剪,缩小和扩展,但是您可以将图像调整为平行四边形的形状吗?我正在使用Java Swing来绘制图像。我想也许某些类或BufferedImage的某些方法可能会成功,但我找不到任何东西。我也搜索了谷歌一段时间,但我找不到答案。有谁知道我可以这样做的方法,或者解释它的网页?提前谢谢。

2 个答案:

答案 0 :(得分:6)

您可以使用AffineTransform.getShearInstance

此示例使用AffineTransformOp到"过滤器"原始图像

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();
            }
        }
    }

}

答案 1 :(得分:1)

使用Java图像处理框架的一个例子。

<强>输出:

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);
    }
}