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

Java实现图片旋转、指定图像大小和水平翻转

程序员文章站 2024-02-29 15:34:34
本文实例为大家分享了java实现图片旋转、指定图像大小、水平翻转,供大家参考,具体内容如下 package com.zeph.j2se.image; im...

本文实例为大家分享了java实现图片旋转、指定图像大小、水平翻转,供大家参考,具体内容如下

package com.zeph.j2se.image;
 
import java.awt.graphics2d;
import java.awt.renderinghints;
import java.awt.image.bufferedimage;
 
public class imageoperate {
 /**
 * 旋转图片为指定角度
 * 
 * @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(w, h, type)).creategraphics())
 .setrenderinghint(renderinghints.key_interpolation,
  renderinghints.value_interpolation_bilinear);
 graphics2d.rotate(math.toradians(degree), w / 2, h / 2);
 graphics2d.drawimage(bufferedimage, 0, 0, null);
 graphics2d.dispose();
 return img;
 }
 
 /**
 * 变更图像为指定大小
 * 
 * @param bufferedimage
 *   目标图像
 * @param w
 *   宽
 * @param h
 *   高
 * @return
 */
 public static bufferedimage resizeimage(final bufferedimage bufferedimage,
 final int w, final int h) {
 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.drawimage(bufferedimage, 0, 0, w, h, 0, 0,
 bufferedimage.getwidth(), bufferedimage.getheight(), null);
 graphics2d.dispose();
 return img;
 }
 
 /**
 * 水平翻转图像
 * 
 * @param bufferedimage
 *   目标图像
 * @return
 */
 public static bufferedimage flipimage(final bufferedimage bufferedimage) {
 int w = bufferedimage.getwidth();
 int h = bufferedimage.getheight();
 bufferedimage img;
 graphics2d graphics2d;
 (graphics2d = (img = new bufferedimage(w, h, bufferedimage
 .getcolormodel().gettransparency())).creategraphics())
 .drawimage(bufferedimage, 0, 0, w, h, w, 0, 0, h, null);
 graphics2d.dispose();
 return img;
 }
}

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