欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现图片水平和垂直翻转效果

程序员文章站 2024-02-22 18:58:22
最近在做小型游戏,遇到了要翻转图片的苦恼,经过我一下午的研究,终于发现了一种好用的方法。 部分代码来源于别人,我在这个基础上修改了下,变得更好用了,之前的别人image输...

最近在做小型游戏,遇到了要翻转图片的苦恼,经过我一下午的研究,终于发现了一种好用的方法。

部分代码来源于别人,我在这个基础上修改了下,变得更好用了,之前的别人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();
 
 }
 
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。