diff --git a/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-application/schisandra-cloud-storage-auth-application-controller/src/main/java/com/schisandra/auth/application/controller/ReactRotateCaptchaController.java b/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-application/schisandra-cloud-storage-auth-application-controller/src/main/java/com/schisandra/auth/application/controller/ReactRotateCaptchaController.java index 21e4d26..6d237aa 100644 --- a/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-application/schisandra-cloud-storage-auth-application-controller/src/main/java/com/schisandra/auth/application/controller/ReactRotateCaptchaController.java +++ b/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-application/schisandra-cloud-storage-auth-application-controller/src/main/java/com/schisandra/auth/application/controller/ReactRotateCaptchaController.java @@ -6,15 +6,11 @@ import com.schisandra.auth.common.entity.CaptchaResult; import com.schisandra.auth.common.redis.RedisUtil; import com.schisandra.auth.common.utils.RotateImageUtils; import lombok.extern.slf4j.Slf4j; -import net.coobird.thumbnailator.Thumbnailator; -import net.coobird.thumbnailator.Thumbnails; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.imageio.ImageIO; -import java.awt.*; import java.awt.image.BufferedImage; -import java.io.IOException; import java.io.InputStream; import java.security.SecureRandom; import java.util.HashMap; @@ -42,19 +38,20 @@ public class ReactRotateCaptchaController { */ @GetMapping("get") public CaptchaResult get() throws Exception { - RotateImageUtils rotateImageUtils = new RotateImageUtils(); CompletableFuture futurePrice = CompletableFuture.supplyAsync(() -> { HashMap map = new HashMap<>(); try { SecureRandom random = new SecureRandom(); double randomNumber = random.nextInt(280) + 40; - InputStream inputStream = ReactRotateCaptchaController.class.getClassLoader().getResourceAsStream("image/cropped_image.png"); + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("image/test2.png"); + assert inputStream != null; BufferedImage image = ImageIO.read(inputStream); String token = UUID.randomUUID().toString(); String prefix = redisUtil.buildKey(authRotateCaptchaPrefix, token); - redisUtil.setNx(prefix, String.valueOf(randomNumber), 60L, TimeUnit.SECONDS); - BufferedImage image1 = rotateImageUtils.rotateImage(image, randomNumber); - String img = RotateImageUtils.BufferedImageToBase64(image1); + redisUtil.setNx(prefix, String.valueOf(randomNumber), 30L, TimeUnit.SECONDS); + BufferedImage bufferedImage = RotateImageUtils.rotateImage(image, randomNumber); + BufferedImage newImage = RotateImageUtils.writeCyclePic(bufferedImage); + String img = RotateImageUtils.bufferedImageToBase64(newImage); map.put("token", token); map.put("str", img); } catch (Exception e) { diff --git a/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-application/schisandra-cloud-storage-auth-application-controller/src/main/resources/image/13000.png b/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-application/schisandra-cloud-storage-auth-application-controller/src/main/resources/image/13000.png deleted file mode 100644 index f019f5c..0000000 Binary files a/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-application/schisandra-cloud-storage-auth-application-controller/src/main/resources/image/13000.png and /dev/null differ diff --git a/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-application/schisandra-cloud-storage-auth-application-controller/src/main/resources/image/test1.jpg b/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-application/schisandra-cloud-storage-auth-application-controller/src/main/resources/image/test1.jpg deleted file mode 100644 index a4498f7..0000000 Binary files a/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-application/schisandra-cloud-storage-auth-application-controller/src/main/resources/image/test1.jpg and /dev/null differ diff --git a/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-application/schisandra-cloud-storage-auth-application-controller/src/main/resources/image/cropped_image.png b/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-application/schisandra-cloud-storage-auth-application-controller/src/main/resources/image/test2.png similarity index 100% rename from schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-application/schisandra-cloud-storage-auth-application-controller/src/main/resources/image/cropped_image.png rename to schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-application/schisandra-cloud-storage-auth-application-controller/src/main/resources/image/test2.png diff --git a/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-common/src/main/java/com/schisandra/auth/common/utils/RotateImageUtils.java b/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-common/src/main/java/com/schisandra/auth/common/utils/RotateImageUtils.java index a6a67d9..6865d1e 100644 --- a/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-common/src/main/java/com/schisandra/auth/common/utils/RotateImageUtils.java +++ b/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-common/src/main/java/com/schisandra/auth/common/utils/RotateImageUtils.java @@ -1,68 +1,17 @@ package com.schisandra.auth.common.utils; -import java.util.Base64; -import java.util.Base64.Encoder; -import java.util.Base64.Decoder; +import net.coobird.thumbnailator.Thumbnails; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; -import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.util.Arrays; +import java.util.Base64; public class RotateImageUtils { - public static Color bgColor = new Color(255, 255, 255); - - - /** - * @description: 压缩图片并将图片转换成base64 - * @param: [bufferedImage] - * @return: java.lang.String - * @author zlg - * @date: 2024/5/9 13:13 - */ - public static String BufferedImageToBase64(BufferedImage bufferedImage) throws IOException { - int targetWidth = 400; - int targetHeight = 400; - // 获取原始图片的尺寸 - int originalWidth = bufferedImage.getWidth(); - int originalHeight = bufferedImage.getHeight(); - // 创建一个新的 BufferedImage,用于存放处理后的图片 - BufferedImage outputImage = new BufferedImage(targetWidth, targetHeight, bufferedImage.getType()); - // 创建一个 Graphics2D 对象,用于绘制新图片 - Graphics2D graphics = outputImage.createGraphics(); - graphics.dispose(); - ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流 - ImageIO.write(outputImage, "png", baos);//写入流中 - byte[] bytes = baos.toByteArray();//转换成字节 - Encoder encoder = Base64.getEncoder(); - String png_base64 = encoder.encodeToString(bytes);//转换成base64串 - png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n - return "data:image/jpg;base64," + png_base64; - - } - - /** - * @description: 将base64转换成图片 - * @param: [base64] - * @return: java.awt.image.BufferedImage - * @author zlg - * @date: 2024/5/9 13:14 - */ - public static BufferedImage base64ToBufferedImage(String base64) { - Decoder decoder = Base64.getDecoder(); - try { - byte[] bytes1 = decoder.decode(base64); - ByteArrayInputStream bais = new ByteArrayInputStream(bytes1); - return ImageIO.read(bais); - } catch (IOException e) { - e.printStackTrace(); - } - return null; - } /** * @description: 获取旋转后的图片 @@ -71,39 +20,77 @@ public class RotateImageUtils { * @author zlg * @date: 2024/5/9 13:14 */ - public BufferedImage rotateImage(BufferedImage image, double theta) throws IOException { - BufferedImage originalImage = image; - int width = originalImage.getWidth(); - int height = originalImage.getHeight(); - // 创建新的BufferedImage对象,设置类型为ARGB_8888 - BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); - // 使用Graphics2D对象将原始图片绘制到新的BufferedImage对象上,同时设置背景色为透明色 - Graphics2D g2d = newImage.createGraphics(); - g2d.setBackground(new Color(0, 0, 0, 0)); // 设置背景色为透明色 - g2d.clearRect(0, 0, width, height); // 清除画布 - g2d.drawImage(originalImage, 0, 0,null); // 绘制原始图片 - g2d.dispose(); - // 对新的BufferedImage对象进行旋转操作 - double angle = Math.toRadians(theta); - int newWidth = (int) (height * Math.cos(angle) + width * Math.sin(angle)); - int newHeight = (int) (height * Math.sin(angle) + width * Math.cos(angle)); - BufferedImage rotatedImage = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB); - Graphics2D g2dRotated = rotatedImage.createGraphics(); - g2dRotated.translate((newWidth - width) / 2, (newHeight - height) / 2); - g2dRotated.rotate(angle, width / 2, height / 2); - g2dRotated.drawImage(newImage, 0, 0,null); - g2dRotated.dispose(); - int newSize = 160; // 新的大小为500像素 - BufferedImage resizedImage = new BufferedImage(newSize, newSize, BufferedImage.TYPE_INT_ARGB); - Graphics2D g2dResized = resizedImage.createGraphics(); - g2dResized.drawImage(rotatedImage, 0, 0, newSize, newSize, null); - g2dResized.dispose(); - // 将调整大小后的图像保存到文件中 - return resizedImage; + public static BufferedImage rotateImageByThumbnails(BufferedImage image, double theta) throws IOException { + + BufferedImage bufferedImage = Thumbnails.of(image).size(200, 200).rotate(theta).asBufferedImage(); + BufferedImage newCompleteImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), + BufferedImage.TYPE_INT_ARGB); + newCompleteImage.createGraphics().drawImage(bufferedImage, 0, 0,null); + return newCompleteImage; + } + + /** + * BufferedImage转base64 + * + * @param bufferedImage + * @return + */ + public static String bufferedImageToBase64(BufferedImage bufferedImage) { + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + try { + // 设置图片格式 + ImageIO.write(bufferedImage, "png", stream); + } catch (IOException e) { + e.printStackTrace(); + } + byte[] bytes = Base64.getEncoder().encode(stream.toByteArray()); + String base64 = new String(bytes); + return "data:image/png;base64," + base64; + } + + + /** + * @description: 获取旋转后的图片 + * @param: [image, theta, backgroundColor] + * @return: java.awt.image.BufferedImage + * @author zlg + * @date: 2024/5/9 13:14 + */ + public static BufferedImage rotateImage(BufferedImage image, double theta) throws IOException { + int width = image.getWidth(); + int height = image.getHeight(); + double angle = theta * Math.PI / 180; // 度转弧度 + 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_RGB); + 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) Math.round(radius * Math.cos(angle1 - angle)); + y = (int) Math.round(radius * Math.sin(angle1 - angle)); + if (x < (width / 2) & x > -(width / 2) & y < (height / 2) & y > -(height / 2)) { + int rgb = image.getRGB((int) Math.round(x + width / 2), (int) Math.round(height / 2 - y)); + resultImage.setRGB(i, j, rgb); + } else { + resultImage.setRGB(i, j, -1); + } + } + } + return resultImage; } // 获取四个角点旋转后Y方向坐标 - private double[] getY(int i, int j, double angle) { + 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); @@ -116,7 +103,7 @@ public class RotateImageUtils { } // 获取四个角点旋转后X方向坐标 - private double[] getX(int i, int j, double angle) { + 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); @@ -135,17 +122,17 @@ public class RotateImageUtils { * @author zlg * @date: 2024/5/9 13:14 */ - public BufferedImage writeCyclePic(BufferedImage image) { - BufferedImage newImage = new BufferedImage(350, 350, BufferedImage.TYPE_INT_BGR); + public static BufferedImage writeCyclePic(BufferedImage image) { + BufferedImage newImage = new BufferedImage(800, 800, BufferedImage.TYPE_INT_BGR); try { int width = image.getWidth(); int heigth = image.getHeight(); double x0 = width / 2; double y0 = heigth / 2; - int woffset = (width - 350) / 2; - int hoffset = (heigth - 350) / 2; - for (int i = woffset; i < 350 + woffset; i++) { - for (int j = hoffset; j < 350 + hoffset; j++) { + int woffset = (width - 800) / 2; + int hoffset = (heigth - 800) / 2; + for (int i = woffset; i < 800 + woffset; i++) { + for (int j = hoffset; j < 800 + hoffset; j++) { double r = Math.sqrt(Math.pow(Math.abs(i - x0), 2.0) + Math.pow(Math.abs(j - y0), 2.0)); if (r > (x0 - woffset)) { newImage.setRGB(i - woffset, j - hoffset, -1); @@ -163,6 +150,4 @@ public class RotateImageUtils { } - - }