Java旋转图片

之前做项目遇到的一个问题,需要把竖直显示的图片转换成水平(横着)显示,思路就是获取图片信息,如果图片的高比宽数值大就做旋转,下面是代码~

先是一个工具类,方便调用

package com.haha.controllers;

import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

public class RotateImageUtil {

    public static BufferedImage rotateImage(BufferedImage bufferedImage, int angel) {
        if (bufferedImage == null) {
            return null;
        }

        if (angel < 0) {
            // 将负数角度,纠正为正数角度
            angel = angel + 360;
        }

        int imageWidth = bufferedImage.getWidth(null);
        int imageHeight = bufferedImage.getHeight(null);

        // 计算重新绘制图片的尺寸
        Rectangle rectangle = calculatorRotatedSize(new Rectangle(new Dimension(imageWidth, imageHeight)), angel);

        // 获取原始图片的透明度
        int type = bufferedImage.getColorModel().getTransparency();
        BufferedImage newImage = null;
        newImage = new BufferedImage(rectangle.width, rectangle.height, type);
        Graphics2D graphics = newImage.createGraphics();

        // 平移位置
        graphics.translate((rectangle.width - imageWidth) / 2, (rectangle.height - imageHeight) / 2);

        // 旋转角度
        graphics.rotate(Math.toRadians(angel), imageWidth / 2, imageHeight / 2);

        // 绘图
        graphics.drawImage(bufferedImage, null, null);
        return newImage;
    }

    public static BufferedImage rotateImage(Image image, int angel) {
        if (image == null) {
            return null;
        }
        if (angel < 0) {
            // 将负数角度,纠正为正数角度
            angel = angel + 360;
        }
        int imageWidth = image.getWidth(null);
        int imageHeight = image.getHeight(null);

        Rectangle rectangle = calculatorRotatedSize(new Rectangle(new Dimension(imageWidth, imageHeight)), angel);

        BufferedImage newImage = null;
        newImage = new BufferedImage(rectangle.width, rectangle.height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics = newImage.createGraphics();

        graphics.translate((rectangle.width - imageWidth) / 2, (rectangle.height - imageHeight) / 2);
        graphics.rotate(Math.toRadians(angel), imageWidth / 2, imageHeight / 2);
        graphics.drawImage(image, null, null);

        return newImage;
    }

    //计算旋转后的尺寸
    private static Rectangle calculatorRotatedSize(Rectangle src, int angel) {
        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));
        int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha - angel_dalta_height));

        int des_width = src.width + len_dalta_width * 2;
        int des_height = src.height + len_dalta_height * 2;

        return new java.awt.Rectangle(new Dimension(des_width, des_height));
    }
}

然后搞个main方法调用一下

package com.haha.controllers;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class test {

    //测试一下,直接抛出异常了,小伙伴们项目中要记住用try···catch···哦
    public static void main(String[] args) throws Exception {
        File picture = new File("C:\\Users\\Administrator\\Desktop\\saga.png");
        BufferedImage sourceImg = ImageIO.read(new FileInputStream(picture));
        if ( sourceImg.getHeight() > sourceImg.getWidth() ) {
            //旋转,这里数值代表顺时针,逆时针,各位可以换其他角度玩儿玩儿
            BufferedImage rotate = RotateImageUtil.rotateImage(sourceImg, -90);

            ImageIO.write(rotate, "png", new File("D:\\", picture.getName()));

            InputStream is = new FileInputStream( new File("D:\\", picture.getName()) );
            byte[] data = new byte[is.available()];
            is.read(data);
            is.close();

        } else {
            System.out.println(false);
        }
    }

}

实现效果:旋转前:

旋转后:

over~

  • 6
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值