测试一
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 {
String base64Image = "你的Base64编码的图像";
BufferedImage bufferedImage = decodeToImage(base64Image);
String bufferedImage1 = rotateImage(bufferedImage, 180);
}
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;
}
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 {
public static BufferedImage decodeBase64Image(String base64Image) throws IOException {
byte[] imageBytes = Base64.getDecoder().decode(base64Image);
ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);
return ImageIO.read(bis);
}
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;
}
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;
}
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);
BufferedImage rotatedImage = rotateImage(originalImage, 90);
BufferedImage croppedImage = cropBlackBorders(rotatedImage);
String base64CroppedImage = encodeImageToBase64(croppedImage);
} catch (IOException e) {
e.printStackTrace();
}
}
}