https://imgtobase64.d777.com/
工具类
public final class Base64ImageUtil {
private static BufferedImage rotate(Image src, int angel) {
int src_width = src.getWidth(null);
int src_height = src.getHeight(null);
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();
res = g2.getDeviceConfiguration().createCompatibleImage(rect_des.width, rect_des.height, Transparency.TRANSLUCENT);
g2.dispose();
g2 = res.createGraphics();
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.setColor(new Color(255, 0, 0));
g2.setStroke(new BasicStroke(1));
g2.drawImage(src, null, null);
g2.dispose();
return res;
}
public static Rectangle CalcRotatedSize(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 Rectangle(new Dimension(des_width, des_height));
}
public static String rotateImage(String base64FileStr, int rotate) throws IOException {
if (StringUtils.isEmpty(base64FileStr)) {
return "";
}
Base64.Decoder decoder = Base64.getDecoder();
final byte[] byt = decoder.decode(base64FileStr);
for (int i = 0, len = byt.length; i < len; ++i) {
if (byt[i] < 0) {
byt[i] += 256;
}
}
InputStream input = new ByteArrayInputStream(byt);
ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedImage src = ImageIO.read(input);
BufferedImage des = rotate(src, rotate);
ImageIO.write(des, "png", out);
final Base64.Encoder encoder = Base64.getEncoder();
final byte[] bytes = out.toByteArray();
return encoder.encodeToString(bytes);
}
public static void main(String[] args) throws IOException {
String base64FileStr = "iVBORw0KGgoAAAANSUhEUgAAAGQAAAAsCAYAAACT6R1VAAAAAXNSR0IArs4c6QAABftJREFUeF7tmudRLDsQhbUJABkACWASwCUAJIBJAJcAJgFMALgEcP8xEWASACLABLD76lNVb/UKSaOZJ6h99aSqW/cyI7W6zzndLc2l1el0OqaMvkGgVQjpGy6sI4WQ/uKjENJnfBRCCiH9hkCf+VN6SCGkzxDoM3f+NEO48rRarVoQNFnDBk3X1XKu5uQUnyoJqTLie7+zs2N2d3fN9PS0ub+/T3KbebOzs5awdrudtMY3qcrfpoZz+Ve1fyUhVQZ87/8NIaLuJvv+5hohJId/MdEUQhJZzEFISvZmI0RvRobs7e2Zqamp2iXLVWBKEImY2mlN7eUgJMXPbITozZqUrLu7OzM3N9cFLcX5v5zzV/7VIuTt7c28v79bHIaHh83IyIgXkxRCXKVqBdLU657GcATf8JExODhoJiYmopx9fn6a5+fnrghmZmaC8+v4x1z8T/HB3TCJEBzf3Nw0p6enPesJ+OLi4gcxMUKenp7saQqbrF1YWLA2QyUB4r6+vuwa1q6vr5uDg4MeP3jP87Ozs57niIa5soe8DMUTmh/zT5dB8KFUv76+dv0YGhoyJycnP3wIMV9JCMGiHMDA4ZWVFWvr8fHRXF9fGzbEAf6WESIEICYnJ62K9/f3zcbGRndNrEYD6NXVlZmfnzeXl5c9sWiCUST+4QvPWTM2NtbNAhbig5Cr57P/w8ODte36VkUI74nl8PDQrkcc+MBekASGkCLYxdK21W63O7HyIGAsLy9btWngxYnt7W0DCVWEQCxBY8vNthAhQu74+LjNIr2/Bpf32AZkGTJfly5Ivbm5sUS59vBpdXXVLqdn6BIWEwzEgxN7M0/vhzCwA8aucH3ERDMEYyiazKDW6mBFbf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BYkYbhZCMYOYwVQjJgWJGG4WQjGDmMFUIyYFiRhuFkIxg5jBVCMmBYkYbhZCMYOYwVQjJgWJGG4WQjGDmMPUPg+N0yLUhwxIAAAAASUVORK5CYII=";
final String image = rotateImage(base64FileStr, 90);
System.out.println("data:image/png;base64,"+image);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
旋转前
旋转后