`
北风norther
  • 浏览: 13416 次
  • 性别: Icon_minigender_1
  • 来自: 邯郸
社区版块
存档分类
最新评论

用java在图片上写字

 
阅读更多
//这个代码网上很常见,不过我找到的都是加一行的。修改后自动换行,见方法list2()

package cloud.com;

import javax.imageio.ImageIO;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class ImageT {

	private Font font = new Font("", Font.PLAIN, 20);// 添加字体的属性设置
	private Graphics2D g = null;
	private int fontsize = 0;
	private int x = 0;
	private int y = 0;
	
	/**
	 * 修改图片,返回修改后的图片缓冲区
	 */
	public BufferedImage modifyImage(BufferedImage img, String content, int x,
			int y) {
		try {
			int w = img.getWidth();
			int h = img.getHeight();
			g = img.createGraphics();
			g.setBackground(Color.WHITE);
			g.setColor(Color.RED);
			if (this.font != null)
				g.setFont(this.font);
			// 验证输出位置的纵坐标和横坐标
			if (x >= h || y >= w) {
				this.x = h - this.fontsize + 2;
				this.y = w;
			} else {
				this.x = x;
				this.y = y;
			}
			List<String> ls = list2(content,w-this.x,this.fontsize);
			for(int i=0;i<ls.size();i++){
				g.drawString(ls.get(i), this.x, this.y);
				this.y+=30;
			}
			g.dispose();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}

		return img;
	}
	/**
	 * 把字符串分行
	 * @param content 
	 * @param maxLength 一行长度#像素
	 * @param fontSzie 字体大小
	 * @return List<String>
	 */
	public List<String> list2(String text,int maxLength,int fontSzie){
		 int length = 0;
		 int lengthcl=maxLength/fontSzie;
		 List<String> list = new ArrayList<String>();
		 StringBuffer sb = new StringBuffer();
	     for (int i = 0; i < text.length(); i++) {  
	    	 if (new String(text.charAt(i) + "").getBytes().length > 1) {  
	              length += 2;
	         } else {
	              length += 1;  
	         }
	    	 if((length+1)/2<=lengthcl){
	    		 sb.append(text.charAt(i));
	    	 }else{
	    		 i--;
	    		 length=0;
	    		 list.add(sb.toString());
	    		 sb= new StringBuffer();
	    	 }
	     }
		return list;
	}

	public BufferedImage modifyImagetogeter(BufferedImage b, BufferedImage d) {
		try {
			int w = b.getWidth();
			int h = b.getHeight();
			g = d.createGraphics();
			g.drawImage(b, 100, 10, w, h, null);
			g.dispose();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		return d;
	}

	public static void main(String[] args) {

		ImageT tt = new ImageT();
		String str = "一个初创却无可ssd限量的团队:我们有来自于fff五百强企业的技术we宅男,有来自于知名房企的品牌经理,有来自于广告行业的资深销售。我们为同一个目标凝聚,励志做一个互联网时代企业革新的引导者,好伙伴。 ";
		BufferedImage d = tt.loadImageLocal("e:\\IMG_0201.JPG");
		tt.setFont("楷体", 30);
		tt.writeImageLocal("e:\\cc.jpg",tt.modifyImage(d,str,410,40));
		System.out.println("success");
	}
	/**
	 * 导入本地图片到缓冲区
	 */
	public BufferedImage loadImageLocal(String imgName) {
		try {
			return ImageIO.read(new File(imgName));
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		return null;
	}

	/**
	 * 导入网络图片到缓冲区
	 */
	public BufferedImage loadImageUrl(String imgName) {
		try {
			URL url = new URL(imgName);
			return ImageIO.read(url);
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		return null;
	}

	/**
	 * 生成新图片到本地
	 */
	public void writeImageLocal(String newImage, BufferedImage img) {
		if (newImage != null && img != null) {
			try {
				File outputfile = new File(newImage);
				ImageIO.write(img, "jpg", outputfile);
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}
		}
	}
	/**
	 * 设置文字字体
	 */
	public void setFont(String fontStyle, int fontSize) {
		this.fontsize = fontSize;
		this.font = new Font(fontStyle, Font.PLAIN, fontSize);
	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics