package com.jeeplus.common.utils;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;
import sun.swing.SwingUtilities2;

public class WordToPic {

    //
    public static int getLength(String text) {
        int length = 0;
        for (int i = 0; i < text.length(); i++) {
            if (new String(text.charAt(i) + "").getBytes().length > 1) {
                length += 2;
            } else {
                length += 1;
            }
        }
        return length / 2;
    }

    public static void TextToPic(String text, int width, int height,int fontSize,String filepath) {
        try {
            File file = new File(filepath);
            Font font = new Font("微软雅黑", 0, fontSize);//sans
            BufferedImage bi = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = bi.createGraphics();
            g2.setBackground(new Color(0,188,212));
            g2.clearRect(0, 0, width, height);
            //btn2文本
            g2.setColor(Color.white);
            g2.setFont(font);
            g2.drawString(text, (width - (text.length() * fontSize)) / 2 + 0,(height - fontSize) / 2 +45);
            g2.dispose();
            /*Graphics2D g2 = bi.createGraphics();
            g2.setColor(new Color(255, 120, 89));
            //g2.setBackground(new Color(56,110,162));
            g2.clearRect(0, 0, width, height);
            g2.setFont(font);
            g2.setColor(Color.white);
            g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP,0.3f));
            g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
            printString(g2, text, (width - (text.length() * fontSize)) / 2 + 0,(height - fontSize) / 2 +45, fontSize);
            g2.dispose();*/
            ImageIO.write(bi, "png", file);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void TextToAliyunPic(String text, int width, int height, int fontHeight,Color bground, ByteArrayOutputStream outputStream) {
        try {
            Font font = new Font("微软雅黑", 0, fontHeight);//sans
            BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D gd = buffImg.createGraphics();
            //设置透明  start
            buffImg = gd.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
            gd=buffImg.createGraphics();
            //设置透明  end
            gd.setFont(new Font("微软雅黑", Font.PLAIN, fontHeight)); //设置字体
            gd.setColor(Color.black); //设置颜色
            gd.drawString(text, width/2-fontHeight*text.length()/2,fontHeight);
            ImageIO.write(buffImg, "png", outputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void printString(Graphics2D g2d, String str, int x, int y,int fontSize) {
        FontMetrics metrics = SwingUtilities2.getFontMetrics(null,g2d.getFont());
        for (char ca : str.toCharArray()) {
            int px = metrics.stringWidth("" + ca);
            g2d.drawString("" + ca, x + (fontSize - px) / 2, y);
            x += fontSize;
        }
    }

    public static String getDate() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        return formatter.format(new Date());
    }

    public static void main(String[] args)
    {
        BufferedImage imgMap = drawTranslucentStringPic(400, 80, 36,"欢迎访问我的博客");
        File imgFile=new File("D://www.cxyapi.com.png");
        try
        {
            ImageIO.write(imgMap, "PNG", imgFile);
        } catch (IOException e)
        {
            e.printStackTrace();
        }
        System.out.println("生成完成");
    }


    public static BufferedImage drawTranslucentStringPic(int width, int height, Integer fontHeight,String drawStr)
    {
        try
        {
            BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D gd = buffImg.createGraphics();
            //设置透明  start
            buffImg = gd.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
            gd=buffImg.createGraphics();
            //设置透明  end
            gd.setFont(new Font("微软雅黑", Font.PLAIN, fontHeight)); //设置字体
            gd.setColor(Color.ORANGE); //设置颜色
           // gd.drawRect(0, 0, width - 1, height - 1); //画边框
            gd.drawString(drawStr, width/2-fontHeight*drawStr.length()/2,fontHeight); //输出文字（中文横向居中）
            return buffImg;
        } catch (Exception e) {
            return null;
        }
    }

}