Java 模板图片添加内容

public class AuthImageUtils {
    /**
     * 生成授权图片
     * @param authCode 授权编码
     * @param memberName 被授权人名称
     * @param phone 手机号
     * @param ids 身份证号
     * @param authInfo 授权名称
     * @param wxInfo 微信编号
     * @return 返回生成的具体文件目录地址
     */
    public static String createNxAuthImg(String authCode,String memberName,String phone,String ids,String authInfo,String wxInfo)
    {
        String localUploadPath = System.getProperty("user.dir") + "/files/";
        File filePath1 = new File(localUploadPath);
        if (!filePath1.exists()) {
            filePath1.mkdirs();
        }
        // 生成图片路径
        String outPath= localUploadPath+authCode+".jpeg";
        // 模板图片路径
        String filePath=localUploadPath+"ex.jpeg";
        
        ImageIcon imgIcon=new ImageIcon(filePath);
        Image theImg =imgIcon.getImage();
        int width=theImg.getWidth(null)==-1?200:theImg.getWidth(null);
        int height= theImg.getHeight(null)==-1?200:theImg.getHeight(null);
        BufferedImage bimage = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
        Graphics2D authCodeConfig=bimage.createGraphics();
        Color mycolor =new Color(77,77,77);
        authCodeConfig.setColor(mycolor);
        authCodeConfig.setBackground(mycolor);
        authCodeConfig.drawImage(theImg, 0, 0, null );
        authCodeConfig.setFont(new Font("宋体",Font.PLAIN,40)); //字体、字型、字号
        authCodeConfig.drawString(authCode,width/2-120,height/2-40); //授权编码
        int memberInfoHeight=height/2+170;
        int memberInfoWidth=width/2-80;
        authCodeConfig.drawString(memberName,memberInfoWidth,memberInfoHeight); //被授权人姓名
        authCodeConfig.drawString(phone,memberInfoWidth,memberInfoHeight+90); //被授权人手机号
        authCodeConfig.drawString(wxInfo,memberInfoWidth,memberInfoHeight+180); //微信号
        authCodeConfig.drawString(ids,memberInfoWidth,memberInfoHeight+260); //身份证号
        authCodeConfig.drawString(authInfo,memberInfoWidth-160,memberInfoHeight+380); //等级
        authCodeConfig.dispose();
        try
        {
            FileOutputStream out=new FileOutputStream(outPath);
            ImageIO.write(bimage,"jpeg",out);
            out.close();
        }
        catch(Exception e) {
            e.printStackTrace();
            return "";
        }
        return outPath;
    }
}

你可能感兴趣的:(Java,第三方配置,个人笔记,java,开发语言)