|
|
|
@@ -0,0 +1,132 @@ |
|
|
|
package com.iformall.utils; |
|
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.springframework.util.ResourceUtils; |
|
|
|
|
|
|
|
import javax.imageio.ImageIO; |
|
|
|
import java.awt.*; |
|
|
|
import java.awt.image.BufferedImage; |
|
|
|
import java.io.ByteArrayInputStream; |
|
|
|
import java.io.ByteArrayOutputStream; |
|
|
|
import java.io.File; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.time.LocalDateTime; |
|
|
|
import java.time.format.DateTimeFormatter; |
|
|
|
import java.util.Locale; |
|
|
|
|
|
|
|
public class ImgYwsqhUtil { |
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ImgYwsqhUtil.class); |
|
|
|
|
|
|
|
//模板图片 |
|
|
|
private static final String imgTempPath = "imgTemp/wxjjywsqh.jpg"; |
|
|
|
|
|
|
|
/** |
|
|
|
* 生成图片证书 |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public static byte[] imgCeate(String contact_name,String contact_id_number,String contact_department,String contact_job, |
|
|
|
String legal_person,String id_card_number,String card_period_begin,String card_period_end, |
|
|
|
boolean isSeal,String merchant_name){ |
|
|
|
if(StringUtils.isBlank(contact_name)){ |
|
|
|
logger.error("名字为空,无法生成证书"); |
|
|
|
return null; |
|
|
|
} |
|
|
|
if(contact_name.length() == 2){ |
|
|
|
contact_name = contact_name.replaceAll("(\\S)", "$0 "); |
|
|
|
} |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
InputStream backImgUrl = ClassLoader.getSystemResourceAsStream(imgTempPath); |
|
|
|
// InputStream backImgUrl = ImgYwsqhUtil.class.getClassLoader().getResourceAsStream(imgTempPath); |
|
|
|
|
|
|
|
BufferedImage backImg = ImageIO.read(backImgUrl); |
|
|
|
Graphics g = backImg.getGraphics(); |
|
|
|
|
|
|
|
Font fTxtBottom = new Font("微软雅黑", Font.PLAIN, 70); |
|
|
|
Color myColorTxtBottom = Color.BLACK; |
|
|
|
|
|
|
|
g.setFont(fTxtBottom); |
|
|
|
g.setColor(myColorTxtBottom); |
|
|
|
|
|
|
|
FontMetrics metrics = g.getFontMetrics(fTxtBottom); |
|
|
|
|
|
|
|
//图片加中文名 |
|
|
|
int xName = 2955 - metrics.stringWidth(contact_name)/2 ; |
|
|
|
g.drawString(contact_name, xName , 1282);//g.drawString(文字, x 位置, y 位置); |
|
|
|
|
|
|
|
//图片加身份证号 |
|
|
|
if(StringUtils.isNotBlank(contact_id_number)){ |
|
|
|
int xNumber = 1200 - metrics.stringWidth(contact_id_number) / 2; |
|
|
|
g.drawString(contact_id_number, xNumber, 1500); |
|
|
|
} |
|
|
|
//图片加部门 |
|
|
|
if(StringUtils.isNotBlank(contact_department)){ |
|
|
|
int xDepartment = 2340 - metrics.stringWidth(contact_department) / 2; |
|
|
|
g.drawString(contact_department, xDepartment, 1500); |
|
|
|
} |
|
|
|
//图片加职务 |
|
|
|
if(StringUtils.isNotBlank(contact_job)){ |
|
|
|
int xJob = 3100 - metrics.stringWidth(contact_job) / 2; |
|
|
|
g.drawString(contact_job, xJob, 1500); |
|
|
|
} |
|
|
|
//图片加法人姓名 |
|
|
|
if(StringUtils.isNotBlank(legal_person)){ |
|
|
|
g.drawString(legal_person, 1250, 2590); |
|
|
|
} |
|
|
|
//图片加法人身份证号 |
|
|
|
if(StringUtils.isNotBlank(id_card_number)){ |
|
|
|
g.drawString(id_card_number, 1100, 2800); |
|
|
|
} |
|
|
|
//图片加有效期 |
|
|
|
if(StringUtils.isNotBlank(card_period_begin) && StringUtils.isNotBlank(card_period_end)){ |
|
|
|
String card_begin_end = card_period_begin + " 至 " + card_period_end; |
|
|
|
g.drawString(card_begin_end, 1190, 3020); |
|
|
|
} |
|
|
|
//图片加商户/单位名称merchant_name |
|
|
|
if(StringUtils.isNotBlank(merchant_name)){ |
|
|
|
g.drawString(merchant_name, 2400, 4540); |
|
|
|
} |
|
|
|
|
|
|
|
//无公章 |
|
|
|
if(!isSeal){ |
|
|
|
Color sealColor = Color.LIGHT_GRAY; |
|
|
|
g.setColor(sealColor); |
|
|
|
int xSeal = 2400; |
|
|
|
g.drawString("无公章", xSeal, 4320); |
|
|
|
} |
|
|
|
|
|
|
|
//调这个方法就是开始这个整合 (可以多张图片,多个文字整合成一张图片,只有把他们放在这方法里面就行) |
|
|
|
g.dispose(); |
|
|
|
|
|
|
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
|
|
ImageIO.write(backImg, "png", out); |
|
|
|
|
|
|
|
return out.toByteArray(); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
logger.error("生成证书异常", e.getMessage()); |
|
|
|
} |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
// public static void main(String[] args) { |
|
|
|
// String serverUploadImgUrl = "C:/Users/xiaohu/Desktop/img"; // 图片保存路径 |
|
|
|
// try { |
|
|
|
// byte[] bytes = imgCeate("周淼淼","421302199908081234","商务发展部","商务经理", |
|
|
|
// "周淼淼","421302199908081234","2020-08-08","长期", |
|
|
|
// false,"武汉富茂链客科技有限公司"); |
|
|
|
// ByteArrayInputStream bais = new ByteArrayInputStream(bytes); |
|
|
|
// BufferedImage bi1 =ImageIO.read(bais); |
|
|
|
// |
|
|
|
// ImageIO.write(bi1, "png", new File(serverUploadImgUrl+"/0.png")); |
|
|
|
// |
|
|
|
// } catch (Exception e) { |
|
|
|
// e.printStackTrace(); |
|
|
|
// } |
|
|
|
// System.out.println("结束"); |
|
|
|
// } |
|
|
|
} |