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

时间:2021-05-19

本文实例为大家分享了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; }}

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

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章