java实现图片任意角度旋转

 更新时间:2019年04月07日 16:08:27   作者:落日流年  
这篇文章主要为大家详细介绍了java实现图片任意角度旋转,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Java实现图片旋转,供大家参考,具体内容如下

方法一:普通方法实现图片旋转

/**
   * 图像旋转
   * @param src
   * @param angel
   * @return
*/
  public static BufferedImage Rotate(Image src, double angel) {
    int src_width = src.getWidth(null);
    int src_height = src.getHeight(null);
    // calculate the new image size
    Rectangle rect_des = CalcRotatedSize(new Rectangle(new Dimension(
        src_width, src_height)), angel);
 
    BufferedImage res = null;
    res = new BufferedImage(rect_des.width, rect_des.height,
        BufferedImage.TYPE_3BYTE_BGR);
    Graphics2D g2 = res.createGraphics();
    // transform
    g2.translate((rect_des.width - src_width) / 2,
        (rect_des.height - src_height) / 2);
    g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2);
 
    g2.drawImage(src, null, null);
    return res;
  }
 
  public static Rectangle CalcRotatedSize(Rectangle src, double angel) {
    // if angel is greater than 90 degree, we need to do some conversion
    if (angel >= 90) {
      if(angel / 90 % 2 == 1){
        int temp = src.height;
        src.height = src.width;
        src.width = temp;
      }
      angel = angel % 90;
    }
 
    double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2;
    double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r;
    double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2;
    double angel_dalta_width = Math.atan((double) src.height / src.width);
    double angel_dalta_height = Math.atan((double) src.width / src.height);
 
    int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha
        - angel_dalta_width));
    len_dalta_width=len_dalta_width>0?len_dalta_width:-len_dalta_width;
 
    int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha
        - angel_dalta_height));
    len_dalta_height=len_dalta_height>0?len_dalta_height:-len_dalta_height;
 
    int des_width = src.width + len_dalta_width * 2;
    int des_height = src.height + len_dalta_height * 2;
    des_width=des_width>0?des_width:-des_width;
    des_height=des_height>0?des_height:-des_height;
    return new java.awt.Rectangle(new Dimension(des_width, des_height));
}

方法二:opencv实现图片旋转

 /**
   * opencv实现图片旋转
   * @param splitImage
   * @param angle
   * @return
*/
  public static Mat rotate3(Mat splitImage, double angle)
  {
    double thera = angle * Math.PI / 180;
    double a = Math.sin(thera);
    double b = Math.cos(thera);
 
    int wsrc = splitImage.width();
    int hsrc = splitImage.height();
 
    int wdst = (int) (hsrc * Math.abs(a) + wsrc * Math.abs(b));
    int hdst = (int) (wsrc * Math.abs(a) + hsrc * Math.abs(b));
    Mat imgDst = new Mat(hdst, wdst, splitImage.type());
 
    Point pt = new Point(splitImage.cols() / 2, splitImage.rows() / 2);
    // 获取仿射变换矩阵
    Mat affineTrans = Imgproc.getRotationMatrix2D(pt, angle, 1.0);
 
    System.out.println(affineTrans.dump());
    // 改变变换矩阵第三列的值
    affineTrans.put(0, 2, affineTrans.get(0, 2)[0] + (wdst - wsrc) / 2);
    affineTrans.put(1, 2, affineTrans.get(1, 2)[0] + (hdst - hsrc) / 2);
 
    Imgproc.warpAffine(splitImage, imgDst, affineTrans, imgDst.size(),
        Imgproc.INTER_CUBIC | Imgproc.WARP_FILL_OUTLIERS);
    return imgDst;
}

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

相关文章

  • 如何在Java中优雅地判空详解

    如何在Java中优雅地判空详解

    这篇文章主要大家介绍了关于如何在Java中优雅地判空的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-11-11
  • 如何通过Java生成一个随机数

    如何通过Java生成一个随机数

    当我们需要在Java中生成随机数时,可以借助JDK中提供的Random类来实现,通过使用Random类,我们可以轻松地生成各种类型的随机数,下面我们就来看看如何利用Random类生成随机数吧
    2023-09-09
  • Spring使用Jackson实现转换XML与Java对象

    Spring使用Jackson实现转换XML与Java对象

    这篇文章主要为大家详细介绍了Spring如何使用Jackson实现转换XML与Java对象,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-02-02
  • java将excel转为pdf的方法步骤

    java将excel转为pdf的方法步骤

    之前工作需要,查了挺多种Excel转PDF的方法,下面这篇文章主要给大家介绍了关于java将excel转为pdf的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-01-01
  • SpringBoot 快速实现 api 加密的方法

    SpringBoot 快速实现 api 加密的方法

    在项目中,为了保证数据的安全,我们常常会对传递的数据进行加密,常用的加密算法包括对称加密(AES)和非对称加密(RSA),本文给大家介绍SpringBoot 快速实现 api 加密,感兴趣的朋友一起看看吧
    2023-10-10
  • shiro之记住登录信息

    shiro之记住登录信息

    Shiro提供了记住我(RememberMe)的功能,当关闭浏览器时下次再次打开还能记住你的信息,下面小编给大家分享shiro之记住登录信息的相关知识,感兴趣的朋友一起看看吧
    2017-09-09
  • 浅谈Spring AOP中args()和argNames的含义

    浅谈Spring AOP中args()和argNames的含义

    这篇文章主要介绍了Spring AOP中args()和argNames的含义,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • 使用kotlin编写spring cloud微服务的过程

    使用kotlin编写spring cloud微服务的过程

    这篇文章主要介绍了使用kotlin编写spring cloud微服务的相关知识,本文给大家提到配置文件的操作代码,给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-09-09
  • SpringBoot 整合 RabbitMQ 的使用方式(代码示例)

    SpringBoot 整合 RabbitMQ 的使用方式(代码示例)

    本文详细介绍了使用RabbitTemplate进行消息传递的几种模式,包括点对点通信、发布/订阅模式、工作队列模式、路由模式和主题模式,每种模式都通过代码示例展示了生产者和消费者的实现,帮助开发者理解和运用RabbitMQ进行高效的消息处理
    2024-10-10
  • 详解Spring整合mybatis--Spring中的事务管理(xml形式)

    详解Spring整合mybatis--Spring中的事务管理(xml形式)

    这篇文章主要介绍了Spring整合mybatis--Spring中的事务管理(xml形式),本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-11-11

最新评论