From f52c9ecaa576145ecb097e2335b5f12acddec93b Mon Sep 17 00:00:00 2001
From: jiangping <jp@doumee.com>
Date: 星期四, 18 七月 2024 15:04:21 +0800
Subject: [PATCH] 提交
---
server/service/src/main/java/com/doumee/core/utils/GeneratePicUtil.java | 264 ++++++++++++++++++++++++++++++++++++++++++++++------
1 files changed, 233 insertions(+), 31 deletions(-)
diff --git a/server/service/src/main/java/com/doumee/core/utils/GeneratePicUtil.java b/server/service/src/main/java/com/doumee/core/utils/GeneratePicUtil.java
index c467fde..7d16eae 100644
--- a/server/service/src/main/java/com/doumee/core/utils/GeneratePicUtil.java
+++ b/server/service/src/main/java/com/doumee/core/utils/GeneratePicUtil.java
@@ -5,6 +5,7 @@
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.lang3.StringUtils;
+import org.checkerframework.checker.units.qual.C;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
@@ -13,6 +14,7 @@
import javax.imageio.stream.ImageOutputStream;
import java.awt.*;
import java.awt.geom.Ellipse2D;
+import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.math.BigDecimal;
@@ -45,7 +47,7 @@
}
private static final int width = 750;
- private static final int height = 1140;
+ private static final int height = 1194;
/**
* 鐢熸垚鍒嗕韩鍥剧墖
@@ -268,83 +270,283 @@
}
return resMatrix;
}
- public static InputStream generateUserCardImg(String imgurl, String qrcode, String name,String type, String phone, String shopaddr, String shopname,String phoneIcon,String addrIcon) {
+ public static InputStream generateUserCardImg(String imgurl, String qrcode, String name,String type, String phone
+ , String shopaddr, String shopname
+ ,String phoneIcon
+ ,String addrIcon
+ ,String shopIcon
+ ,String logoIcon) {
- BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
+ BufferedImage bi = new BufferedImage(750, 1194, BufferedImage.TYPE_INT_RGB);
//寰楀埌瀹冪殑缁樺埗鐜(杩欏紶鍥剧墖鐨勭瑪)
Graphics2D g2 = (Graphics2D) bi.getGraphics();
- g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
- g2.fillRect(0, 0, width, height);
+ g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+ g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
+ g2.fillRect(0, 0, 750, 1194);
//鑳屾櫙鍥剧墖
BufferedImage img1 =getImgIO(imgurl);
if(img1!=null){
+ int w = img1.getWidth();
+ int h = img1.getHeight();
+ int h1 = (int)((double)w *700/750);
+ int w1 = (int)((double)h *750/700);
+ if(h>h1){
+ h = h1;
+ }
+ if(w>w1){
+ w = w1;
+ }
+ img1 = img1.getSubimage(0,0,w,h);
g2.drawImage(img1, 0, 0, 750, 700, null);
}else{
g2.setColor(new Color(102, 102, 102, 1));
g2.drawRect( 0, 0, 750, 700);
}
- //鍟嗗搧鍚嶇О
+ //鍚嶇О
if(name.length()>5){
name = name.substring(0,5)+"...";
}
g2.setColor(Color.black);
g2.setFont(new Font("榛戜綋", Font.PLAIN, 40));
- g2.drawString(name, 40, 990-242);
+ g2.drawString(name, 40, 742+th);
- int w = 40+ name.length()*40;
- g2.setColor(Color.GRAY);
+ int w = 60+ name.length()* 40;
+ g2.setColor(new Color(176, 135, 113));
+ g2.drawRoundRect(w,744-10,85,50,15,15);//鐢讳竴涓渾瑙掔煩褰�
+
+ g2.setColor(new Color(176, 135, 113));
g2.setFont(new Font("榛戜綋", Font.PLAIN, 30));
- g2.drawString(type, w, 990-242);
+ g2.drawString(type, w+12, 741+th);
+
if(StringUtils.isNotBlank(qrcode)){
//涓汉浜岀淮鐮�
BufferedImage img2 = getImgIO(qrcode);
if(img2!=null){
- g2.drawImage(img2, 522, 982-242, 188, 188, null);
+ g2.drawImage(img2, 522, 740, 188, 188, null);
}
-
- g2.setColor(Color.GRAY);
+ g2.setColor(new Color(176, 135, 113));
g2.setFont(new Font("榛戜綋", Font.PLAIN, 24));
- g2.drawString("鎵爜娣诲姞濂藉弸", 544, 1186-242);
+ g2.drawString("鎵爜娣诲姞濂藉弸", 544, 944+th);
}
+
//鎵嬫満鍙�
BufferedImage img4 = getImgIO(phoneIcon);
if(img4!=null){
- g2.drawImage(img4, 40, 1072-28-242, 34, 36, null);
+ g2.drawImage(img4, 40, 830 , 34, 36, null);
}
g2.setColor(Color.GRAY);
g2.setFont(new Font("榛戜綋", Font.PLAIN, 30));
- g2.drawString(phone, 94, 1072-242);
+ g2.drawString(phone, 94, 828+th);
+ //闂ㄥ簵鍚嶇О
+ BufferedImage img5 = getImgIO(shopIcon);
+ if(img4!=null){
+ g2.drawImage(img5, 40, 904 , 34, 36, null);
+ }
+ if(shopname.length()>11){
+ shopname = shopname.substring(0,11)+"...";
+ }
+ g2.setColor(Color.GRAY);
+ g2.setFont(new Font("榛戜綋", Font.PLAIN, 30));
+ g2.drawString(shopname, 94,901+th);
+
BufferedImage img3 = getImgIO(addrIcon);
if(img3!=null){
- g2.drawImage(img3, 40, 1142-28-242, 34, 36, null);
+ g2.drawImage(img3, 40, 980, 34, 36, null);
}
- if(shopaddr.length()>12){
- shopaddr = shopaddr.substring(0,12)+"\n"+shopaddr.substring(12);
- }
- //鍦板潃
- g2.setColor(Color.GRAY);
- g2.setFont(new Font("榛戜綋", Font.PLAIN, 30));
- g2.drawString(shopaddr, 94, 1142-242);
-
+ dealDrawShopAddr(g2,94,973+th,shopaddr);
//搴曢儴
- if(shopname.length()>15){
- shopname = shopname.substring(0,15)+"...";
+
+ BufferedImage img6 = getImgIO(logoIcon);
+ if(img6!=null){
+ g2.drawImage(img6, 254, 1126+th-20, 242, 28, null);
}
- g2.setColor(Color.GRAY);
- g2.setFont(new Font("榛戜綋", Font.PLAIN, 30));
- g2.drawString(shopname, 180, 1300-242);
InputStream inputStream = bufferedImageToInputStream(bi);
return inputStream;
}
- private static BufferedImage getImgIO(String imgurl) {
+ private static void dealDrawShopAddr(Graphics2D g2, int x, int y, String shopaddr) {
+ int num =shopaddr.length() / 13;
+ for (int i = 0; i <= num; i++) {
+ String temp ="";
+ if(shopaddr.length() >= (i+1)*13){
+ temp = shopaddr.substring(i*13,(i+1)*13);
+ }else{
+ temp=shopaddr.substring( i*13);
+ }
+ //鍦板潃
+ g2.setColor(Color.GRAY);
+ g2.setFont(new Font("榛戜綋", Font.PLAIN, 30));
+ g2.drawString(temp, x, y+i*40);
+ }
+ }
+
+ public static BufferedImage getImgIO(String imgurl) {
try {
return ImageIO.read(new URL(imgurl));
}catch (Exception e){
}return null;
}
+ private static BufferedImage getImgIO(InputStream is) {
+ try {
+ return ImageIO.read(is);
+ }catch (Exception e){
+
+ }return null;
+ }
+
+ public static InputStream generateShareImg(String imgurl, InputStream mpCode, String title) {
+
+ BufferedImage bi = new BufferedImage(750, 976, BufferedImage.TYPE_INT_RGB);
+ //寰楀埌瀹冪殑缁樺埗鐜(杩欏紶鍥剧墖鐨勭瑪)
+ Graphics2D g2 = (Graphics2D) bi.getGraphics();
+ g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+ g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
+ g2.fillRect(0, 0, 750, 976);
+ //鑳屾櫙鍥剧墖
+ dealTitleCenter(g2,th,title);
+ if(StringUtils.isNotBlank(imgurl)){
+ BufferedImage img1 =getImgIO(imgurl);
+ if(img1!=null){
+ int w = img1.getWidth();
+ int h = img1.getHeight();
+ int h1 = (int)((double)w *502/670);
+ int w1 = (int)((double)h *670/502);
+ if(h>h1){
+ h = h1;
+ }
+ if(w>w1){
+ w = w1;
+ }
+ img1 = img1.getSubimage(0,0,w,h);
+ g2.drawImage(setRadius(img1,20,0,0), 40, 204, 670 ,502, null);
+
+ }
+ }
+
+// g2.setColor(Color.white);
+// g2.drawRoundRect(39,204, 672, 502, 1,1);//鐢讳竴涓渾瑙掔煩褰�
+
+ g2.setColor(Color.black);
+ g2.setFont(new Font("榛戜綋", Font.BOLD, 40));
+ g2.drawString("闀挎寜璇嗗埆灏忕▼搴�", 40,768+th);
+ g2.setColor(Color.gray);
+ g2.setFont(new Font("榛戜綋", Font.PLAIN, 32));
+ g2.drawString("鏌ョ湅鏇村绮惧僵鍐呭", 40,820+th);
+
+
+ BufferedImage img3 = getImgIO(mpCode);
+ if(img3!=null){
+ g2.drawImage(img3, 530, 736, 180, 180, null);
+ }
+ InputStream inputStream = bufferedImageToInputStream(bi);
+ return inputStream;
+ }
+
+ /**
+ * 鍥剧墖璁剧疆鍦嗚
+ * @param srcImage
+ * @return
+ * @throws
+ */
+ public static BufferedImage setRadius(BufferedImage srcImage, int radius, int border, int padding){
+ int width = srcImage.getWidth();
+ int height = srcImage.getHeight();
+ int canvasWidth = width + padding * 2;
+ int canvasHeight = height + padding * 2;
+
+ BufferedImage image = new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_ARGB);
+ Graphics2D gs = image.createGraphics();
+ gs.setComposite(AlphaComposite.Src);
+ gs.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+ gs.setColor(Color.WHITE);
+ gs.fill(new RoundRectangle2D.Float(0, 0, canvasWidth, canvasHeight, radius, radius));
+ gs.setComposite(AlphaComposite.SrcAtop);
+ gs.drawImage(setClip(srcImage, radius), padding, padding, null);
+ if(border !=0){
+ gs.setColor(Color.white);
+ gs.setStroke(new BasicStroke(border));
+ gs.drawRoundRect(padding, padding, canvasWidth - 2 * padding, canvasHeight - 2 * padding, radius, radius);
+ }
+ gs.dispose();
+ return image;
+ }
+
+
+ /**
+ * 鍥剧墖鍒囧渾瑙�
+ * @param srcImage
+ * @param radius
+ * @return
+ */
+ public static BufferedImage setClip(BufferedImage srcImage, int radius){
+ int width = srcImage.getWidth();
+ int height = srcImage.getHeight();
+ BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
+ Graphics2D gs = image.createGraphics();
+
+ gs.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+ gs.setClip(new RoundRectangle2D.Double(0, 0, width, height, radius, radius));
+ gs.drawImage(srcImage, 0, 0, null);
+ gs.dispose();
+ return image;
+ }
+
+ private static void dealTitleCenter(Graphics2D g2, int th, String title) {
+ String[] titles = title.split("&");
+ for (int i = 0; i < titles.length && i<2; i++) {
+ String name = titles[i];
+ //鍚嶇О
+ if(name.length()>=17){
+ name = name.substring(0,17);
+ }
+ Font font =new Font("榛戜綋", Font.BOLD, 38);
+ FontMetrics fontMetrics = g2.getFontMetrics(font);
+ int centerX = 750 / 2;
+ int textWidth = fontMetrics.stringWidth(name);
+ g2.setColor(Color.black);
+ g2.setFont(font);
+ g2.drawString(name, centerX - textWidth / 2, 60+th+i*55);
+ }
+ }
+ public static int th = 30;
+ public static InputStream generateShareWithUserImg(BufferedImage imgurl, InputStream mpCode, String header, String name) {
+ int w = imgurl.getWidth();
+ int h = imgurl.getHeight();
+ double rate = (double)w/750d;
+
+ //寰楀埌瀹冪殑缁樺埗鐜(杩欏紶鍥剧墖鐨勭瑪)
+ Graphics2D g2 = (Graphics2D) imgurl.getGraphics();
+ g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
+ g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
+ //鑳屾櫙鍥剧墖
+
+ BufferedImage img1 =getImgIO(header);
+ if(img1!=null){
+ g2.drawImage(setRadius(img1,img1.getWidth(),0,0), (int)(20*rate), (int)(h-68*rate), (int)(48*rate) ,(int)(48*rate), null);
+ }
+
+ Font font =new Font("榛戜綋", Font.PLAIN, (int)(28 * rate));
+ FontMetrics fontMetrics = g2.getFontMetrics(font);
+ int textWidth = fontMetrics.stringWidth(name);
+
+ g2.setColor(Color.white);
+ g2.setFont(font);
+ g2.drawString(name, (int) (80*rate),h-(int)((64-th)*rate));
+ g2.setColor(Color.white);
+ g2.setFont(new Font("榛戜綋", Font.PLAIN, (int)(24* rate)));
+ g2.drawString("鎵弿鐪嬫洿澶�", (int)(w -(textWidth+30*rate)),h-(int)(rate*(50-th)));
+
+ BufferedImage img3 = getImgIO(mpCode);
+ if(img3!=null){
+ g2.setColor(Color.white);
+ g2.fillOval((int)(w-130*rate), (int)(h-rate*150), (int)(100*rate) , (int)(100*rate));
+ g2.drawImage(img3, (int)(w-130*rate), (int)(h-rate*150), (int)(100*rate) , (int)(100*rate), null);
+ }
+ InputStream inputStream = bufferedImageToInputStream(imgurl);
+ return inputStream;
+ }
}
--
Gitblit v1.9.3