欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现图片任意角度旋转

程序员文章站 2024-02-22 19:01:54
本文实例为大家分享了java实现图片旋转,供大家参考,具体内容如下 方法一:普通方法实现图片旋转 /** * 图像旋转 * @param src...

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

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