liukangdong
2024-07-16 20b11236b4364034f52df294b9240776f539ede1
server/service/src/main/java/com/doumee/core/utils/GeneratePicUtil.java
@@ -4,6 +4,7 @@
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
@@ -43,8 +44,8 @@
        System.out.println("结束:" + (System.currentTimeMillis() - starttime) / 1000);
    }
    private static final int width = 898;
    private static final int height = 1308;
    private static final int width = 750;
    private static final int height = 1140;
    /**
     * 生成分享图片
@@ -195,7 +196,7 @@
        }
    }
    public static InputStream bufferedImageToInputStream(BufferedImage backgroundImage) throws IOException {
    public static InputStream bufferedImageToInputStream(BufferedImage backgroundImage) {
        return bufferedImageToInputStream(backgroundImage, "jpg");
    }
@@ -207,7 +208,7 @@
     * @return
     * @throws IOException
     */
    public static InputStream bufferedImageToInputStream(BufferedImage backgroundImage, String format) throws IOException {
    public static InputStream bufferedImageToInputStream(BufferedImage backgroundImage, String format)  {
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        try (
                ImageOutputStream
@@ -215,6 +216,8 @@
            ImageIO.write(backgroundImage, format, imOut);
            InputStream is = new ByteArrayInputStream(bs.toByteArray());
            return is;
        }catch (Exception e){
            return null;
        }
    }
@@ -265,8 +268,83 @@
        }
        return resMatrix;
    }
    public static InputStream generateUserCardImg(String imgurl, String qrcode, String name,String type, String phone, String shopaddr, String shopname,String phoneIcon,String addrIcon)  {
        BufferedImage bi =  new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        //得到它的绘制环境(这张图片的笔)
        Graphics2D g2 = (Graphics2D) bi.getGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
        g2.fillRect(0, 0, width, height);
        //背景图片
        BufferedImage img1 =getImgIO(imgurl);
        if(img1!=null){
            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);
        int w = 40+ name.length()*40;
        g2.setColor(Color.GRAY);
        g2.setFont(new Font("黑体", Font.PLAIN, 30));
        g2.drawString(type, w, 990-242);
        if(StringUtils.isNotBlank(qrcode)){
            //个人二维码
            BufferedImage img2 = getImgIO(qrcode);
            if(img2!=null){
                g2.drawImage(img2, 522, 982-242, 188, 188, null);
            }
            g2.setColor(Color.GRAY);
            g2.setFont(new Font("黑体", Font.PLAIN, 24));
            g2.drawString("扫码添加好友", 544, 1186-242);
        }
        //手机号
        BufferedImage img4 = getImgIO(phoneIcon);
        if(img4!=null){
            g2.drawImage(img4, 40, 1072-28-242, 34, 36, null);
        }
        g2.setColor(Color.GRAY);
        g2.setFont(new Font("黑体", Font.PLAIN, 30));
        g2.drawString(phone, 94, 1072-242);
        BufferedImage img3 = getImgIO(addrIcon);
        if(img3!=null){
            g2.drawImage(img3, 40, 1142-28-242, 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);
        //底部
        if(shopname.length()>15){
            shopname = shopname.substring(0,15)+"...";
        }
        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) {
        try {
        return     ImageIO.read(new URL(imgurl));
        }catch (Exception e){
        }return null;
    }
}