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

 更新时间:2019年02月10日 08:43:55   作者:benw1988  
这篇文章主要为大家详细介绍了Java实现图像旋转,指定图像大小,水平翻转图像,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

相关文章

  • MybatisPlus 自定义.vm模板的生成

    MybatisPlus 自定义.vm模板的生成

    为更加快捷方便的开发代码,使用MybatisPlus的代码自动生成功能,将一些繁琐的操作自动生成,本文主要介绍了MybatisPlus 自定义.vm模板的生成,感兴趣的可以了解一下
    2024-03-03
  • Java精品项目瑞吉外卖之员工信息管理篇

    Java精品项目瑞吉外卖之员工信息管理篇

    这篇文章主要为大家详细介绍了java精品项目-瑞吉外卖订餐系统,此项目过大,分为多章独立讲解,本篇内容为员工信息分页查询与启用或禁用员工状态,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • Java过滤器@WebFilter用法详解

    Java过滤器@WebFilter用法详解

    @WebFilter用于将一个类声明为过滤器,该注解将会在部署时被容器处理,容器将根据具体的属性配置将相应的类部署为过滤器,这篇文章主要给大家介绍了关于Java过滤器@WebFilter用法的相关资料,需要的朋友可以参考下
    2024-01-01
  • Spring中存对象和取对象的方式详解

    Spring中存对象和取对象的方式详解

    这篇文章主要介绍了Spring中存对象和取对象的方式,Spring中更简单的存对象与取对象的方式是注解,注解实现有两种方式:一在编译的时候,把注解替换成相关代码,并添加到我们原来的代码中,二拦截方法,文中有详细的代码示例供大家参考,需要的朋友可以参考下
    2024-08-08
  • SpringBoot调用第三方WebService接口的两种方法

    SpringBoot调用第三方WebService接口的两种方法

    本文主要介绍了SpringBoot调用第三方WebService接口的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-06-06
  • SpringBoot中FailureAnalyzer的使用详解

    SpringBoot中FailureAnalyzer的使用详解

    这篇文章主要介绍了SpringBoot中FailureAnalyzer的使用详解,FailureAnalyzer拦截启动时异常,将异常转换成更加易读的信息并包装成org.springframework.boot.diagnostics.FailureAnalysis对象,监控应用启动过程,需要的朋友可以参考下
    2023-12-12
  • 被遗忘的Java关键字transient的使用详解

    被遗忘的Java关键字transient的使用详解

    在 Java 中,transient 是一个关键字,用于指定一个类的字段(成员变量)在序列化时应该被忽略。本文将通过示例为大家简单讲讲transient的使用,需要的可以参考一下
    2023-04-04
  • spring mvc中的@ModelAttribute注解示例介绍

    spring mvc中的@ModelAttribute注解示例介绍

    在Spring mvc中,注解@ModelAttribute是一个非常常用的注解,下面这篇文章主要给大家介绍了关于spring mvc中@ModelAttribute注解的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下。
    2017-09-09
  • Java同学找工作最懵圈的问题:到底啥是分布式系统开发经验?(推荐)

    Java同学找工作最懵圈的问题:到底啥是分布式系统开发经验?(推荐)

    这篇文章主要介绍了分布式系统开发经验,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-04-04
  • Java实现发送手机短信语音验证功能代码实例

    Java实现发送手机短信语音验证功能代码实例

    这篇文章主要介绍了Java实现发送手机短信语音验证功能代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09

最新评论