0

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

}

C.K.Jun
  • 1
  • 1
  • You're trying to perform a 3D effect with 2D library, pretty sure you're not going to get it to work - unfortunately, I can't suggest an alternative – MadProgrammer Apr 11 '22 at 07:40
  • [zxing `PerspectiveTransform`](https://zxing.github.io/zxing/apidocs/com/google/zxing/common/PerspectiveTransform.html) . JavaFX (apparently) has support for this kind of stuff – MadProgrammer Apr 11 '22 at 07:43
  • @MadProgrammer Thanks for your advice, but the project in my company is used swing&awt. I'll look for alternatives – C.K.Jun Apr 11 '22 at 07:50
  • [For example](https://stackoverflow.com/questions/3202323/java-image-transformation) (or lack of, but it might take in to some more information). OpenCV apparently can do it, but I've never used it – MadProgrammer Apr 11 '22 at 07:51
  • [Example](https://stackoverflow.com/questions/7366099/skew-or-distort-image-object-in-java) using JavaFX that works on a `BufferedImage` - but yes, you'll need the FX libraries installed and enabled ... and that's a whole other mess – MadProgrammer Apr 11 '22 at 07:53
  • @MadProgrammer I have found the Class JAI PerspectiveTransform and now I am looking for the way to use it.The document is really sparse.Of course OpenCV can slove my question,its function is strong,I just want to do something simple.if i can not solve it with jdk , I will consider using OpenCV. Thank you for your help. – C.K.Jun Apr 11 '22 at 08:04
  • AFAKI JAI is no longer supported – MadProgrammer Apr 11 '22 at 21:22
  • @MadProgrammer All right, I'll think about another – C.K.Jun Apr 12 '22 at 00:43

0 Answers0