base64数据图片处理
1.spring boot post请求数据过大:
server:
tomcat:
max-http-form-post-size: -1
spring:
http:
multipart:
max-file-size: -1
max-request-size: -1
2.base64转png
/**
* base64字符串转化成图片
*
* @param imgData
* 图片编码
* @param imgFilePath
* 存放到本地路径
* @return
* @throws IOException
*/
@SuppressWarnings("finally")
public static boolean GenerateImage(String imgData, String imgFilePath) { // 对字节数组字符串进行Base64解码并生成图片
if (imgData == null) // 图像数据为空
return false;
BASE64Decoder decoder = new BASE64Decoder();
OutputStream out = null;
try {
out = new FileOutputStream(imgFilePath);
// Base64解码
byte[] b = decoder.decodeBuffer(imgData);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
out.write(b);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
out.flush();
out.close();
} catch (IOException e) {
}
return true;
}
}
3.图片旋转任意角度
/**
* 创建任意角度的旋转图像
* @param image
* @param theta
* @param backgroundColor
* @return
*/
public static BufferedImage rotateImage(BufferedImage image, double theta, Color backgroundColor) {
int width = image.getWidth();
int height = image.getHeight();
double angle = theta * Math.PI / 360; // 度转弧度
double[] xCoords = getX(width / 2, height / 2, angle);
double[] yCoords = getY(width / 2, height / 2, angle);
int WIDTH = (int) (xCoords[3] - xCoords[0]);
int HEIGHT = (int) (yCoords[3] - yCoords[0]);
BufferedImage resultImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < HEIGHT; j++) {
int x = i - WIDTH / 2;
int y = HEIGHT / 2 - j;
double radius = Math.sqrt(x * x + y * y);
double angle1;
if (y > 0) {
angle1 = Math.acos(x / radius);
} else {
angle1 = 2 * Math.PI - Math.acos(x / radius);
}
x = (int) (radius * Math.cos(angle1 - angle));
y = (int) (radius * Math.sin(angle1 - angle));
if (x < (width / 2) & x > -(width / 2) & y < (height / 2) & y > -(height / 2)) {
int rgb = image.getRGB(x + width / 2, height / 2 - y);
resultImage.setRGB(i, j, rgb);
}else {
int rgb = ((0 & 0xff) << 24) | ((backgroundColor.getRed() & 0xff) << 16) | ((backgroundColor.getGreen() & 0xff) << 8)
| ((backgroundColor.getBlue() & 0xff));
resultImage.setRGB(i, j, rgb);
}
}
}
return resultImage;
}
// 获取四个角点旋转后Y方向坐标
private static double[] getY(int i, int j, double angle) {
double results[] = new double[4];
double radius = Math.sqrt(i * i + j * j);
double angle1 = Math.asin(j / radius);
results[0] = radius * Math.sin(angle1 + angle);
results[1] = radius * Math.sin(Math.PI - angle1 + angle);
results[2] = -results[0];
results[3] = -results[1];
Arrays.sort(results);
return results;
}
// 获取四个角点旋转后X方向坐标
private static double[] getX(int i, int j, double angle) {
double results[] = new double[4];
double radius = Math.sqrt(i * i + j * j);
double angle1 = Math.acos(i / radius);
results[0] = radius * Math.cos(angle1 + angle);
results[1] = radius * Math.cos(Math.PI - angle1 + angle);
results[2] = -results[0];
results[3] = -results[1];
Arrays.sort(results);
return results;
}