【图片与文字的合成】

先做好几个准备工作:

1. 要在图片上写的内容 可以使String[] ,也可以是 map 具体情况具体分析

2. 要确定在那张图片上写,要给出具体路径,如:d:/p38.jpg

3. 确定文字写在图片上的位置 可以手工调整,这里提供的例子是有两个选择 文字在左,文字在右

上方法:---------------------------->

/**
* 给指定的图片,添加文字信息
*
* @param String[] 存储的文字信息

* @param imgName 待处理的图片路径名称,比如 d:/p38.jpg
* @param positionType 文字的方位 <pre>
* 1:文字在名片的右
* 2:文字在名片的左
* @return 合成文字成功,返回true ; 失败,返回false ;
*/
public static boolean wordAddPhoto(String[] Message, String imgName,String position)
{
byte[] bytes = null;
try
{
File _file = new File(imgName);
Image src = ImageIO.read(_file);
int width = src.getWidth(null);
int height = src.getHeight(null);
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.createGraphics();
g.drawImage(src,0,0,width,height,null);
g.setColor(Color.DARK_GRAY);

if(position.equals("1")){
g.setFont(new Font("宋体", Font.PLAIN, 30));
g.drawString(Message[0],width - width/2 + 9 ,height - height/2-30);
g.setFont(new Font("宋体", Font.PLAIN, 15));
g.drawString(Message[1],width - width/2 + 104 ,height - height/2-30);
g.setFont(new Font("微软雅黑", Font.PLAIN, 11));
g.drawString("公 司:"+Message[2],width - width/2 - 20,height - height/2 + 10);
g.drawString("地 址:"+Message[3],width - width/2 - 20,height - height/2 + 25);
g.drawString("固定电话:"+Message[4],width - width/2 - 20,height - height/2 + 40);
g.drawString("手 机:"+Message[5],width - width/2 - 20,height - height/2 + 55);
g.drawString("邮 箱:"+Message[6],width - width/2 - 20,height - height/2 + 70);
}else if(position.equals("2")){
g.setFont(new Font("宋体", Font.PLAIN, 30));
g.drawString(Message[0],width - width/2 - 150,height - height/2-30);
g.setFont(new Font("宋体", Font.PLAIN, 15));
g.drawString(Message[1],width - width/2 - 55,height - height/2-30);
g.setFont(new Font("微软雅黑", Font.PLAIN, 11));
g.drawString("公 司:"+Message[2],width - width/2 - 175,height - height/2 + 7);
g.drawString("地 址:"+Message[3],width - width/2 - 175,height - height/2 + 22);
g.drawString("电固定话:"+Message[4],width - width/2 - 175,height - height/2 + 37);
g.drawString("手 机:"+Message[5],width - width/2 - 175,height - height/2 + 52);
g.drawString("邮 箱:"+Message[6],width - width/2 - 175,height - height/2 + 67);
}
g.dispose();
ByteArrayOutputStream out1 = new ByteArrayOutputStream();
saveImage(image,out1);
bytes = out1.toByteArray();
out1.close();
FileOutputStream out2 = new FileOutputStream(imgName);
out2.write(bytes);
out2.close();
}
catch (Exception e)
{
System.out.println(e);
return false ;
}

return true ;
}
/**
* 文字图片合成后 保存新名片在原来位置
* @param img
* @param out1
* @throws Exception
*/
public static void saveImage(BufferedImage img, OutputStream out1) throws Exception
{
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out1);
encoder.encode(img);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值