123

package com.yudao.photos;

public class FontText {
private String text;
    
    private int wm_text_pos;
    
    private String wm_text_color;
    
    private Integer wm_text_size;
    
    private String wm_text_font;//字体  “黑体,Arial”

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public int getWm_text_pos() {
        return wm_text_pos;
    }

    public void setWm_text_pos(int wm_text_pos) {
        this.wm_text_pos = wm_text_pos;
    }

    public String getWm_text_color() {
        return wm_text_color;
    }

    public void setWm_text_color(String wm_text_color) {
        this.wm_text_color = wm_text_color;
    }

    public Integer getWm_text_size() {
        return wm_text_size;
    }

    public void setWm_text_size(Integer wm_text_size) {
        this.wm_text_size = wm_text_size;
    }

    public String getWm_text_font() {
        return wm_text_font;
    }

    public void setWm_text_font(String wm_text_font) {
        this.wm_text_font = wm_text_font;
    }

    public FontText(String text, int wm_text_pos, String wm_text_color,
            Integer wm_text_size, String wm_text_font) {
        super();
        this.text = text;
        this.wm_text_pos = wm_text_pos;
        this.wm_text_color = wm_text_color;
        this.wm_text_size = wm_text_size;
        this.wm_text_font = wm_text_font;
    }
    
    public FontText(){}
}
package com.yudao.photos;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

import org.apache.commons.lang3.StringUtils;

public class TestImg {
	public static void main(String[] args) {
		String filePath = "d:\\2.jpg";
		String outPath = "d:\\3.jpg";
		drawTextInImg(filePath, outPath, new FontText("李凯旋新视觉", 1, "#ff0000", 40, "宋体"));

	}

	public static void drawTextInImg(String filePath, String outPath, FontText text) {
		ImageIcon imgIcon = new ImageIcon(filePath);
		Image img = imgIcon.getImage();
		int width = img.getWidth(null);
		int height = img.getHeight(null);
		BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

		Graphics2D g = bimage.createGraphics();
		g.setColor(getColor(text.getWm_text_color()));
		g.setBackground(Color.white);
		g.drawImage(img, 0, 0, null);
		Font font = null;
		if (StringUtils.isEmpty(text.getWm_text_font()) && text.getWm_text_size() != null) {
			font = new Font(text.getWm_text_font(), Font.BOLD, text.getWm_text_size());
		} else {
			font = new Font(null, Font.BOLD, 15);
		}

		g.setFont(font);
		FontMetrics metrics = new FontMetrics(font) {
		};
		Rectangle2D bounds = metrics.getStringBounds(text.getText(), null);
		int textWidth = (int) bounds.getWidth();
		int textHeight = (int) bounds.getHeight();
		int left = 0;
		int top = textHeight;

		// 九宫格控制位置
		if (text.getWm_text_pos() == 2) {
			left = width / 2;
		}
		if (text.getWm_text_pos() == 3) {
			left = width - textWidth;
		}
		if (text.getWm_text_pos() == 4) {
			top = height / 2;
		}
		if (text.getWm_text_pos() == 5) {
			left = width / 2;
			top = height / 2;
		}
		if (text.getWm_text_pos() == 6) {
			left = width - textWidth;
			top = height / 2;
		}
		if (text.getWm_text_pos() == 7) {
			top = height - textHeight;
		}
		if (text.getWm_text_pos() == 8) {
			left = width / 2;
			top = height - textHeight;
		}
		if (text.getWm_text_pos() == 9) { 
			left = width - textWidth;
			top = height - textHeight;
		}
		g.drawString(text.getText(), left, top);
		g.drawString(text.getText(), 100, 100);
		g.drawString(text.getText(), 150, 150);
		g.dispose();

		try {
			FileOutputStream out = new FileOutputStream(outPath);
			ImageIO.write(bimage, "JPEG", out);
			out.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
    // color #2395439
    public static Color getColor(String color) {
        if (color.charAt(0) == '#') {
            color = color.substring(1);
        }
        if (color.length() != 6) {
            return null;
        }
        try {
            int r = Integer.parseInt(color.substring(0, 2), 16);
            int g = Integer.parseInt(color.substring(2, 4), 16);
            int b = Integer.parseInt(color.substring(4), 16);
            return new Color(r, g, b);
        } catch (NumberFormatException nfe) {
            return null;
        }
    }
	
}

 

转载于:https://my.oschina.net/likaixuan0/blog/1584245

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值