服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - java实现图片任意角度旋转

java实现图片任意角度旋转

2021-07-29 11:42落日流年 Java教程

这篇文章主要为大家详细介绍了java实现图片任意角度旋转,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
   * 图像旋转
   * @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实现图片旋转

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
   * 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;
}

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

原文链接:https://blog.csdn.net/zwl18210851801/article/details/81279622

延伸 · 阅读

精彩推荐