美文网首页
java绘图

java绘图

作者: YANG_ad29 | 来源:发表于2020-10-24 10:46 被阅读0次
/**
     * @param posterImgUrl
     *            海报
     * @param qrCodeImageTemp
     *            临时二维码
     *@param nickName 昵称
     * @return 合成图片地址
     */
    public static String drawImage(String posterImgUrl, String qrCodeImageTemp,String nickName) throws IOException {
    
 
        int width = 750;
        int height = 1334;
        BufferedImage bgBufImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// RGB形式
        Graphics2D bgBufImageGraphics = bgBufImage.createGraphics();
        bgBufImageGraphics.setBackground(Color.WHITE);// 设置背景色
        bgBufImageGraphics.clearRect(0, 0, width, height);// 通过使用当前绘图表面的背景色进行填充来清除指定的矩形。

        if (nickName.length() > 5)
            nickName = nickName.substring(0,5);
        // bgBufImageGraphics.setBackground(new Color(255,255,255));
        bgBufImageGraphics.setPaint(Color.black);// 设置画笔,设置Paint属性
        Font font = new Font("微软雅黑", Font.PLAIN, 28);
        bgBufImageGraphics.setFont(font);
        // 抗锯齿
        bgBufImageGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        // 计算文字长度,计算居中的x点坐标
        FontMetrics fm = bgBufImageGraphics.getFontMetrics(font);
        int textWidth = fm.stringWidth(nickName);
        int widthX = (width - textWidth) / 2;

        // 表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。

        // BufferedImage posterBufImage = ImageIO.read(new URL(posterImgUrl));
        // //直接使用图片做背景,自定义背景使用上面方式
        // Graphics2D posterBufImageGraphics = posterBufImage.createGraphics();
 
        BufferedImage posterBufImage = ImageIO.read(new URL(posterImgUrl));
        BufferedImage qrCodeImage = ImageIO.read(new URL(qrCodeImageTemp));
    //  BufferedImage headImage = ImageIO.read(new URL(headImgUrl));
 
        // 设置圆形头像
//      BufferedImage roundHeadImg = new BufferedImage(headImage.getWidth(), headImage.getHeight(),
//              BufferedImage.TYPE_INT_RGB);
//
//      Graphics2D roundHeadGraphics = roundHeadImg.createGraphics();
//      Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, roundHeadImg.getWidth(), roundHeadImg.getHeight());
//      roundHeadGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//      roundHeadImg = roundHeadGraphics.getDeviceConfiguration().createCompatibleImage(headImage.getWidth(), headImage.getHeight(), Transparency.TRANSLUCENT);
//      roundHeadGraphics = roundHeadImg.createGraphics();
//      // 使用 setRenderingHint 设置抗锯齿
//      roundHeadGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
//      roundHeadGraphics.setClip(shape);
//      roundHeadGraphics.drawImage(headImage, 0, 0, null);
//      roundHeadGraphics.dispose();
 
        // bgBufImageGraphics.drawImage(qrCodeImage, (posterBufImage.getWidth()
        // - qrCodeImage.getWidth()), 10, qrCodeImage.getWidth(),
        // qrCodeImage.getHeight(), null);
        // posterBufImageGraphics.drawImage(roundHeadImg, 50, 100,
        // HEAD_URL_WIDTH, HEAD_URL_HEIGHT, null);
        bgBufImageGraphics.drawImage(posterBufImage, 0, 0, 750, 1334, null);
        bgBufImageGraphics.drawImage(qrCodeImage, 265, 640, 220, 220, null);
        bgBufImageGraphics.drawString(nickName, widthX, 900);
        bgBufImageGraphics.dispose();
 
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        ImageOutputStream imgOut = ImageIO.createImageOutputStream(bs);
        ImageIO.write(bgBufImage, "png", imgOut);
        String url = FilePersistenceUtil.uploadQNImg(new ByteArrayInputStream(bs.toByteArray()));//上传七牛云
        return "https://"+url;// 返回合成的图片地址url
    }

相关文章

网友评论

      本文标题:java绘图

      本文链接:https://www.haomeiwen.com/subject/nieimktx.html