java 图片写字_用java在图片上写字

这段代码展示了如何使用Java在图片上写字,包括设置字体、颜色,以及处理文字自动换行。通过方法`modifyImage`修改图片并在指定位置添加文字,通过`list2`方法将长文本按行分割适应图片宽度。

//这个代码网上很常见,不过我找到的都是加一行的。修改后自动换行,见方法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 ls = list2(content,w-this.x,this.fontsize);

for(int i=0;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

*/

public List list2(String text,int maxLength,int fontSzie){

int length = 0;

int lengthcl=maxLength/fontSzie;

List list = new ArrayList();

StringBuffer sb = new StringBuffer();

for (int i = 0; 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);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值