时间:2021-05-19
最近在做小型游戏,遇到了要翻转图片的苦恼,经过我一下午的研究,终于发现了一种好用的方法。
部分代码来源于别人,我在这个基础上修改了下,变得更好用了,之前的别人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(); } }以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
一、Canvas图片水平镜像翻转效果预览您可以狠狠的点击这里:canvas图片水平镜像翻转动画demodemo页面中点击图片动画效果可见。二、Canvas上实现
目的:利用html5,css实现钟摆效果知识点:1)利用position/left/top和calc()实现元素的水平和垂直居中;2)利用CSS3的animat
本文实例为大家分享了JavaOpenCV实现图像镜像翻转效果的具体代码,供大家参考,具体内容如下主要使用OpenCV的flip()方法,可以实现图像的垂直、水平
最终实现效果为实现行内元素的水平、垂直居中对齐,效果图如下:接下来为实现代码:复制代码代码如下:利用vertical-align实现图片垂直居中对齐#conte
这是一个未知大小的图片在一个已知大小容器中的水平和垂直居中的例子,原文标题:“Centeringanimageofunknownsizeinanoutercon