/** * 旋转角度 * @param src 源图片 * @param angel 角度 * @return 目标图片 */ public static BufferedImage rotate(Image src, int 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_INT_RGB); 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; } /** * 计算转换后目标矩形的宽高 * @param src 源矩形 * @param angel 角度 * @return 目标矩形 */ private static Rectangle CalcRotatedSize(Rectangle src, int angel) { double cos = Math.abs(Math.cos(Math.toRadians(angel))); double sin = Math.abs(Math.sin(Math.toRadians(angel))); int des_width = (int)(src.width * cos) + (int)(src.height * sin); int des_height = (int)(src.height * cos) + (int)(src.width * sin); return new java.awt.Rectangle(new Dimension(des_width, des_height)); }
java旋转图片
猜你喜欢
转载自www.cnblogs.com/hdwang/p/10189645.html
今日推荐
周排行