java 邀请卡生成工具,微信嵌入头像(圆角),图片,文字

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 邀请卡生成工具,微信嵌入头像(圆角),图片,文字相关的知识,希望对你有一定的参考价值。

public class InvitationCardUtil {

    private static final int QR_X = 240;
    private static final int QR_Y = 50;

    private static final int TEXT_X = 530;
    private static final int TEXT_Y = 1040;

    private static final int HEAD_X = 550;
    private static final int HEAD_Y = 985;

    private static final int COURSE_TEXT_X = 460;
    private static final int COURSE_TEXT_Y = 900;

    private static final int COURSE_HEAD_X = 330;
    private static final int COURSE_HEAD_Y = 950;


    private static final int COURSE_QR_X = 240;
    private static final int COURSE_QR_Y = 240;


    private static final int COURSE_X = 85;//越大越靠近left
    private static final int COURSE_Y = 280; //越大越靠近top

    private static final String FILE_EXT = "PNG";


    /**
     * @param cardType     邀请卡模板枚举
     * @param qrUrl        二维码url(微信)
     * @param nickname     微信昵称
     * @param headImgUrl   微信头像url(微信)
     * @param courseImgUrl 课程图片Url(oss)
     * @return 生成好的模板文件Url(oss)
     * @throws IOException
     */
    public static String createInvitationCard(InvitationCardType cardType, String qrUrl, String nickname, String headImgUrl, String courseImgUrl) throws IOException {

        if (null == cardType || !StringUtils.isNoneBlank(qrUrl, nickname, headImgUrl)) {
            return null;
        }

        if (InvitationCardType.COURSE == cardType && StringUtils.isBlank(courseImgUrl)) {
            return null;
        }

        //生成后的文件名
        String fileName = UUID.randomUUID().toString();
        //读取模板文件
        Image src = ImageIO.read(cardType.getCard());
        int width = src.getWidth(null);
        int height = src.getHeight(null);
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.createGraphics();
        g.drawImage(src, 0, 0, width, height, null);

        // 嵌入二维码
        Image read = handleQR(qrUrl);
        int readWidth = read.getWidth(null);
        int readHeight = read.getHeight(null);
        g.drawImage(read, width - readWidth - (InvitationCardType.COURSE == cardType ? COURSE_QR_X : QR_X), height - readHeight - (InvitationCardType.COURSE == cardType ? COURSE_QR_Y : QR_Y), readWidth, readHeight, null);

        // 嵌入头像
        Image headRead = handleHead(headImgUrl);
        int headReadWidth = headRead.getWidth(null);
        int headReadHeight = headRead.getHeight(null);
        g.drawImage(headRead, width - headReadWidth - (InvitationCardType.COURSE == cardType ? COURSE_HEAD_X : HEAD_X), height - headReadHeight - (InvitationCardType.COURSE == cardType ? COURSE_HEAD_Y : HEAD_Y), headReadWidth, headReadHeight, null);

        if (InvitationCardType.COURSE == cardType) {
            // 嵌入课程图片
            Image courseRead = handleCourse(courseImgUrl);
            int courseReadWidth = courseRead.getWidth(null);
            int courseReadHeight = courseRead.getHeight(null);
            g.drawImage(courseRead, width - courseReadWidth - COURSE_X, height - courseReadHeight - COURSE_Y, courseReadWidth, courseReadHeight, null);
        }

        // 昵称
        g.setColor((InvitationCardType.COURSE == cardType ? Color.BLACK : Color.WHITE));
        g.setFont(new Font("PingFangSC-Medium", Font.BOLD, 25));
        g.drawString(nickname, width - (InvitationCardType.COURSE == cardType ? COURSE_TEXT_X : TEXT_X), height - 25 / 2 - (InvitationCardType.COURSE == cardType ? COURSE_TEXT_Y : TEXT_Y));
        g.dispose();

        String key = "invite-cd/" + fileName + "." + FILE_EXT;
        return AliOSSUtil.writeImage(key, image);
    }

    /**
     * 处理头像
     *
     * @param headImgUrl
     * @return
     */
    private static Image handleHead(String headImgUrl) throws IOException {
        return getCircularImage(urlToImage(headImgUrl), 100);
    }


    /**
     * 处理二维码
     *
     * @param qrUrl
     * @return
     */
    private static Image handleQR(String qrUrl) throws IOException {
        return getResizeImage(urlToImage(qrUrl), 250);
    }

    /**
     * 处理课程图片
     *
     * @param qrUrl
     * @return
     */
    private static Image handleCourse(String qrUrl) throws IOException {
        return getResizeImage(urlToImage(qrUrl), 233);
    }

    /**
     * 圆形图片
     *
     * @param source
     * @param size
     * @return
     */
    private static Image getCircularImage(BufferedImage source, int size) {
        BufferedImage circularImg = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2 = circularImg.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setColor(Color.WHITE);
        g2.fill(new RoundRectangle2D.Float(0, 0, size, size, 360, 360));
        g2.setComposite(AlphaComposite.SrcAtop);
        g2.drawImage(source, 0, 0, size, size, null);
        g2.dispose();
        return circularImg;
    }

    /**
     * 等比压缩图片
     *
     * @param source
     * @param size
     * @return
     */
    private static Image getResizeImage(BufferedImage source, int size) {
        int type = source.getType();
        BufferedImage target;
        double sx = (double) size / source.getWidth();
        double sy = (double) size / source.getHeight();
        // 这里想实现在targetW,targetH范围内实现等比缩放。如果不需要等比缩放
        // 则将下面的if else语句注释即可
        if (sx < sy) {
            sx = sy;
            size = (int) (sx * source.getWidth());
        } else {
            sy = sx;
            size = (int) (sy * source.getHeight());
        }
        if (type == BufferedImage.TYPE_CUSTOM) { // handmade
            ColorModel cm = source.getColorModel();
            WritableRaster raster = cm.createCompatibleWritableRaster(size,
                    size);
            boolean alphaPremultiplied = cm.isAlphaPremultiplied();
            target = new BufferedImage(cm, raster, alphaPremultiplied, null);
        } else {
            target = new BufferedImage(size, size, type);
        }
        Graphics2D g = target.createGraphics();
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
        g.dispose();
        return target;
    }

    /**
     * url -> Image
     *
     * @param urlStr
     * @return
     * @throws IOException
     */
    private static BufferedImage urlToImage(String urlStr) throws IOException {
        URL url = new URL(urlStr);
        URLConnection con = url.openConnection();
        //超时
        con.setConnectTimeout(5000);

        //缓存
        con.setUseCaches(true);
        con.setDefaultUseCaches(true);
        InputStream is = con.getInputStream();

        //先读入内存
        ByteArrayOutputStream buf = new ByteArrayOutputStream(8192);
        byte[] b = new byte[1024];
        int len;
        while ((len = is.read(b)) != -1) {
            buf.write(b, 0, len);
        }
        //读图像
        return ImageIO.read(new ByteArrayInputStream(buf.toByteArray()));
    }

    /**
     * emoji表情替换
     *
     * @param source  原字符串
     * @param slipStr emoji表情替换成的字符串
     * @return 过滤后的字符串
     */
    public static String filterEmoji(String source, String slipStr) {
        if (StringUtils.isNotBlank(source)) {
            return source.replaceAll("[\\ud800\\udc00-\\udbff\\udfff\\ud800-\\udfff]", slipStr);
        } else {
            return source;
        }
    }


    public static void main(String[] args) throws IOException {

        String qrUrl = "http://mmbiz.qpic.cn/mmbiz_jpg/Ok5g79kcQL182Ba4CCZHCUPB24DsYYsGpzicBQ34nKP5qWljLFBat1ic8UCrsrKIe7I9GM8PJCpo320bKBKvDnhw/0";

        String nickname = "Aperson’s狂欢\uD83C\uDF02";

        //大图
        String headImgUrl ="";
        //小图
        String headImgUrlMin = "";

        String courseImgUrl = "http://hbb-deploy.oss-cn-beijing.aliyuncs.com/hpc_banner/GeNm8DHfpd.png";

        //String invitationCard = createInvitationCard(InvitationCardType.COURSE, qrUrl, nickname, headImgUrlMin, courseImgUrl); 直接测试路径不一样

        //System.out.println("生成的模板绝对路径: " + invitationCard);

    }
}

以上是关于java 邀请卡生成工具,微信嵌入头像(圆角),图片,文字的主要内容,如果未能解决你的问题,请参考以下文章

微信小程序 progress 进度条 内部圆角及内部条渐变色

canvas 生成圆角图片(头像等)

java怎么生成带用户微信头像的图片,并把这张图片发送给用户。

java 微信海报的实现

java 微信海报的实现

iOS之设置用户头像的圆角