C.K.Jun
C.K.Jun

Reputation: 1

How can I use Graphics and Affinetransform draw a perspective picture

I want draw a picture with Java. And I want rotate this picture along the X-axis or Y-axis to make the picture perspective. It can make the image three-dimensional. Do you know the function '3D rotation' in the PowerPoint? I just want to achieve this effect. Can I use java to make it?

I am sorry that I have not describe my question carefully before. This is the original image:

original image

I added a shear operation when I drew it

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

And then it looks like this

The transformed image

But this is a linear transformation, I want perspective image, just like this

What I need to achieve

The AffineTransform in JAVA can only do linear transformations. How can I use PerspectiveTransform, Do I need to use OpenGL or OpenCV to achieve it?

This is my complete code

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

}

Upvotes: 0

Views: 382

Answers (1)

Jose
Jose

Reputation: 90

Have you looked at PerspectiveFilter from jhlabs? It sounds like what you're looking for.

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

Upvotes: 0

Related Questions