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 268cc43..21e4d26 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 @@ -1,20 +1,20 @@ package com.schisandra.auth.application.controller; -import cn.dev33.satoken.secure.SaSecureUtil; import cn.hutool.core.lang.UUID; -import cn.hutool.core.lang.generator.UUIDGenerator; import com.schisandra.auth.application.dto.SchisandraCaptchaDTO; 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 org.springframework.beans.factory.annotation.Value; +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; @@ -48,12 +48,12 @@ public class ReactRotateCaptchaController { try { SecureRandom random = new SecureRandom(); double randomNumber = random.nextInt(280) + 40; - InputStream inputStream = ReactRotateCaptchaController.class.getClassLoader().getResourceAsStream("image/test1.jpg"); + InputStream inputStream = ReactRotateCaptchaController.class.getClassLoader().getResourceAsStream("image/cropped_image.png"); 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, Color.black); + BufferedImage image1 = rotateImageUtils.rotateImage(image, randomNumber); String img = RotateImageUtils.BufferedImageToBase64(image1); map.put("token", token); map.put("str", img); @@ -92,5 +92,4 @@ public class ReactRotateCaptchaController { return CaptchaResult.fail(); } } - } 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/Test.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/Test.java new file mode 100644 index 0000000..984710f --- /dev/null +++ b/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-application/schisandra-cloud-storage-auth-application-controller/src/main/java/com/schisandra/auth/application/controller/Test.java @@ -0,0 +1,46 @@ +package com.schisandra.auth.application.controller; + +import com.schisandra.auth.common.utils.RotateImageUtils; +import net.coobird.thumbnailator.Thumbnails; + +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.IOException; +import java.io.InputStream; +import java.security.SecureRandom; + +/** + * @Classname Test + * @BelongsProject: schisandra-cloud-storage + * @BelongsPackage: com.schisandra.auth.application.controller + * @Author: landaiqing + * @CreateTime: 2024-06-06 12:07 + * @Description: TODO + * @Version: 1.0 + */ +public class Test { + public static void main(String[] args){ + InputStream inputStream = ReactRotateCaptchaController.class.getClassLoader().getResourceAsStream("image/13000.png"); + SecureRandom random = new SecureRandom(); + double randomNumber = random.nextInt(280) + 40; + BufferedImage bufferedImage = null; + try { + bufferedImage = Thumbnails.of(inputStream).size(1280,1024).rotate(randomNumber).asBufferedImage(); + } catch (IOException e) { + throw new RuntimeException(e); + } + + BufferedImage newCompleteImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), + BufferedImage.TYPE_INT_ARGB); + newCompleteImage.createGraphics().drawImage(bufferedImage, 0, 0, null); + String img = null; + try { + img = RotateImageUtils.BufferedImageToBase64(newCompleteImage); + } catch (IOException e) { + throw new RuntimeException(e); + } + System.out.println(img); + + } + +} 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 new file mode 100644 index 0000000..f019f5c Binary files /dev/null and b/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-application/schisandra-cloud-storage-auth-application-controller/src/main/resources/image/13000.png 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/cropped_image.png new file mode 100644 index 0000000..b55fb40 Binary files /dev/null and b/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-application/schisandra-cloud-storage-auth-application-controller/src/main/resources/image/cropped_image.png differ diff --git a/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-common/pom.xml b/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-common/pom.xml index acbc017..99d758a 100644 --- a/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-common/pom.xml +++ b/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-common/pom.xml @@ -121,6 +121,13 @@ 1.16.6 + + + net.coobird + thumbnailator + 0.4.20 + + diff --git a/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-common/src/main/java/com/schisandra/auth/common/utils/CopeImageUtil.java b/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-common/src/main/java/com/schisandra/auth/common/utils/CopeImageUtil.java new file mode 100644 index 0000000..8b62e18 --- /dev/null +++ b/schisandra-cloud-storage-auth/schisandra-cloud-storage-auth-common/src/main/java/com/schisandra/auth/common/utils/CopeImageUtil.java @@ -0,0 +1,91 @@ +package com.schisandra.auth.common.utils; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.geom.Ellipse2D; +import java.awt.image.BufferedImage; +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.net.URL; + +/** + * @Author: EDZ + * @Description: ${description} + * @Date: 2019/5/9 13:05 + * @Version: 1.0 + */ +public class CopeImageUtil { + public static BufferedImage cutHeadImages(String headUrl) { + BufferedImage avatarImage = null; + try { + avatarImage = ImageIO.read(new URL(headUrl)); + avatarImage = scaleByPercentage(avatarImage, avatarImage.getWidth(), avatarImage.getWidth()); + int width = avatarImage.getWidth(); + // 透明底的图片 + BufferedImage formatAvatarImage = new BufferedImage(width, width, BufferedImage.TYPE_4BYTE_ABGR); + Graphics2D graphics = formatAvatarImage.createGraphics(); + //把图片切成一个园 + graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + //留一个像素的空白区域,这个很重要,画圆的时候把这个覆盖 + int border = 1; + //图片是一个圆型 + Ellipse2D.Double shape = new Ellipse2D.Double(border, border, width - border * 2, width - border * 2); + //需要保留的区域 + graphics.setClip(shape); + graphics.drawImage(avatarImage, border, border, width - border * 2, width - border * 2, null); + graphics.dispose(); + //在圆图外面再画一个圆 + //新创建一个graphics,这样画的圆不会有锯齿 + graphics = formatAvatarImage.createGraphics(); + graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + int border1 = 3; + //画笔是4.5个像素,BasicStroke的使用可以查看下面的参考文档 + //使画笔时基本会像外延伸一定像素,具体可以自己使用的时候测试 + Stroke s = new BasicStroke(5F, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND); + graphics.setStroke(s); + graphics.setColor(Color.WHITE); + graphics.drawOval(border1, border1, width - border1 * 2, width - border1 * 2); + graphics.dispose(); + OutputStream os = new FileOutputStream("E:\\Desktop\\13000.png");//发布项目时,如:Tomcat 他会在服务器本地tomcat webapps文件下创建此文件名 + ImageIO.write(formatAvatarImage, "PNG", os); + return formatAvatarImage; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + /** + * 缩小Image,此方法返回源图像按给定宽度、高度限制下缩放后的图像 + * + * @param inputImage :压缩后宽度 + * :压缩后高度 + * @throws java.io.IOException return + */ + public static BufferedImage scaleByPercentage(BufferedImage inputImage, int newWidth, int newHeight) { + // 获取原始图像透明度类型 + try { + int type = inputImage.getColorModel().getTransparency(); + int width = inputImage.getWidth(); + int height = inputImage.getHeight(); + // 开启抗锯齿 + RenderingHints renderingHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + // 使用高质量压缩 + renderingHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); + BufferedImage img = new BufferedImage(newWidth, newHeight, type); + Graphics2D graphics2d = img.createGraphics(); + graphics2d.setRenderingHints(renderingHints); + graphics2d.drawImage(inputImage, 0, 0, newWidth, newHeight, 0, 0, width, height, null); + graphics2d.dispose(); + return img; + + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + public static void main(String[] args) throws Exception { + cutHeadImages("https://mjmall.oss-cn-shanghai.aliyuncs.com/18/1/merchantIcon.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 5bf47e6..deb9680 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 @@ -76,7 +76,7 @@ public class RotateImageUtils { * @author zlg * @date: 2024/5/9 13:14 */ - public BufferedImage rotateImage(BufferedImage image, double theta, Color backgroundColor) { + public BufferedImage rotateImage(BufferedImage image, double theta) { int width = image.getWidth(); int height = image.getHeight(); double angle = theta * Math.PI / 180; // 度转弧度