首页 > 代码库 > (转)Java中Image的水平翻转、缩放与自由旋转操作
(转)Java中Image的水平翻转、缩放与自由旋转操作
来自:http://cping1982.blog.51cto.com/601635/130066/
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://cping1982.blog.51cto.com/601635/130066
老生常谈了,直接看代码。
bufferedimage.getColorModel().getTransparency()以获得原始图片透明度用于生成缓存图,在缓存图进行相应操作即可。
/**
* 旋转图片为指定角度
*
* @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 = createImage(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 = createImage(w, h, bufferedimage
.getColorModel().getTransparency())).createGraphics())
.drawImage(bufferedimage, 0, 0, w, h, w, 0, 0, h, null);
graphics2d.dispose();
return img;
}
* 旋转图片为指定角度
*
* @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 = createImage(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 = createImage(w, h, bufferedimage
.getColorModel().getTransparency())).createGraphics())
.drawImage(bufferedimage, 0, 0, w, h, w, 0, 0, h, null);
graphics2d.dispose();
return img;
}
本文出自 “Java究竟怎么玩” 博客,请务必保留此出处http://cping1982.blog.51cto.com/601635/130066
(转)Java中Image的水平翻转、缩放与自由旋转操作
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。