java给图片加文字_java在图片上添加文字

该Java代码实现了一个将文字添加到图片上的功能。它读取指定路径的图片,使用Graphics2D绘制文字,并根据九宫格位置调整文字坐标。颜色、字体大小、字体类型和文字位置都可以通过参数进行设置。最终,生成的图片保存到输出路径。
摘要由CSDN通过智能技术生成

packagecom.pro.drawTextOnImg;importjava.awt.Color;importjava.awt.Font;importjava.awt.FontMetrics;importjava.awt.Graphics2D;importjava.awt.Image;importjava.awt.geom.Rectangle2D;importjava.awt.image.BufferedImage;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjavax.imageio.ImageIO;importjavax.swing.ImageIcon;importorg.apache.commons.lang3.StringUtils;public classTestImg {public static voidmain(String[] args) {

String filePath= "d:\\inimg03.png";

String outPath= "d:\\2.jpg";

drawTextInImg(filePath, outPath,new FontText("中国", 1, "#CC2BAC", 40, "黑体"));

}public static voiddrawTextInImg(String filePath,String outPath, FontText text) {

ImageIcon imgIcon= newImageIcon(filePath);

Image img=imgIcon.getImage();int width = img.getWidth(null);int height = img.getHeight(null);

BufferedImage bimage= newBufferedImage(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= newFont(text.getWm_text_font(), Font.BOLD,

text.getWm_text_size());

}else{

font= new Font(null, Font.BOLD, 15);

}

g.setFont(font);

FontMetrics metrics= newFontMetrics(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.dispose();try{

FileOutputStream out= newFileOutputStream(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 staticColor 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 newColor(r, g, b);

}catch(NumberFormatException nfe) {return null;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值