package com.ssj.framework.weixin.util;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

  
  
public class ImgBean {  
  
  //  private Font font = new Font("宋体", Font.PLAIN, 30);// 添加字体的属性设置  
  
    private static int fontsize = 0;  
  
    private static int x = 0;  
  
    private static  int y = 0;  
  
    /** 
     * 导入本地图片到缓冲区 
     */  
    public static BufferedImage loadImageLocal(String imgName) {  
        try {  
            return ImageIO.read(new File(imgName));  
        } catch (IOException e) {  
        }  
        return null;  
    }  

  
    /** 
     * 生成新图片到本地 
     */  
    public static void writeImageLocal(String newImage, BufferedImage img) {
        if (newImage != null && img != null) {
            try {  
            	String t_ext = newImage.substring(newImage.lastIndexOf(".") + 1);
                File outputfile = new File(newImage);
                ImageIO.write(img, t_ext, outputfile);  
            } catch (IOException e) {
                e.printStackTrace();
            }  
        }  
    }  
  
  
    /** 
     * 修改图片,返回修改后的图片缓冲区（只输出一行文本） 
     */  
    public static BufferedImage modifyImage(BufferedImage img, Object content, int x,  
            int y) {  
  
        try {  
            int w = img.getWidth();  
            int h = img.getHeight();  
            Graphics2D  g = img.createGraphics();  
            g.setBackground(Color.WHITE);  
            g.setColor(Color.white);//设置字体颜色 
            //g.setBackground(Color.GRAY);
           // g.setColor(Color.GRAY);
           // if (this.font != null)  
           //     g.setFont(this.font);  
            g.setFont(new Font("微软雅黑", Font.PLAIN, 32));
            // 验证输出位置的纵坐标和横坐标  
            if (x >= h || y >= w) {  
            	ImgBean.x = h - ImgBean.fontsize + 2;  
            	ImgBean.y = w;  
            } else {  
            	ImgBean.x = x;  
            	ImgBean.y = y;  
            }  
            if (content != null) {  
                g.drawString(content.toString(), ImgBean.x, ImgBean.y);  
            }  
            g.dispose();  
        } catch (Exception e) {  
        }  
  
        return img;  
    }  
    
    public static BufferedImage modifyImage(BufferedImage img, Object content, int x,  
    		int y,Color color,Font font) {  
    	try {  
    		Graphics2D  g = img.createGraphics();  
    		g.setStroke(new BasicStroke(0));
    		g.setBackground(color);  
    		g.setColor(color);//设置字体颜色 
    		g.setFont(font);
    		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
    		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    		if (content != null) {  
    			g.drawString(content.toString(), x, y);  
    		}  
    		g.dispose();  
    	} catch (Exception e) {  
    	}  
    	return img;  
    }  


    /**
     * 修改图片,返回修改后的图片缓冲区（输出多个文本段）
     * @param b
     * @param d
     * @param width
     * @param height
     * @return
     */
    public static BufferedImage modifyImagetogeter(BufferedImage b, BufferedImage d,int width,int height) {  
  
        try {  
            int w = b.getWidth();  
            int h = b.getHeight();  
            Graphics2D  g = d.createGraphics();  
            g.drawImage(b, width, height, w, h, null);  
            g.dispose();  
        } catch (Exception e) {  
        }  
  
        return d;  
    }  

    
  
}  