java图片处理(图片逆时针旋转90度,图片剪裁截取)

base64字符串转化成图片
给定坐标点,以及宽高范围,然后截取图片
图片逆时针旋转90度

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

public class ImgUtil {

    private static final Logger log = LoggerFactory.getLogger(ImgUtil.class);
    
    /**
     * 根据指定坐标 截取控件元素周边图片
     * @param inputPath 输入图片路径
     * @param x         截取位置X坐标
     * @param y         截取位置Y坐标
     * @param interceptWidth 以x坐标为基准,截取的宽度
     * @param interceptHeight 以y坐标为基准,截取的高度
     * @param outputPath 输出新图片路径
     */
    public static void interceptImg(String inputPath, int x, int y, int interceptWidth, int interceptHeight, String outputPath) {
        try(
            OutputStream out = new FileOutputStream(outputPath);   
        ) {
            File sourcePic = new File(inputPath);
            BufferedImage pic = ImageIO.read(sourcePic);
            //参数依次为,截取起点的x坐标,y坐标,截取宽度,截取高度
            BufferedImage pic2 = pic.getSubimage(x, y, interceptWidth, interceptHeight);
            //将截取的子图另行存储
            Image _img = pic2.getScaledInstance(interceptWidth, interceptHeight, Image.SCALE_AREA_AVERAGING);
            BufferedImage image = new BufferedImage(interceptWidth, interceptHeight, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = image.createGraphics();
            graphics.drawImage(_img, 0, 0, null);
            graphics.dispose();
            ImageIO.write(pic2, "png", out);
            out.flush();
        } catch (IOException e) {
            log.error("图片剪裁出错", e);
            throw new RuntimeException(e);
        }
    }


    // base64字符串转化成图片
    public static boolean base64ToImg(String imgBase64 ,String imgFilePath) {
        if(imgBase64 == null || StringUtils.isEmpty(imgBase64)){
            return false;
        }
        int index = imgBase64.indexOf(";base64,");
        if (index > -1) {
            imgBase64 = imgBase64.substring(index + 8);
        }
        if(imgFilePath == null || StringUtils.isEmpty(imgFilePath)){
            return false;
        }

        File dirFile = new File(imgFilePath);
        if(!dirFile.getParentFile().exists()
                && !dirFile.getParentFile().mkdirs()){
            log.warn("创建文件目录失败, 路径: {}", imgFilePath);
            return false;
        }

        // Base64解码
        byte[] b = Base64.decodeBase64(imgBase64);
        try(
            OutputStream out = new FileOutputStream(imgFilePath);
        ){
            out.write(b);
            out.flush();
            return true;
        } catch (Exception e) {
            log.warn("保存图片失败", e);
        }
        return false;
    }


    /**
     * 图片逆时针旋转90度
     * @param inputBytes 源文件字节数组
     * @param targetPath 输出路径
     */
    public static void leftRotate90(byte[] inputBytes, String targetPath) {
        // 图片类型
        try(
            InputStream inputStream = new ByteArrayInputStream(inputBytes);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        ) {
            BufferedImage originalImg = ImageIO.read(bufferedInputStream);
            leftRotate90(originalImg, targetPath);
        } catch (IOException e) {
            log.error("图片逆时针旋转90度失败", e);
            throw new RuntimeException("图片逆时针旋转90度失败");
        }
    }

    /**
     * 图片逆时针旋转90度
     * @param sourcePath 源文件路径
     * @param targetPath 输出文件路径
     */
    public static void leftRotate90(String sourcePath, String targetPath) {
        try {
            BufferedImage originalImg = ImageIO.read(new File(sourcePath));
            leftRotate90(originalImg, targetPath);
        } catch (IOException e) {
            log.error("图片逆时针旋转90度失败", e);
            throw new RuntimeException("图片逆时针旋转90度失败");
        }
    }

    /**
     * 图片逆时针旋转90度
     * @param originalImg 源文件图形对象
     * @param targetPath 输出文件路径
     */
    public static void leftRotate90(BufferedImage originalImg, String targetPath) {
        try {
            int width = originalImg.getWidth();
            int height = originalImg.getHeight();
            BufferedImage outBufferedImg = new BufferedImage(height, width, originalImg.getType());
            for (int i = 0; i < width; i++) {
                for (int j = 0; j < height; j++) {
                    outBufferedImg.setRGB(j, width - i - 1, originalImg.getRGB(i, j));
                }
            }
            ImageIO.write(outBufferedImg, "png", new File(targetPath));
        } catch (IOException e) {
            log.error("图片逆时针旋转90度失败", e);
            throw new RuntimeException("图片逆时针旋转90度失败");
        }
    }
}

码字不易,于你有利,勿忘点赞

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值