java后台生成微信中用的分享图片

本文介绍了如何在Java后台生成用于微信分享的图片,包括使用底图和色值转换工具,提供了一段具体的代码示例。
摘要由CSDN通过智能技术生成

案例:

      

底图:

          

        

色值转换工具:http://www.atool.org/colorpicker.php

代码:

public interface ActivityShareImageServise {

    /**生成活动分享图*/
    String generateActivityShareImage(ActivityShareImageType activityType,Integer itemId) throws Exception;

    /**
     *  生成分享图片组件
     * @param activityType
     * @param itemId    商品id
     * @return  有序组件(ImageParam、TextParam),绘图时有序添加组件
     * @throws IOException
     */
    LinkedList<Object> generateimgComponent(ActivityShareImageType activityType,Integer itemId) throws Exception;
}



/**
 * 活动分享图类型
 */
public enum ActivityShareImageType {
    TEJIA_ACTIVITY("特价活动分享图","images/tejia/tejia-background.jpg"),
    CUTPRICE_ACTIVITY_POOL_COLOR("砍价活动分享图策略池-色彩","images/cutpricepool/color.png"),;

    ActivityShareImageType(String type,String imgPath){
        this.type = type;
        this.imgPath = imgPath;
    }

    public String type;
    public String imgPath;
}

 

 @Override
    public String generateActivityShareImage(ActivityShareImageType activityType, Integer itemId) throws Exception{
        try {
            BufferedImage backImage = loadClassPathImage(activityType.imgPath);
            LinkedList list = generateimgComponent(activityType,itemId);
            return graphics2DImg(activityType,backImage,list);
        }catch (Exception e){   //避免创建分享图
            logger.error("生成活动分析卡片图异常:{}",e.getMessage(),e);
            throw new BizException(0,"创建活动分享卡片图异常");
        }
    }  




//加载底图
    private BufferedImage loadClassPathImage(String classPath) {
        BufferedImage bufferedImage = null;
        try {
            InputStream resourceAsStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(classPath);
            bufferedImage = ImageIO.read(resourceAsStream);
        } catch (IOException e) {
            //e.printStackTrace();
        	logger.error(e.getMessage(),e);;
        }
        return bufferedImage;
    }

 @Override
public LinkedList<Object> generateimgComponent(ActivityShareImageType activityType, 
  Integer itemId) throws Exception{
        LinkedList<Object> list = new LinkedList<>();
        switch (activityType){
case TEJIA_ACTIVITY:{
                BufferedImage backImage = loadClassPathImage(activityType.imgPath);
                //白色底图
                BufferedImage whiteImage = new BufferedImage(backImage.getWidth()-20*2, backImage.getHeight()-20,BufferedImage.TYPE_INT_BGR);
                Graphics2D graphics = whiteImage.createGraphics();
                graphics.setBackground(Color.white);
                graphics.clearRect(0, 0, backImage.getWidth()-20*2, backImage.getHeight()-20);
                graphics.dispose();
                whiteImage = roundImage(whiteImage,whiteImage.getWidth(),30);
                ImageParam whiteParam = new ImageParam(whiteImage,
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值