java实现图片翻转

1. package Assis;
2.
3. import java.awt.geom.AffineTransform;
4. import java.awt.image.AffineTransformOp;
5. import java.awt.image.BufferedImage;
6. import java.io.IOException;
7. import java.net.URL;
8.
9. import javax.imageio.ImageIO;
10. import javax.swing.ImageIcon;
11. import javax.swing.JFrame;
12. import javax.swing.JLabel;
13. import javax.swing.JTabbedPane;
14.
15. /**
16. * 使图片水平翻转、垂直翻转、旋转180度
17. *
18. * @author Administrator
19. */
20. public class TestPicture {
21.
22. public static void main(String[] args) throws IOException {
23. BufferedImage sourceImage = ImageIO.read(getURL("/user.png"));
24. BufferedImage dstImage = null;
25. // AffineTransform transform = new AffineTransform(-1, 0, 0, 1,
26. // sourceImage.getWidth(), 0);// 水平翻转
27. AffineTransform transform = new AffineTransform(1, 0, 0, -1, 0,
28. sourceImage.getHeight());// 垂直翻转
29. // AffineTransform transform = new AffineTransform(-1, 0, 0, -1,
30. // sourceImage.getWidth(), sourceImage.getHeight());// 旋转180度
31. AffineTransformOp op = new AffineTransformOp(transform,
32. AffineTransformOp.TYPE_BILINEAR);
33. dstImage = op.filter(sourceImage, null);
34.
35. JTabbedPane tabbedPane = new JTabbedPane();
36. tabbedPane.add("Source Transform", new JLabel(
37. new ImageIcon(sourceImage)));
38. tabbedPane.add("Affine Transform", new JLabel(new ImageIcon(dstImage)));
39.
40. JFrame jframe = new JFrame();
41. jframe.setSize(800, 600);
42. jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
43. jframe.getContentPane().add(tabbedPane);
44. jframe.setVisible(true);
45. }
46.
47. /** 获得文件的绝对地址 */
48. public static final URL getURL(String path) {
49. return "".getClass().getResource(path);
50. }
51. }

你可能感兴趣的:(J2SE)