java处理旋转图片后出现黑边,包含BufferedImage转为base64,base64转为BufferedImage

测试一

package pro.agris.branch.querymachine.service.impl;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;

/**
 * 旋转图片后去除黑边
 */
public class ImageRotatorUtil {

    public static String rotateImage(BufferedImage originalImage, double degree) throws IOException {
        int width = originalImage.getWidth();
        int height = originalImage.getHeight();

        // 计算旋转后的新尺寸
        double sin = Math.abs(Math.sin(Math.toRadians(degree))),
                cos = Math.abs(Math.cos(Math.toRadians(degree)));
        int newWidth = (int) Math.floor(width * cos + height * sin),
                newHeight = (int) Math.floor(height * cos + width * sin);

        // 创建一个新的空白图像,大小为旋转后的尺寸
        BufferedImage rotatedImage = new BufferedImage(newWidth, newHeight, originalImage.getType());
        Graphics2D g2d = rotatedImage.createGraphics();

        // 设置旋转的中心点为新图像的中心
        AffineTransform at = new AffineTransform();
        at.translate((newWidth - width) / 2, (newHeight - height) / 2);
        at.rotate(Math.toRadians(degree), width / 2, height / 2);

        // 执行旋转
        AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
        rotatedImage = op.filter(originalImage, rotatedImage);

        // 裁剪空白边框
        rotatedImage = cropToVisible(rotatedImage);

        g2d.dispose();

        return encodeToString(rotatedImage, "png");
    }

    private static BufferedImage cropToVisible(BufferedImage image) {
        int x, y, w, h;
        x = y = Integer.MAX_VALUE;
        w = h = 0;

        for (int i = 0; i < image.getWidth(); i++) {
            for (int j = 0; j < image.getHeight(); j++) {
                if (image.getRGB(i, j) != Color.BLACK.getRGB()) {
                    if (i < x) x = i;
                    if (j < y) y = j;
                    if (i > w) w = i;
                    if (j > h) h = j;
                }
            }
        }

        w = w - x + 1;
        h = h - y + 1;

        return image.getSubimage(x, y, w, h);
    }

    public static void main(String[] args) throws IOException {
        // 假设你已经有了一个Base64编码的图像,并将其解码为字节数组
        String base64Image = "你的Base64编码的图像";
        BufferedImage bufferedImage = decodeToImage(base64Image);
        String bufferedImage1 = rotateImage(bufferedImage, 180);


        // 旋转
    }

    /**
     * base64转为BufferedImage
     *
     * @param base64Image
     * @return
     * @throws IOException
     */
    public static BufferedImage decodeToImage(String base64Image) throws IOException {
        byte[] imageBytes = Base64.getDecoder().decode(base64Image);
        ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);
        BufferedImage image = ImageIO.read(bis);
        bis.close();
        return image;
    }

    /**
     * BufferedImage转为base64
     *
     * @param image
     * @param type
     * @return
     * @throws IOException
     */
    public static String encodeToString(BufferedImage image, String type) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, type, baos);
        byte[] imageBytes = baos.toByteArray();
        return Base64.getEncoder().encodeToString(imageBytes);
    }
}

测试二

package pro.agris.branch.querymachine.service.impl;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;

public class ImageRotator {

    // 将Base64字符串解码为BufferedImage
    public static BufferedImage decodeBase64Image(String base64Image) throws IOException {
        byte[] imageBytes = Base64.getDecoder().decode(base64Image);
        ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);
        return ImageIO.read(bis);
    }

    // 旋转BufferedImage
    public static BufferedImage rotateImage(BufferedImage image, double degrees) throws IOException {
        int w = image.getWidth();
        int h = image.getHeight();
        BufferedImage dimg = new BufferedImage(w, h, image.getType());
        Graphics2D g2d = (Graphics2D) dimg.getGraphics();
        g2d.translate((w - 1) / 2, (h - 1) / 2);
        g2d.rotate(Math.toRadians(degrees), w / 2, h / 2);
        g2d.drawRenderedImage(image, null);
        g2d.dispose();

        // 旋转后可能需要裁剪,但这里先不裁剪
        return dimg;
    }

    // 裁剪BufferedImage周围的黑边
    public static BufferedImage cropBlackBorders(BufferedImage image) {
        int left = Integer.MAX_VALUE, right = Integer.MIN_VALUE, top = Integer.MAX_VALUE, bottom = Integer.MIN_VALUE;

        for (int x = 0; x < image.getWidth(); x++) {
            for (int y = 0; y < image.getHeight(); y++) {
                int pixel = image.getRGB(x, y);
                if ((pixel & 0xFF000000) != 0xFF000000) { // 非黑色
                    if (x < left) left = x;
                    if (x > right) right = x;
                    if (y < top) top = y;
                    if (y > bottom) bottom = y;
                }
            }
        }

        int width = right - left + 1;
        int height = bottom - top + 1;

        if (width <= 0 || height <= 0) return null; // 没有非黑色像素

        BufferedImage croppedImage = image.getSubimage(left, top, width, height);
        return croppedImage;
    }

    // 将BufferedImage编码为Base64字符串
    public static String encodeImageToBase64(BufferedImage image) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, "png", baos);
        byte[] imageBytes = baos.toByteArray();
        return Base64.getEncoder().encodeToString(imageBytes);
    }

    public static void main(String[] args) {
        try {
            String base64Image = "你的Base64编码的图像";
            BufferedImage originalImage = decodeBase64Image(base64Image);

            // 旋转图像(例如,旋转90度)
            BufferedImage rotatedImage = rotateImage(originalImage, 90);

            // 裁剪旋转后的图像
            BufferedImage croppedImage = cropBlackBorders(rotatedImage);

            // 编码为Base64字符串(如果需要)
            String base64CroppedImage = encodeImageToBase64(croppedImage);

            // 在这里处理base64CroppedImage...

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值