java实现图片水平和垂直翻转效果
最近在做小型游戏,遇到了要翻转图片的苦恼,经过我一下午的研究,终于发现了一种好用的方法。
部分代码来源于别人,我在这个基础上修改了下,变得更好用了,之前的别人Image输入都是BufferedImage,我改成了Image,也就是加了一个转换。
大家如果看不懂代码没关系,会用就行了,我会展示一下怎么用的。
ImageRotate类
package mypackage; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; /** * Author Mythos_Q * * Time 2011-07-20 * * Description 图片旋转、翻转处理 */ public class ImgRotate { static int Up_Down_Reverse = 0; static int Left_Right_Reverse = 1; /** * 旋转图片为指定角度 * * @param bufferedimage * 目标图像 * @param degree * 旋转角度 * @return */ public static BufferedImage rotateImage(final BufferedImage bufferedimage,final int degree){ int w = bufferedimage.getWidth(); int h = bufferedimage.getHeight(); int type = bufferedimage.getColorModel().getTransparency(); BufferedImage img; Graphics2D graphics2d; (graphics2d = (img = new BufferedImage(h, w, type)).createGraphics()).setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2d.rotate(Math.toRadians(degree), w / 2, h / 2 + (w>h?(w-h)/2:(h-w)/2)); graphics2d.drawImage(bufferedimage, 0, 0, null); graphics2d.dispose(); return img; } /** * 旋转图片为指定角度 * * @param file * 目标图像 * @param degree * 旋转角度(90,180,270) * @return */ public static File rotateImage(File file,int degree) throws Exception{ if(degree==90) return rotateImage90(file); if(degree==180) return rotateImage180(file); if(degree==270) return rotateImage270(file); return null; } public static Image rotateImage(Image Image,int degree) { if(degree==90) return rotateImage90(Image); if(degree==180) return rotateImage180(Image); if(degree==270) return rotateImage270(Image); return null; } private static Image rotateImage90(Image image) { BufferedImage bufferedimage = ImageToBufferedImage(image); int w = bufferedimage.getWidth(); int h = bufferedimage.getHeight(); int type = bufferedimage.getColorModel().getTransparency(); BufferedImage img; Graphics2D graphics2d; (graphics2d = (img = new BufferedImage(h, w, type) ).createGraphics() ).setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2d.rotate(Math.toRadians(270), w / 2, h / 2 + (w-h)/2); graphics2d.drawImage(bufferedimage, 0, 0, null); graphics2d.dispose(); return BufferedImageToImage(img); } //左转90度 public static File rotateImage90(File file) throws Exception { BufferedImage bufferedimage = ImageIO.read(file); int w = bufferedimage.getWidth(); int h = bufferedimage.getHeight(); int type = bufferedimage.getColorModel().getTransparency(); BufferedImage img; Graphics2D graphics2d; (graphics2d = (img = new BufferedImage(h, w, type) ).createGraphics() ).setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2d.rotate(Math.toRadians(270), w / 2, h / 2 + (w-h)/2); graphics2d.drawImage(bufferedimage, 0, 0, null); graphics2d.dispose(); ImageIO.write(img,"jpg",file); return file; } //右转90度 public static File rotateImage270(File file) throws Exception { BufferedImage bufferedimage = ImageIO.read(file); int w = bufferedimage.getWidth(); int h = bufferedimage.getHeight(); int type = bufferedimage.getColorModel().getTransparency(); BufferedImage img; Graphics2D graphics2d; (graphics2d = (img = new BufferedImage(h, w, type)).createGraphics()).setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2d.rotate(Math.toRadians(90), w / 2 - (w-h)/2, h / 2 ); graphics2d.drawImage(bufferedimage, 0, 0, null); graphics2d.dispose(); ImageIO.write(img,"jpg",file); return file; } public static Image rotateImage270(Image image) { BufferedImage bufferedimage = ImageToBufferedImage(image); int w = bufferedimage.getWidth(); int h = bufferedimage.getHeight(); int type = bufferedimage.getColorModel().getTransparency(); BufferedImage img; Graphics2D graphics2d; (graphics2d = (img = new BufferedImage(h, w, type)).createGraphics()).setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2d.rotate(Math.toRadians(90), w / 2 - (w-h)/2, h / 2 ); graphics2d.drawImage(bufferedimage, 0, 0, null); graphics2d.dispose(); return BufferedImageToImage(img); } //对转 public static File rotateImage180(File file) throws Exception { BufferedImage bufferedimage = ImageIO.read(file); int w = bufferedimage.getWidth(); int h = bufferedimage.getHeight(); int type = bufferedimage.getColorModel().getTransparency(); BufferedImage img; Graphics2D graphics2d; (graphics2d = (img = new BufferedImage(w, h, type)).createGraphics()).setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2d.rotate(Math.toRadians(180), w / 2, h / 2 ); graphics2d.drawImage(bufferedimage, 0, 0, null); graphics2d.dispose(); ImageIO.write(img,"jpg",file); return file; } public static Image rotateImage180(Image image) { BufferedImage bufferedimage = ImageToBufferedImage(image); int w = bufferedimage.getWidth(); int h = bufferedimage.getHeight(); int type = bufferedimage.getColorModel().getTransparency(); BufferedImage img; Graphics2D graphics2d; (graphics2d = (img = new BufferedImage(w, h, type)).createGraphics()).setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR); graphics2d.rotate(Math.toRadians(180), w / 2, h / 2 ); graphics2d.drawImage(bufferedimage, 0, 0, null); graphics2d.dispose(); return BufferedImageToImage(img); } /*** * 图片镜像处理 * @param file * @param FX 0 为上下反转 1 为左右反转 * @return */ public static void imageMisro(File file,int FX) { try { BufferedImage bufferedimage = ImageIO.read(file); int w = bufferedimage.getWidth(); int h = bufferedimage.getHeight(); int[][] datas = new int[w][h]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { datas[j][i] = bufferedimage.getRGB(j, i); } } int[][] tmps = new int[w][h]; if (FX == 0) { for (int i = 0, a = h - 1; i < h; i++, a--) { for (int j = 0; j < w; j++) { tmps[j][a] = datas[j][i]; } } } else if (FX == 1) { for (int i = 0; i < h; i++) { for (int j = 0, b = w - 1; j < w; j++, b--) { tmps[b][i] = datas[j][i]; } } } for (int i = 0; i < h; i++){ for (int j = 0; j<w ;j++){ bufferedimage.setRGB(j, i, tmps[j][i]); } } ImageIO.write(bufferedimage, "jpg", file); } catch (Exception e) { e.printStackTrace(); } } /* * * 镜像处理,输入image和方式,返回翻转的新image * type = 0 表示上下翻转,type = 1 表示左右翻转 */ public static Image imageMisro(Image image,int type ) { try { //用到了自己写的方法 BufferedImage bufferedimage = ImageToBufferedImage(image); int w = bufferedimage.getWidth(); int h = bufferedimage.getHeight(); int[][] datas = new int[w][h]; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { datas[j][i] = bufferedimage.getRGB(j, i); } } int[][] tmps = new int[w][h]; if (type == 0) { for (int i = 0, a = h - 1; i < h; i++, a--) { for (int j = 0; j < w; j++) { tmps[j][a] = datas[j][i]; } } } else if (type == 1) { for (int i = 0; i < h; i++) { for (int j = 0, b = w - 1; j < w; j++, b--) { tmps[b][i] = datas[j][i]; } } } for (int i = 0; i < h; i++){ for (int j = 0; j<w ;j++){ bufferedimage.setRGB(j, i, tmps[j][i]); } } Image newImage = (Image)bufferedimage; return newImage; //ImageIO.write(bufferedimage, "jpg", file); } catch (Exception e) { e.printStackTrace(); } return null; } //Image转换成BufferedImage public static BufferedImage ImageToBufferedImage( Image image ) { BufferedImage bufferedimage = new BufferedImage (image.getWidth(null), image.getHeight(null),BufferedImage.TYPE_INT_RGB); Graphics g = bufferedimage.createGraphics(); g.drawImage(image, 0, 0, null); //这里,大家可能会有疑问,似乎没有对bufferedimage干啥 g.dispose(); //但是是正确的,g调用drawImage就自动保存了 return bufferedimage; } //BufferedImage 转换成Image类型 public static Image BufferedImageToImage( BufferedImage b ) { return (Image)b; } }
下面展示用法:
package mypackage; import java.awt.*; import java.awt.image.*; import java.net.URL; import javax.swing.*; public class Test extends JPanel { JFrame frame; Image image = new ImageIcon("hand2.jpg").getImage(); public void paint( Graphics g ) { g.drawImage(image, 0, 0,null); //之前的图片 Image m = ImgRotate.imageMisro(image, ImgRotate.Left_Right_Reverse); g.drawImage(m, 200, 200, null); //水平翻转的图片 Image mm = ImgRotate.rotateImage(m, 90); //这里只能填90,180,270 g.drawImage(mm, 250, 0, null); } public Test() { frame = new JFrame(); frame.add(this); frame.setSize(500,500); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } public static void main(String[] args) { new Test(); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)