如何使用Graphics和Affinetransform绘制透视图

问题描述 投票:0回答:1

我想用Java画一幅画。 我想沿着X轴或Y轴旋转这张图片,使图片透视。 它可以使图像三维。 你知道PowerPoint中的“3D旋转”功能吗?我就是想达到这样的效果。 我可以用java来制作吗?

很抱歉之前没有仔细描述我的问题。 这是原图:

原图

我画的时候加了剪切操作

@Override
protected void paintComponent(Graphics g) {
    Graphics2D g2d  = (Graphics2D)g;
    g2d.clearRect(0, 0, this.getWidth(), this.getHeight());
    if (sourceImage != null) {
        g2d.shear(0.5, 0);
        g2d.drawImage(sourceImage, 0, 0, sourceImage.getWidth(), sourceImage.getHeight(), null);
    }
}

然后它看起来像这样

变换后的图像

但是这是一个线性变换,我想要透视图像,就像这样

我需要实现什么

JAVA中的AffineTransform只能做线性变换。 如何使用PerspectiveTransform,是否需要使用OpenGL或OpenCV来实现?

这是我的完整代码

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
public class ImagePanel extends JPanel{

private static final long serialVersionUID = 1L;

private BufferedImage sourceImage;

public ImagePanel() {
}

@Override
protected void paintComponent(Graphics g) {
    Graphics2D g2d  = (Graphics2D)g;
    g2d.clearRect(0, 0, this.getWidth(), this.getHeight());
    if (sourceImage != null) {
        g2d.shear(0.5, 0);
        g2d.drawImage(sourceImage, 0, 0, sourceImage.getWidth(), sourceImage.getHeight(), null);
    }
}

public BufferedImage getSourceImage() {
    return sourceImage;
}

public void setSourceImage(BufferedImage sourceImage) {
    this.sourceImage = sourceImage;
}

}

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.filechooser.FileNameExtensionFilter;

public class MainUI extends JFrame implements ActionListener {

private static final long serialVersionUID = 1L;

public static final String IMAGE_CMD = "choose image ... ";

private JButton imgBtn;

private ImagePanel imagePanel;

private BufferedImage srcImage;

public MainUI() {
    setTitle("image demo");
    imgBtn = new JButton(IMAGE_CMD);
    
    JPanel btnPanel = new JPanel();
    btnPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
    btnPanel.add(imgBtn);
    
    imagePanel = new ImagePanel();
    getContentPane().setLayout(new BorderLayout());
    getContentPane().add(imagePanel, BorderLayout.CENTER);
    getContentPane().add(btnPanel, BorderLayout.SOUTH);
    
    imgBtn.addActionListener(this);
}

@Override
public void actionPerformed(ActionEvent e) {
    if(IMAGE_CMD.equals(e.getActionCommand())){
        try {
            JFileChooser chooser = new JFileChooser();
            setFileTypeFilter(chooser);
            chooser.showOpenDialog(null);
            File f = chooser.getSelectedFile();
            if (f != null) {
                srcImage = ImageIO.read(f);
                imagePanel.setSourceImage(srcImage);
                imagePanel.repaint();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
        imagePanel.repaint();
    }
}
public void setFileTypeFilter(JFileChooser chooser){
    FileNameExtensionFilter filter = new FileNameExtensionFilter("JPG & PNG Images", "jpg", "png");
    chooser.setFileFilter(filter);
}
public void openView(){
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setPreferredSize(new Dimension(1280, 960));
    pack();
    setVisible(true);
}
public static void main(String[] args) {
    MainUI ui = new MainUI();
    ui.openView();
}

}

java swing graphics 3d affinetransform
1个回答
0
投票

你看过 jhlabs 的 PerspectiveFilter 吗?听起来像是您要找的东西。

http://www.jhlabs.com/ip/filters/PerspectiveFilter.html

© www.soinside.com 2019 - 2024. All rights reserved.