1、图片工具类
package org.springblade.modules.api.utils;import org.springframework.mock.web.MockMultipartFile;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;public final class FileImageUtil {public static int color_range = 120;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 MultipartFile koutTu(@RequestParam MultipartFile file) {BufferedImage image = getBufferedImage(file);int height = image.getHeight();int width = image.getWidth();ImageIcon imageIcon = new ImageIcon(image);BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);Graphics2D g2D = (Graphics2D) bufferedImage.getGraphics(); g2D.drawImage(imageIcon.getImage(), 0, 0, null); int alpha = 0; for (int y = bufferedImage.getMinY(); y < bufferedImage.getHeight(); y++) {for (int x = bufferedImage.getMinX(); x < bufferedImage.getWidth(); x++) {int rgb = bufferedImage.getRGB(x, y);if (colorInRange(rgb)) {alpha = 0;} else {alpha = 255;}rgb = (alpha << 24) | (rgb & 0x00ffffff);bufferedImage.setRGB(x, y, rgb);}}g2D.drawImage(bufferedImage, 0, 0, null);MultipartFile multipartFile = fileCase(bufferedImage);return multipartFile;}public static boolean colorInRange(int color) {int red = (color & 0xff0000) >> 16;int green = (color & 0x00ff00) >> 8;int blue = (color & 0x0000ff);if (red >= color_range && green >= color_range && blue >= color_range) {return true;}return false;}public static BufferedImage getBufferedImage(@RequestParam MultipartFile file) {BufferedImage image = null;try {image = ImageIO.read(file.getInputStream());} catch (IOException e) {System.out.println("读取图片文件出错!" + e.getMessage());}return image;}public static MultipartFile fileCase(BufferedImage image) {MultipartFile multipartFile = null;try {ByteArrayOutputStream os = new ByteArrayOutputStream();ImageIO.write(image, "png", os);InputStream input = new ByteArrayInputStream(os.toByteArray());multipartFile = new MockMultipartFile("file", "file.png", "text/plain", input);} catch (IOException e) {e.printStackTrace();}return multipartFile;}}
2、接口
@PostMapping("kouTu")public R<Kv> kouTu(@RequestParam MultipartFile file) {MultipartFile multipartFile = FileImageUtil.koutTu(file);LocalFile localFile = getFile(multipartFile);localFile.transfer();localFile.setUploadVirtualPath(localFile.getUploadVirtualPath().replace("//", "/"));String url = localFile.getDomain() + localFile.getUploadVirtualPath();Kv kv = Kv.create().set("url", url);return R.data(kv);}