最近做了生成电子名片的公众号,分享一下关于图片和文字拼接的功能。
直接上代码:
Controller
@RequestMapping(value="/getImgPosterLogin")
public void getImgPosterLogin (HttpServletRequest request , HttpServletResponse response , CardModel cardModel , String openId ,String imgName) throws ServletException, IOException {
User user = (User)request.getSession().getAttribute("user");
if (user == null && openId !=null) {
//先通过openId查询用户是不是在数据库中存在
user = userService.getUserInfoByOpenId(openId);
if (user == null) {
JSONObject jsonObject = WeChatClientUtil.doGetJson("http://ldsfirstwechat.imwork.net/accessToken");
//根据用户openId 和 accessToken 来获取用户信息
String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+jsonObject.getString("access_token")+"&openid="+openId+"&lang=zh_CN";
System.out.println(url +" url123456 ");
JSONObject userInfo = WeChatClientUtil.doGetJson(url);
System.out.println(userInfo +" userInfo");
//插入用户
userService.insert(new User(null, null, openId, new Date(), new Date(), userInfo.getString("nickname"), userInfo.getLong("sex"), userInfo.getString("province"), userInfo.getString("city"), userInfo.getString("country"), userInfo.getString("headimgurl"), userInfo.getString("unionid"), false , null));
user = userService.getUserInfoByOpenId(openId);
request.getSession().setAttribute("user", user);
} else {
request.getSession().setAttribute("user", user);
}
}
if(cardModel.getModelCard().equals("2018032101.jpg")) {
ImageHandleHelper.overlapImageOne(cardModel,response);
}else if(cardModel.getModelCard().equals("2018032102.jpg")) {
ImageHandleHelper.overlapImagetwo(cardModel,response);
}else if(cardModel.getModelCard().equals("2018032103.jpg")) {
ImageHandleHelper.overlapImagethree(cardModel,response);
}else if(cardModel.getModelCard().equals("2018032104.jpg")) {
ImageHandleHelper.overlapImagefour(cardModel,response);
}
}
工具类
public static void overlapImageOne(CardModel cardModel , HttpServletResponse response) {
try {
BufferedImage big = ImageIO.read(new URL("网络链接"));
BufferedImage small = ImageIO.read(new URL("网络链接"+cardModel.getErweima().substring(0, cardModel.getErweima().indexOf("?"))));
Graphics2D g = big.createGraphics();
int x = 49;
int y = 233;
//drawImage(文件,文件左上角x坐标,文件左上角y坐标,文件width,文件height,observer)
//输出字体抗锯齿
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(small, x, y, 169, 167, null);
//添加文字
//最后一个参数用来设置字体的大小
Font f = new Font("黑体",Font.BOLD,30);
Color mycolor = Color.BLACK;//new Color(0, 0, 255);
g.setColor(mycolor);
g.setFont(f);
//10,20 表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。
g.drawString(cardModel.getName(),275,279);
String address = "*************************";
f = new Font("黑体",Font.BOLD,16);
g.setFont(f);
g.drawString(cardModel.getCompany()+cardModel.getJob(),375,279);
if(cardModel.getPhone1() == null || cardModel.getPhone1().equals("")) {
cardModel.setPhone1(cardModel.getPhone2());
cardModel.setPhone2("");
}
if(cardModel.getPhone2() == null || cardModel.getPhone2().equals("")) {
cardModel.setPhone2("");
}
g.drawString(cardModel.getPhone1(),304,321);
g.drawString(cardModel.getPhone2(),420,321);
g.drawString(cardModel.getEmail(),304,350);
g.drawString(address,304,382);
g.dispose();
//返回图片
response.setContentType("image/jpg");
OutputStream os = response.getOutputStream();
ImageIO.write(big, "jpg", os);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
这样就直接返回图片到页面上了 页面上可以吧 img的 src改为上面这个方法的路径,就可以直接将拼接好的路径返回了,
可以按照自己想放的位置来指定 x,y的坐标
File output = new File("c:\\image\\"+name+".jpg");
ImageIO.write(big, "JPG", output);
这样可以将图片保存在本地 你想保存的目录
BufferedImage big = ImageIO.read(new File(backgroundFile)); //这个可以读取本地图片,上面的代码是读取的网络图片。