java.awt.Graphics2D 生成图片--个人章的方法

# java.awt 画矩形踩坑 

    本来使用Graphics2D的drawRect()方法画图的,但是出现了下边框过粗,不得已 使用了画线的方法,四个边框,单独划线,

这里要注意坐标轴和线的宽度--高度height

  一下是代码

/**
     * 创建私人章的方法
     * @param drawStrName  输入的姓名 1- 18 位
     * @param filePathAndFileName E:\test2\grp.png
     * @param drawStrSpecial -- CN310110HAJ403CFD383
     */
    public static void createPersonalSeal(String drawStrName,String filePathAndFileName,String drawStrSpecial) {

        String drawStrSecondLine ="";
        String drawStrFirstLine ="";

        int length = drawStrName.length();
        Map<Integer,SignImage.Params> weidthHeightMap = getWeidthHeight(length);

        Params params = weidthHeightMap.get(length);
        int width = params.getWidth() *2;
        int height = params.getHeight() *2;

        int fontHeight = 12 * 2;
        int fontHeightSpecial = 12;

        int frameLineWhiteSize = 8;
        int newWidth = width;
        int newHeight =  height + frameLineWhiteSize + fontHeightSpecial;
        int frameLineSize = 5;

        BufferedImage buffImg = new BufferedImage(newWidth, newHeight , BufferedImage.TYPE_INT_RGB);
        Graphics2D gd = buffImg.createGraphics();
        //设置透明  start
        buffImg = gd.getDeviceConfiguration().createCompatibleImage(newWidth, newHeight, Transparency.TRANSLUCENT);
        gd = buffImg.createGraphics();
        //设置透明  end

        //设置字体
        Font fontSong = new Font("宋体", Font.BOLD, fontHeight);
        gd.setFont(fontSong);
        //类对象,可以获得某个字体的高度,以及字符串的宽度
        FontMetrics fmSong = gd.getFontMetrics(fontSong);

        Stroke oldStroke = gd.getStroke();
        //设置颜色

        //设置线宽为5.0
        gd.setStroke(new BasicStroke(5.0f));
        //画边框
//        gd.drawRect(0, 0, width - 1 , height - fontHeightSpecial -8);
        // 画一个矩形
        gd.fillRect(0, 0, newWidth, newHeight);
        // 去除锯齿(当设置的字体过大的时候,会出现锯齿)
        gd.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        gd.setColor(Color.black);
        gd.fillRect(0, 0, frameLineSize, height);
        gd.fillRect(0, 0, width , frameLineSize);
        gd.fillRect(0, height , width, frameLineSize);
        gd.fillRect(width - frameLineSize, 0, width, height);

        int songFontwidthFirst = 0;
        int songFontwidthSecond = 0;
        int arialFontheightFirst = 0;
        int arialFontheightSecond = 0;
        boolean largeFlag = length >3;
        if(largeFlag){
            int subStartLength = length % 2;
            if(subStartLength ==1){
                subStartLength = (length -1)/2;
            }else{
                subStartLength = length / 2;
            }
            drawStrFirstLine = drawStrName.substring(0,subStartLength);
            drawStrSecondLine = drawStrName.substring(subStartLength,length);

            songFontwidthFirst = fmSong.stringWidth(drawStrFirstLine);
            songFontwidthSecond = fmSong.stringWidth(drawStrSecondLine);

            arialFontheightFirst = fmSong.getHeight();
            arialFontheightSecond = fmSong.getHeight();
        }else{
            drawStrFirstLine = drawStrName;
            songFontwidthFirst = fmSong.stringWidth(drawStrFirstLine);
            arialFontheightFirst = fmSong.getHeight();
        }

        if(largeFlag){
            //先画第一行,输出文字(中文横向居中)
            gd.drawString(drawStrFirstLine, (width - songFontwidthFirst) /2,
                    (newHeight - arialFontheightFirst *2   -frameLineSize)/2 + 10);
            //画第二行,输出文字(中文横向居中)
            gd.drawString(drawStrSecondLine, (width - songFontwidthSecond) /2,
                    (newHeight - arialFontheightFirst *2  - frameLineSize )/2 + arialFontheightFirst + 20);
        }else{
            gd.drawString(drawStrFirstLine, (width - songFontwidthFirst) /2, newHeight /2 );
        }

        Font arialFont = new Font("Arial", 2, fontHeightSpecial);
        gd.setFont(arialFont);
        //类对象,可以获得某个字体的高度,以及字符串的宽度
        FontMetrics fm = gd.getFontMetrics(arialFont);
        int arialFontwidth = fm.stringWidth(drawStrSpecial);

        gd.setColor(Color.BLACK);
        gd.drawString(drawStrSpecial, (width - arialFontwidth) /2,  newHeight);
        // 透明度设置结束
        gd.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
        gd.dispose();
        try {
            ImageIO.write(buffImg, "png", new File(filePathAndFileName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        buffImg.flush();
    }

    public static Map<Integer,SignImage.Params> getWeidthHeight(int length){
        int width =0;
        int height = 0;

        switch (length){
            case 1:
                width = 93;
                height = 44 ;
                break;
            case 2:
                width = 93;
                height = 44 ;
                break;
            case 3:
                width = 93;
                height = 44 ;
                break;
            case 4:
                width = 93;
                height = 44 ;
                break;
            case 5:
                width = 93;
                height = 44 ;
                break;
            case 6:
                width = 93;
                height = 44 ;
                break;
            case 7:
                width = 107;
                height = 50;
                break;
            case 8:
                width = 107;
                height = 50;
                break;
            case 9:
                width = 119;
                height = 50;
                break;
            case 10:
                width = 119;
                height = 50;
                break;
            case 11:
                width = 136;
                height = 58;
                break;
            case 12:
                width = 136;
                height = 58;
                break;
            case 13:
                width = 136;
                height = 58;
                break;
            case 14:
                width = 136;
                height = 58;
                break;
            case 15:
                width = 136;
                height = 58;
                break;
            case 16:
                width = 136;
                height = 58;
                break;
            case 17:
                width = 136;
                height = 58;
                break;
            case 18:
                width = 136;
                height = 58;
                break;
            default: throw new RuntimeException("姓名字数不符合要求");
        }
        Map<Integer,SignImage.Params> reParentMap = com.google.common.collect.Maps.newHashMap();
        SignImage.Params returnParams = new SignImage.Params();
        returnParams.setHeight(height);
        returnParams.setWidth(width);
        reParentMap.put(length,returnParams);

       return reParentMap;
    }
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    @Builder
    @Accessors(chain = true)
    @EqualsAndHashCode
    static class Params {

        int width;
        int height;
    }

    public static void main(String[] args) {
        SignImage.createPersonalSeal("张大牛","E:\\test2\\grp.png","CN310110HAJ403CFD383");
    }

一下为参考的资源;

    private static void transparent() throws IOException {
        int width = 400;
        int height = 300;
// 创建BufferedImage对象
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 获取Graphics2D
        Graphics2D g2d = image.createGraphics();

// ----------  增加下面的代码使得背景透明  -----------------
//        image = g2d.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
//        g2d.dispose();
//        g2d = image.createGraphics();
// ----------  背景透明代码结束  -----------------
//        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,0.5f));

        // 画一个矩形
        g2d.fillRect(0, 0, width, height);
        // 去除锯齿(当设置的字体过大的时候,会出现锯齿)
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        Color color = new Color(255, 0, 0);
        g2d.setColor(color);
        g2d.fillRect(0, 0, 8, height);
        g2d.fillRect(0, 0, width, 8);
        g2d.fillRect(0, height - 8, width, height);
        g2d.fillRect(width - 8, 0, width, height);
// 画图
        g2d.setColor(new Color(255, 0, 0));
        g2d.setStroke(new BasicStroke(10));
        g2d.drawString("name", 100, 100);
//释放对象
        g2d.dispose();
        BufferedImage bufferedImage = alphaProcess(image);
// 保存文件
        ImageIO.write(bufferedImage, "png", new File("d:/test/test.png"));
        bufferedImage.flush();
    }
发布了76 篇原创文章 · 获赞 94 · 访问量 35万+

猜你喜欢

转载自blog.csdn.net/u012240455/article/details/103730922