Java後臺Html轉圖片和獲取頁面屬性值,及圖片拼接
Java後臺Html轉圖片
1.引入相關maven包
<dependency>
<groupId>gui.ava</groupId>
<artifactId>html2image</artifactId>
<version>2.0.1</version>
</dependency>
<!--二維碼 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>2.2</version>
</dependency>
<!--圖片疊加 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.18</version>
</dependency>
<!-- html 內容獲取-->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
2. HtmlImageGeneratorUtil 工具類
public class HtmlImageGeneratorUtil {
private final static Logger logger = LoggerFactory.getLogger(HtmlImageGeneratorUtil.class);
private static Pattern p = Pattern.compile("[\u4E00-\u9FA5|\\!|\\,|\\。|\\(|\\)|\\《|\\》|\\“|\\”|\\?|\\:|\\;|\\【|\\】]");
/**
* 封裝模板頁面資料
* @param map
* @param type
* @return
*/
public static List<String> getSingleImageHtml(Map<String,String> map, String type) {
String html = "";
String html2 = "";
List<String> list = new ArrayList<>();
Map<String,String> sizeMap = getFontSize( map,type);
String streetSize = sizeMap.get("streetSize");
String width = sizeMap.get("width"); //圖片寬
String height = sizeMap.get("height"); //圖片高
String cityWidth = "156";//sizeMap.get("cityWidth"); //城市拼音圖片寬
String cityHeight = sizeMap.get("cityHeight"); //城市拼音圖片高
String qrcodeWidth = sizeMap.get("qrcodeWidth"); //二維碼圖片寬
String qrcodeHeight = sizeMap.get("qrcodeHeight"); //二維碼圖片高
String street = sizeMap.get("street");
String streetFontSize = sizeMap.get("streetFontSize");
String streetNo = sizeMap.get("streetNo");
String streetNoFontSize = sizeMap.get("streetNoFontSize");
String subStreet = sizeMap.get("subStreet");
String subStreetFontSize = sizeMap.get("subStreetFontSize");
String subStreetNo = sizeMap.get("subStreetNo");
String subStreetNoFontSize = sizeMap.get("subStreetNoFontSize");
String unit = sizeMap.get("unit");
String unitFontSize = sizeMap.get("unitFontSize");
String aoiGate = sizeMap.get("aoiGate");
String aoiGateFontSize = sizeMap.get("aoiGateFontSize");
String room = sizeMap.get("room");
String roomFontSize = sizeMap.get("roomFontSize");
String cityCode = sizeMap.get("cityCode");
String cityCodeFontSize = sizeMap.get("cityCodeFontSize");
String streetNoWidth = sizeMap.get("streetNoWidth");
logger.info("########################## type = "+ type +"################################");
logger.info("##########################street = "+ map.get("street")+"################################");
logger.info("##########################streetNo = "+ map.get("streetNo")+"################################");
logger.info("##########################subStreet = "+ map.get("subStreet")+"################################");
logger.info("##########################subStreetNo = "+ map.get("subStreetNo")+"################################");
logger.info("##########################unit = "+ map.get("unit")+"################################");
logger.info("##########################room = "+ map.get("room")+"################################");
if(TaskPackageConstants.DOOR_CLASS_BIG.equals(type)){
html += "<div style=\"width: "+width+"px; height: "+height+"px; background-color: rgb(255, 255, 255); box-shadow: rgba(119, 119, 119, 0.2) 0px 1px 2px 0px, rgba(119, 119, 119, 0.1) 0px 1px 5px 0px, rgba(119, 119, 119, 0.12) 0px 2px 3px 0px; position: relative;\" id=\"panelNode\">";
html += "<div style=\"width: 401.625px; height: 101.25px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 67.5px; font-weight: 600; color: rgb(51, 51, 51); line-height: 27px; max-height: 90px; position: absolute; left: 100px; top: 63px;\" id=\"street\"><b>";
html += map.get("street");
//}
html += "</b></div>";
html += "<div style=\"width: 256.5px; height: 67.5px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 45px; font-weight: 500; color: rgb(51, 51, 51); line-height: 67.5px; max-height: 90px; position: absolute; left: 140px; top: 198px;\" id=\"subStreet\"><b>";
if(StringUtil.isNotEmpty(map.get("subStreet"))){
html += "(" + map.get("subStreet") + ")";
}
html += "</b></div>";
html += "<div style=\"width: 320.625px; height: 219.375px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size:90px; font-weight: 500; color: rgb(51, 51, 51); line-height: 213px; max-height: 180px; position: absolute; left: 108px; top: 293px;\" id=\"streetNo\">";
html += map.get("streetNo");
html += "</div>";
// if(StringUtil.isNotEmpty(map.get(“subStreetNo”))){
// html += “<div style=“width: 60.75px; height: 148.5px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 60px; font-weight: 500; color: rgb(51, 51, 51); line-height: 148.5px; max-height: 90px;position: absolute; left:345px;top:370px” id=“line”>”;
// html += “-”;
// html += “”;
// }
html += “<div style=“width: 74.25px; height: 148.5px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 50px; font-weight: 500; color: rgb(51, 51, 51); line-height: 148.5px; max-height: 90px; position: absolute; left: 320px; top: 350px;” id=“subStreetNo”>”;
html += map.get(“subStreetNo”);
html += “”;
html += “<div style=“width: 118.125px; height: 57.375px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 28px; font-weight: 500; color: rgb(202, 42, 42); line-height: 57px; max-height: 30px; position: absolute; left: 574px;top:235px” id=“aoiGate”>”;
html += map.get(“aoiGate”);
html += “”;
html += “”;
html2 += "<div style="width: "+cityWidth+"px; height: “+cityHeight+“px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 25px; font-weight: 500; color: rgba(0, 0, 0, 0.65); line-height: 45px; max-height: 30px;” id=“cityCode”>”;
html2 += map.get(“cityCode”);
html2 += “”;
}else if(TaskPackageConstants.DOOR_CLASS_BUILD.equals(type)){
html += "<div style=\"width: "+width+"px; height: "+height+"px; background-color: rgb(255, 255, 255); box-shadow: rgba(119, 119, 119, 0.2) 0px 1px 2px 0px, rgba(119, 119, 119, 0.1) 0px 1px 5px 0px, rgba(119, 119, 119, 0.12) 0px 2px 3px 0px; position: relative;\" id=\"panelNode\">";
if(map.get("street").length()>20){
html += "<div style=\"width: 340.875px; height: 84.375px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size:26px; font-weight: 600; color: rgb(51, 51, 51); line-height: 84px; max-height: 120px; position: absolute; left: 100px; top: 74px;\" id=\"street\"><b>";
html += map.get("street");
html += "</b></div>";
}else{
html += "<div style=\"width: 340.875px; height: 84.375px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size:30px; font-weight: 600; color: rgb(51, 51, 51); line-height: 84px; max-height: 120px; position: absolute; left: 100px; top: 74px;\" id=\"street\"><b>";
html += map.get("street");
html += "</b></div>";
}
html += "<div style=\"width: 108px; height: 263.25px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size:150px; font-weight: 500; color: rgb(51, 51, 51); line-height: 262.5px; max-height: 195px; position: absolute; left: 140px; top: 205px;\" id=\"streetNo\">";
html += map.get("streetNo");
html += "</div>";
// html += "<div style=“width: 81px; height: 111.375px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 72px; font-weight: 500; color: rgb(51, 51, 51); line-height: 111px; max-height: 90px;position: absolute; left: 296px; top: 308px; " id=“none”>”;
// html += “棟”;
// html += “”;
html += “<div style=“width: 118.125px; height: 57.375px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 37.5px; font-weight: 500; color: rgb(202, 42, 42); line-height: 57px; max-height: 40.5px; position: absolute; left: 470px; top: 220px;” id=“unit”>”;
html += map.get(“unit”);
html += “”;
html += “”;
html2 += "<div style=\"width: "+cityWidth+"px; height: "+cityHeight+"px; display: flex; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 25px; font-weight: 500; color: rgba(0, 0, 0, 0.65); line-height: 46.5px; max-height: 31.5px;\" id=\"cityCode\">";
html2 += map.get("cityCode");
html2 += "</div>";
}else if(TaskPackageConstants.DOOR_CLASS_MIDDLE.equals(type)){
html += "<div style=\"width: "+width+"px; height: "+height+"px; background-color: rgb(255, 255, 255); box-shadow: rgba(119, 119, 119, 0.2) 0px 1px 2px 0px, rgba(119, 119, 119, 0.1) 0px 1px 5px 0px, rgba(119, 119, 119, 0.12) 0px 2px 3px 0px; position: relative;\" id=\"panelNode\">";
if(street.length() > 10){
html += "<div style=\"width: 283.5px; height: 74.25px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 25px; font-weight: 600; color: rgb(51, 51, 51); line-height: 73.5px; max-height: 90px; position: absolute; left: 84px; top: 52px;\" id=\"street\"><b>";
html += map.get("street");
html += "</b></div>";
}else {
html += "<div style=\"width: 283.5px; height: 74.25px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 38px; font-weight: 600; color: rgb(51, 51, 51); line-height: 73.5px; max-height: 90px; position: absolute; left: 84px; top: 52px;\" id=\"street\"><b>";
html += map.get("street");
html += "</b></div>";
}
html += "<div style=\"width: 239.625px; height: 57.375px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 45px; font-weight: 500; color: rgb(51, 51, 51); line-height: 57px; max-height: 90px; position: absolute; left: 102px; top: 155px;\" id=\"subStreet\">";
if(StringUtil.isNotEmpty(map.get("subStreet"))){
html += "(" + map.get("subStreet") + ")";
}
html += "</div>";
html += "<div style=\"width: 142.5px; height: 148.5px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 110.5px; font-weight: 500; color: rgb(51, 51, 51); line-height: 148.5px; max-height: 112.5px; position: absolute; left: 79px; top: 241px;\" id=\"streetNo\">";
html += map.get("streetNo");
html += "</div>";
String leftSize = "210px";
if(streetNo.length() > 3){
leftSize = "260px";
}
html += "<div style=\"width: 74.25px; height: 148.5px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 55px; font-weight: 500; color: rgb(51, 51, 51); line-height: 148.5px; max-height: 90px; position: absolute; left:"+leftSize+"; top: 260px;\" id=\"subStreetNo\">";
html += map.get("subStreetNo");
html += "</div>";
html += "<div style=\"width: 128.25px; height: 57.375px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 25px; font-weight: 500; color: rgb(202, 42, 42); line-height: 57px; max-height: 40.5px; position: absolute; left: 389px; top: 200px;\" id=\"unit\">";
html += map.get("unit");
html += "</div>";
html += "</div>";
html2 += "<div style=\"width: "+cityWidth+"px; height: "+cityHeight+"px; display: flex; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 25px; font-weight: 500; color: rgba(0, 0, 0, 0.65); line-height: 46.5px; max-height: 27px;\" id=\"cityCode\">";
html2 += map.get("cityCode");
html2 += "</div>";
}else if(TaskPackageConstants.DOOR_CLASS_MIDDLE_M.equals(type)){
html += "<div style=\"width: "+width+"px; height: "+height+"px; background-color: rgb(255, 255, 255); box-shadow: rgba(119, 119, 119, 0.2) 0px 1px 2px 0px, rgba(119, 119, 119, 0.1) 0px 1px 5px 0px, rgba(119, 119, 119, 0.12) 0px 2px 3px 0px; position: relative;\" id=\"panelNode\">";
if(street.length() > 10){
html += "<div style=\"width: 283.5px; height: 74.25px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 30px; font-weight: 600; color: rgb(51, 51, 51); line-height: 73.5px; max-height: 90px; position: absolute; left: 85px; top: 61px;\" id=\"street\"><b>";
html += map.get("street");
html += "</b></div>";
}else {
html += "<div style=\"width: 283.5px; height: 74.25px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 44px; font-weight: 600; color: rgb(51, 51, 51); line-height: 73.5px; max-height: 90px; position: absolute; left: 85px; top: 61px;\" id=\"street\"><b>";
html += map.get("street");
html += "</b></div>";
}
html += "<div style=\"width: 239.625px; height: 57.375px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 45px; font-weight: 500; color: rgb(51, 51, 51); line-height: 57px; max-height: 90px; position: absolute; left: 82px; top: 166px;\" id=\"subStreet\"><b>";
html += map.get("subStreet");
html += "</b></div>";
html += "<div style=\"width: 90px; height: 148.5px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 50px; font-weight: 500; color: rgb(51, 51, 51); line-height: 148.5px; max-height: 180px; position: absolute; left: 161px; top: 233px;\" id=\"streetNo\">";
html += map.get("streetNo");
html += "</div>";
if(StringUtil.isNotEmpty(map.get("subStreetNo")) && StringUtil.isContainChinese(map.get("subStreetNo"))){
html += "<div style=\"width: 30px; height: 10px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 40px; font-weight: 500; color: rgb(51, 51, 51); line-height: 298px; max-height: 90px; position: absolute; left: 280px; top: 170px;\" id=\"subStreetNo\">";
}else{
html += "<div style=\"width: 30px; height: 10px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 40px; font-weight: 500; color: rgb(51, 51, 51); line-height: 298px; max-height: 90px; position: absolute; left: 250px; top: 170px;\" id=\"subStreetNo\">";
}
html += map.get("subStreetNo");
html += "</div>";
html += "</div>";
html2 += "<div style=\"width: "+cityWidth+"px; height: "+cityHeight+"px; display: flex; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 34.5px; font-weight: 500; color: rgba(0, 0, 0, 0.65); line-height: 46.5px; max-height: 27px;\" id=\"cityCode\">";
html2 += map.get("cityCode");
html2 += "</div>";
}else if(TaskPackageConstants.DOOR_CLASS_ROOM.equals(type)){
html += "<div style=\"width: "+width+"px; height: "+height+"px; background-color: rgb(255, 255, 255); box-shadow: rgba(119, 119, 119, 0.2) 0px 1px 2px 0px, rgba(119, 119, 119, 0.1) 0px 1px 5px 0px, rgba(119, 119, 119, 0.12) 0px 2px 3px 0px; position: relative;\" id=\"panelNode\">";
if(map.get("street").length() > 18){
html += "<div style=\"width: 240px; height: 57.375px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 14px; font-weight: 600; color: rgb(51, 51, 51); line-height: 57px;max-height: 105px; position: absolute; left: 55px; top: 49px;\" id=\"street\"><b>";
html += map.get("street");
}else{
html += "<div style=\"width: 240px; height: 57.375px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 22px; font-weight: 600; color: rgb(51, 51, 51);line-height: 57px; max-height: 105px; position: absolute; left: 55px; top: 49px;\" id=\"street\"><b>";
html += map.get("street");
}
html += "</b></div>";
html += "<div style=\"width: 180px; height: 141.75px; display: flex; text-align: center;display: block; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 94.5px; font-weight: 500; color: rgb(51, 51, 51); line-height: 141px; max-height: 120px; position: absolute; left: 76px; top: 127px;\" id=\"room\">";
html += map.get("room");
html += "</div>";
html += "</div>";
html2 += "<div style=\"width: "+cityWidth+"px; height: "+cityHeight+"px; display: flex; align-items: center; flex-wrap: nowrap; white-space: nowrap; cursor: move; font-size: 15px; font-weight: 500; color: rgba(0, 0, 0, 0.65); line-height: 40.5px; max-height: 22.5px;\" id=\"cityCode\">";
html2 += map.get("cityCode");
html2 += "</div>";
}
logger.info("########################## html = "+ html+"################################");
logger.info("########################## html2 = "+ html2+"################################");
list.add(html);
list.add(html2);
list.add(width);
list.add(height);
list.add(cityWidth);
list.add(cityHeight);
list.add(qrcodeWidth);
list.add(qrcodeHeight);
list.add(street);
list.add(streetFontSize);
list.add(streetNo);
list.add(streetNoFontSize);
list.add(subStreet);
list.add(subStreetFontSize);
list.add(subStreetNo);
list.add(subStreetNoFontSize);
list.add(unit);
list.add(unitFontSize);
list.add(aoiGate);
list.add(aoiGateFontSize);
list.add(room);
list.add(roomFontSize);
list.add(cityCode);
list.add(cityCodeFontSize);
list.add(streetNoWidth);
return list;
}
/**
* html轉圖片,併疊加圖片
* @param map
* @param type
* @param fileName
* @return
* @throws Exception
*/
public static BufferedImage graphicsHtmlGeneration(Map<String, String> map, String type, String fileName) throws Exception{
//獲取頁面
List<String> list = getSingleImageHtml(map,type);
if (list.get(0) != null && list.get(0).length() > 0) {
String html1 = list.get(0);
//城市拼音html
String html2 = list.get(1);
String width = list.get(2);
String height = list.get(3);
String cityWidth = list.get(4);
String cityHeight = list.get(5);
String qrcodeWidth = list.get(6);
String qrcodeHeight = list.get(7);
String street = list.get(8);
String streetFontSize = list.get(9);
String streetNo = list.get(10);
String streetNoFontSize = list.get(11);
String subStreet = list.get(12);
String subStreetFontSize = list.get(13);
String subStreetNo = list.get(14);
String subStreetNoFontSize = list.get(15);
String unit = list.get(16);
String unitFontSize = list.get(17);
String aoiGate = list.get(18);
String aoiGateFontSize = list.get(19);
String room = list.get(20);
String roomFontSize = list.get(21);
String cityCode = list.get(22);
String cityCodeFontSize = list.get(23);
String streetNoWidth = list.get(24);
Html2Image imageGenerator = Html2Image.fromHtml(html1);
Html2Image imageGenerator2 = Html2Image.fromHtml(html2);
ImageRenderer t1 = imageGenerator.getImageRenderer();
ImageRenderer t2 = imageGenerator2.getImageRenderer();
//設定圖片寬高
t1.setWidth(Double.valueOf(width).intValue());
t1.setHeight(Double.valueOf(height).intValue());
t2.setWidth(Double.valueOf(cityWidth).intValue());
t2.setHeight(Double.valueOf(cityHeight).intValue());
//二維碼寬高
int widthCode = 0;
int heightCode = 0;
if(TaskPackageConstants.DOOR_CLASS_ROOM.equals(type)){
widthCode =Double.valueOf(qrcodeWidth).intValue()+30;
heightCode = Double.valueOf(qrcodeHeight).intValue()+30;
}else{
widthCode =Double.valueOf(qrcodeWidth).intValue();
heightCode = Double.valueOf(qrcodeHeight).intValue();
}
try {
BufferedImage image = imageGenerator.getImageRenderer().getBufferedImage();
String widthFont = new BigDecimal(width).subtract(new BigDecimal(cityHeight)).toString();
if(StringUtil.isNotEmpty(street)){
//設定 street 字型大小
setFontSize(image,widthFont, streetFontSize, street);
}
if(StringUtil.isNotEmpty(streetNo)){
//設定 streetNo 字型大小
setFontSize(image,streetNoWidth, streetNoFontSize, streetNo);
}
if(StringUtil.isNotEmpty(subStreet)){
//設定 subStreet 字型大小
setFontSize(image,width, subStreetFontSize, subStreet);
}
if(StringUtil.isNotEmpty(subStreetNo)){
//設定 subStreetNo 字型大小
setFontSize(image,width, subStreetNoFontSize, subStreetNo);
}
if(StringUtil.isNotEmpty(unit)){
//設定 unit 字型大小
setFontSize(image,qrcodeWidth, unitFontSize, unit);
}
if(StringUtil.isNotEmpty(aoiGate)){
//設定 aoiGate 字型大小
setFontSize(image,qrcodeWidth, aoiGateFontSize, aoiGate);
}
if(StringUtil.isNotEmpty(room)){
//設定 room 字型大小
setFontSize(image,width, roomFontSize, room);
}
BufferedImage citiImage = imageGenerator2.getImageRenderer().getBufferedImage();
if(StringUtil.isNotEmpty(cityCode)){
//設定 cityCode 字型大小
setFontSize(citiImage,cityWidth, cityCodeFontSize, cityCode);
}
//旋轉城市拼音圖片270度
BufferedImage des = rotate(citiImage, 90);
//修改旋轉後圖片寬高
BufferedImage desImage = resizeBufferedImage(des,imageGenerator2.getImageRenderer().getHeight(),imageGenerator2.getImageRenderer().getWidth(),false);
int x = 0;
int y = 0;
if(TaskPackageConstants.DOOR_CLASS_ROOM.equals(type)){
//int x = 540; //原圖片寬減二維碼寬
x = new BigDecimal(width).subtract(new BigDecimal(qrcodeWidth)).intValue() -20;
//int y = (int) 289.125; //原圖片高減二維碼高
y = new BigDecimal(height).subtract(new BigDecimal(qrcodeHeight)).intValue() -20;
}else{
//int x = 540; //原圖片寬減二維碼寬
x = new BigDecimal(width).subtract(new BigDecimal(qrcodeWidth)).intValue();
//int y = (int) 289.125; //原圖片高減二維碼高
y = new BigDecimal(height).subtract(new BigDecimal(qrcodeHeight)).intValue();
}
//int city_X = (int) 708.75; //原圖寬減原城市圖片高
int cityX = new BigDecimal(width).subtract(new BigDecimal(cityHeight)).intValue();
//int city_Y = (int) 47.25; //原城市圖片高
int cityY = Double.valueOf(cityHeight).intValue();
//String imageType = "png";
//String saveFilePath = "D:\\"+fileName+".png";
//獲取二維碼流
BufferedImage image2 = RecodeUtil.codeCreate(map.get("qrcode"),widthCode,heightCode);
//拼接城市拼音旋轉後的圖片
BufferedImage buffImg = overlyingImage(image, desImage, cityX, cityY, 1.0f);
//拼接門牌和二維碼
BufferedImage buffImg1 = overlyingImage(buffImg, image2, x, y, 1.0f);
return buffImg1;
} catch (Exception e) {
logger.error("圖片轉換失敗:", e);
}
}
return null;
}
/**
* 調整bufferedimage大小
* @param source BufferedImage 原始image
* @param targetW int 目標寬
* @param targetH int 目標高
* @param flag boolean 是否同比例調整
* @return BufferedImage 返回新image
*/
private static BufferedImage resizeBufferedImage(BufferedImage source, int targetW, int targetH, boolean flag) {
int type = source.getType();
BufferedImage target = null;
double sx = (double) targetW / source.getWidth();
double sy = (double) targetH / source.getHeight();
if (flag && sx > sy) {
sx = sy;
targetW = (int) (sx * source.getWidth());
} else if (flag && sx <= sy) {
sy = sx;
targetH = (int) (sy * source.getHeight());
}
if (type == BufferedImage.TYPE_CUSTOM) { // handmade
ColorModel cm = source.getColorModel();
WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
boolean alphaPremultiplied = cm.isAlphaPremultiplied();
target = new BufferedImage(cm, raster, alphaPremultiplied, null);
} else {
target = new BufferedImage(targetW, targetH, type);
}
Graphics2D g = target.createGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
g.dispose();
return target;
}
/**
*
* @Title: 構造圖片
* @Description: 生成水印並返回java.awt.image.BufferedImage
* @param buffImg 原始檔(BufferedImage)
* @param waterImg 水印檔案(BufferedImage)
* @param x 距離右下角的X偏移量
* @param y 距離右下角的Y偏移量
* @param alpha 透明度, 選擇值從0.0~1.0: 完全透明~完全不透明
* @return BufferedImage
* @throws IOException
*/
public static BufferedImage overlyingImage(BufferedImage buffImg, BufferedImage waterImg, int x, int y, float alpha) throws IOException {
// 建立Graphics2D物件,用在底圖物件上繪圖
Graphics2D g2d = buffImg.createGraphics();
int waterImgWidth = waterImg.getWidth();// 獲取層圖的寬度
int waterImgHeight = waterImg.getHeight();// 獲取層圖的高度
// 在圖形和影像中實現混合和透明效果
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
// 繪製
g2d.drawImage(waterImg, x, y, waterImgWidth, waterImgHeight, null);
g2d.dispose();// 釋放圖形上下文使用的系統資源
return buffImg;
}
/**
* 圖片旋轉
* @param src
* @param angel
* @return
*/
public static BufferedImage rotate(Image src, int angel) {
int srcWidth = src.getWidth(null)-1;
int srcHeight = src.getHeight(null)-3;
// calculate the new image size
Rectangle rectDes = calcRotatedSize(new Rectangle(new Dimension(
srcWidth, srcHeight)), angel);
BufferedImage res = null;
res = new BufferedImage(rectDes.width, rectDes.height, BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = res.createGraphics();
// transform
g2.translate((rectDes.width - srcWidth) / 2, (rectDes.height - srcHeight) / 2);
g2.rotate(Math.toRadians(angel), (double)srcWidth / 2, (double)srcHeight / 2);
g2.drawImage(src, null, null);
return res;
}
/**
* 圖片旋轉
* @param src
* @param angel
* @return
*/
public static Rectangle calcRotatedSize(Rectangle src, int angel) {
// if angel is greater than 90 degree, we need to do some conversion
if (angel >= 90) {
if(angel / 90 % 2 == 1){
int temp = src.height;
src.height = src.width;
src.width = temp;
}
angel = angel % 90;
}
double r = Math.sqrt((double)src.height * src.height + (double)src.width * src.width) / 2;
double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r;
double angelAlpha = (Math.PI - Math.toRadians(angel)) / 2;
double angelDaltaWidth = Math.atan((double) src.height / src.width);
double angelDaltaHeight = Math.atan((double) src.width / src.height);
int lenDaltaWidth = (int) (len * Math.cos(Math.PI - angelAlpha
- angelDaltaWidth));
int lenDaltaHeight = (int) (len * Math.cos(Math.PI - angelAlpha
- angelDaltaHeight));
int desWidth = src.width + lenDaltaWidth * 2;
int desHeight = src.height + lenDaltaHeight * 2;
return new java.awt.Rectangle(new Dimension(desWidth, desHeight));
}
/**
* 獲取模板裡相關屬性資料
* @param map
* @param type
* @return
*/
public static Map<String,String> getHtmlContext(Map<String, String> map, String type){
Map<String,String> resultMap = new HashMap<>(16);
//獲取頁面
//List<String> list = getSingleImageHtml(map,type);
String html = map.get("html");
//城市拼音
if(TaskPackageConstants.DOOR_CLASS_BIG.equals(type)){
Document document = Jsoup.parse(html);
Element postList = document.getElementById("panelNode");
//獲取模板第一個div樣式
String style = postList.attr("style");
//獲取模板寬高
String style1 = style.substring(style.indexOf("width"));
String style2 = style1.substring(0, style1.indexOf(";"));
String attr = style2.substring(style2.indexOf(":") + 2);
String width = attr.substring(0, attr.indexOf("px"));
//獲取模板高
String style3 = style.substring(style.indexOf("height"));
String style4 = style3.substring(0, style3.indexOf(";"));
String attr1 = style4.substring(style4.indexOf(":") + 2);
String height = attr1.substring(0, attr1.indexOf("px"));
//獲取模板street div
Element streetHtml = document.getElementById("street");
//獲取文字值
//獲取第2個div樣式
String styleStreet = streetHtml.attr("style");
String styleStreet1 = styleStreet.substring(styleStreet.indexOf("font-size"));
String styleStreet2 = styleStreet1.substring(0, styleStreet1.indexOf(";"));
String attr2 = styleStreet2.substring(styleStreet2.indexOf(":") + 2);
String streetFontSize = attr2.substring(0, attr2.indexOf("px"));
//subStreet
Element subStreetHtml = document.getElementById("subStreet");
String styleSubStreet = subStreetHtml.attr("style");
String styleSubStreet1 = styleSubStreet.substring(styleSubStreet.indexOf("font-size"));
String styleSubStreet2 = styleSubStreet1.substring(0, styleSubStreet1.indexOf(";"));
String attrSubStreet = styleSubStreet2.substring(styleSubStreet2.indexOf(":") + 2);
String subStreetFontSize = attrSubStreet.substring(0, attrSubStreet.indexOf("px"));
//streetNo
Element streetNoHtml = document.getElementById("streetNo");
String styleStreetNo = streetNoHtml.attr("style");
String styleStreetNo1 = styleStreetNo.substring(styleStreetNo.indexOf("font-size"));
String styleStreetNo2 = styleStreetNo1.substring(0, styleStreetNo1.indexOf(";"));
String attrStreetNo = styleStreetNo2.substring(styleStreetNo2.indexOf(":") + 2);
String streetNoFontSize = attrStreetNo.substring(0, attrStreetNo.indexOf("px"));
//subStreetNo
Element subStreetNoHtml = document.getElementById("subStreetNo");
String styleSubStreetNo = subStreetNoHtml.attr("style");
String styleSubStreetNo1 = styleSubStreetNo.substring(styleSubStreetNo.indexOf("font-size"));
String styleSubStreetNo2 = styleSubStreetNo1.substring(0, styleSubStreetNo1.indexOf(";"));
String attrSubStreetNo = styleSubStreetNo2.substring(styleSubStreetNo2.indexOf(":") + 2);
String subStreetNoFontSize = attrSubStreetNo.substring(0, attrSubStreetNo.indexOf("px"));
//判斷是否包含中文
String streetNo = map.get("streetNo");
Matcher m = p.matcher(streetNo);
if (m.find()) {
streetNoFontSize = "70";
}
String streetNoWidth1 = styleStreetNo.substring(styleStreetNo.indexOf("width"));
String streetNoWidth2 = streetNoWidth1.substring(0, streetNoWidth1.indexOf(";"));
String attrStreetNoWidth = streetNoWidth2.substring(streetNoWidth2.indexOf(":") + 2);
String streetNoWidth = attrStreetNoWidth.substring(0, attrStreetNoWidth.indexOf("px"));
//aoiGate 大門
Element aoiGateHtml = document.getElementById("aoiGate");
String styleAoiGate = aoiGateHtml.attr("style");
String styleAoiGate1 = styleAoiGate.substring(styleAoiGate.indexOf("font-size"));
String styleAoiGate2 = styleAoiGate1.substring(0, styleAoiGate1.indexOf(";"));
String attrAoiGate = styleAoiGate2.substring(styleAoiGate2.indexOf(":") + 2);
String aoiGateFontSize = attrAoiGate.substring(0, attrAoiGate.indexOf("px"));
//城市拼音div 寬高
Element cityHtml = document.getElementById("cityCode");
//獲取文字值
String cityCode = streetHtml.select("#cityCode").text();
//獲取第一個div樣式
String styleCityCode = cityHtml.attr("style");
String styleCityCode1 = styleCityCode.substring(styleCityCode.indexOf("width"));
String styleCityCode2 = styleCityCode1.substring(0, styleCityCode1.indexOf(";"));
String attr3 = styleCityCode2.substring(styleCityCode2.indexOf(":") + 2);
String cityWidth = attr3.substring(0, attr3.indexOf("px"));
String styleCityCode3 = styleCityCode.substring(styleCityCode.indexOf("height"));
String styleCityCode4 = styleCityCode3.substring(0, styleCityCode3.indexOf(";"));
String attr4 = styleCityCode4.substring(styleCityCode4.indexOf(":") + 2);
String cityHeight = attr4.substring(0, attr4.indexOf("px"));
String styleCityCode11 = styleCityCode.substring(styleCityCode.indexOf("font-size"));
String styleCityCode22 = styleCityCode11.substring(0, styleCityCode11.indexOf(";"));
String attrCityCode = styleCityCode22.substring(styleCityCode22.indexOf(":") + 2);
String cityCodeFontSize = attrCityCode.substring(0, attrCityCode.indexOf("px"));
//二維碼div 寬高
Element qrcodeHtml = document.getElementById("qrcode");
//獲取第一個div樣式
String styleQrcode = qrcodeHtml.attr("style");
String styleQrcode1 = styleQrcode.substring(styleQrcode.indexOf("width"));
String styleQrcode2 = styleQrcode1.substring(0, styleQrcode1.indexOf(";"));
String attrCode = styleQrcode2.substring(styleQrcode2.indexOf(":") + 2);
String qrcodeWidth = attrCode.substring(0, attrCode.indexOf("px"));
String styleQrcode3 = styleQrcode.substring(styleQrcode.indexOf("height"));
String styleQrcode4 = styleQrcode3.substring(0, styleQrcode3.indexOf(";"));
String attrCode2 = styleQrcode4.substring(styleQrcode4.indexOf(":") + 2);
String qrcodeHeight = attrCode2.substring(0, attrCode2.indexOf("px"));
resultMap.put("width",width);
resultMap.put("height",height);
resultMap.put("cityWidth",cityWidth);
resultMap.put("cityHeight",cityHeight);
resultMap.put("qrcodeWidth",qrcodeWidth);
resultMap.put("qrcodeHeight",qrcodeHeight);
resultMap.put("streetFontSize",streetFontSize);
resultMap.put("subStreetFontSize",subStreetFontSize);
resultMap.put("streetNoFontSize",streetNoFontSize);
resultMap.put("aoiGateFontSize",aoiGateFontSize);
resultMap.put("cityCodeFontSize",cityCodeFontSize);
resultMap.put("streetNoWidth",streetNoWidth);
resultMap.put("subStreetNoFontSize",subStreetNoFontSize);
}else if(TaskPackageConstants.DOOR_CLASS_BUILD.equals(type)){
Document document = Jsoup.parse(html);
Element postList = document.getElementById("panelNode");
//獲取模板第一個div樣式
String style = postList.attr("style");
//獲取模板寬高
String style1 = style.substring(style.indexOf("width"));
String style2 = style1.substring(0, style1.indexOf(";"));
String attr = style2.substring(style2.indexOf(":") + 2);
String width = attr.substring(0, attr.indexOf("px"));
//獲取模板高
String style3 = style.substring(style.indexOf("height"));
String style4 = style3.substring(0, style3.indexOf(";"));
String attr1 = style4.substring(style4.indexOf(":") + 2);
String height = attr1.substring(0, attr.indexOf("px"));
//獲取模板第二個div
Element streetHtml = document.getElementById("street");
//獲取文字值
//獲取第一個div樣式
String styleStreet = streetHtml.attr("style");
String styleStreet1 = styleStreet.substring(styleStreet.indexOf("font-size"));
String styleStreet2 = styleStreet1.substring(0, styleStreet1.indexOf(";"));
String attr2 = styleStreet2.substring(styleStreet2.indexOf(":") + 2);
String streetFontSize = attr2.substring(0, attr2.indexOf("px"));
//streetNo
Element streetNoHtml = document.getElementById("streetNo");
String styleStreetNo = streetNoHtml.attr("style");
String styleStreetNo1 = styleStreetNo.substring(styleStreetNo.indexOf("font-size"));
String styleStreetNo2 = styleStreetNo1.substring(0, styleStreetNo1.indexOf(";"));
String attrStreetNo = styleStreetNo2.substring(styleStreetNo2.indexOf(":") + 2);
String streetNoFontSize = attrStreetNo.substring(0, attrStreetNo.indexOf("px"));
//判斷是否包含中文
String streetNo = map.get("streetNo");
Matcher m = p.matcher(streetNo);
if (m.find()) {
streetNoFontSize = "90";
}
String streetNoWidth1 = styleStreetNo.substring(styleStreetNo.indexOf("width"));
String streetNoWidth2 = streetNoWidth1.substring(0, streetNoWidth1.indexOf(";"));
String attrStreetNoWidth = streetNoWidth2.substring(streetNoWidth2.indexOf(":") + 2);
String streetNoWidth = attrStreetNoWidth.substring(0, attrStreetNoWidth.indexOf("px"));
//unit
Element unitHtml = document.getElementById("unit");
String styleUnit = unitHtml.attr("style");
String styleUnit1 = styleUnit.substring(styleUnit.indexOf("font-size"));
String styleUnit2 = styleUnit1.substring(0, styleUnit1.indexOf(";"));
String attrUnit = styleUnit2.substring(styleUnit2.indexOf(":") + 2);
String unitFontSize = attrUnit.substring(0, attrUnit.indexOf("px"));
//城市拼音div 寬高
Element cityHtml = document.getElementById("cityCode");
//獲取文字值
//獲取第一個div樣式
String styleCityCode = cityHtml.attr("style");
String styleCityCode1 = styleCityCode.substring(styleCityCode.indexOf("width"));
String styleCityCode2 = styleCityCode1.substring(0, styleCityCode1.indexOf(";"));
String attr3 = styleCityCode2.substring(styleCityCode2.indexOf(":") + 2);
String cityWidth = attr3.substring(0, attr3.indexOf("px"));
String styleCityCode3 = styleCityCode.substring(styleCityCode.indexOf("height"));
String styleCityCode4 = styleCityCode3.substring(0, styleCityCode3.indexOf(";"));
String attr4 = styleCityCode4.substring(styleCityCode4.indexOf(":") + 2);
String cityHeight = attr4.substring(0, attr4.indexOf("px"));
String styleCityCode11 = styleCityCode.substring(styleCityCode.indexOf("font-size"));
String styleCityCode22 = styleCityCode11.substring(0, styleCityCode11.indexOf(";"));
String attrCityCode = styleCityCode22.substring(styleCityCode22.indexOf(":") + 2);
String cityCodeFontSize = attrCityCode.substring(0, attrCityCode.indexOf("px"));
//二維碼div 寬高
Element qrcodeHtml = document.getElementById("qrcode");
//獲取第一個div樣式
String styleQrcode = qrcodeHtml.attr("style");
String styleQrcode1 = styleQrcode.substring(styleQrcode.indexOf("width"));
String styleQrcode2 = styleQrcode1.substring(0, styleQrcode1.indexOf(";"));
String attrCode = styleQrcode2.substring(styleQrcode2.indexOf(":") + 2);
String qrcodeWidth = attrCode.substring(0, attrCode.indexOf("px"));
String styleQrcode3 = styleQrcode.substring(styleQrcode.indexOf("height"));
String styleQrcode4 = styleQrcode3.substring(0, styleQrcode3.indexOf(";"));
String attrCode2 = styleQrcode4.substring(styleQrcode4.indexOf(":") + 2);
String qrcodeHeight = attrCode2.substring(0, attrCode2.indexOf("px"));
resultMap.put("width",width);
resultMap.put("height",height);
resultMap.put("cityWidth",cityWidth);
resultMap.put("cityHeight",cityHeight);
resultMap.put("qrcodeWidth",qrcodeWidth);
resultMap.put("qrcodeHeight",qrcodeHeight);
resultMap.put("streetFontSize",streetFontSize);
resultMap.put("streetNoFontSize",streetNoFontSize);
resultMap.put("unitFontSize",unitFontSize);
resultMap.put("cityCodeFontSize",cityCodeFontSize);
resultMap.put("streetNoWidth",streetNoWidth);
}else if(TaskPackageConstants.DOOR_CLASS_MIDDLE.equals(type)){
Document document = Jsoup.parse(html);
Element postList = document.getElementById("panelNode");
//獲取模板第一個div樣式
String style = postList.attr("style");
//獲取模板寬高
String style1 = style.substring(style.indexOf("width"));
String style2 = style1.substring(0, style1.indexOf(";"));
String attr = style2.substring(style2.indexOf(":") + 2);
String width = attr.substring(0, attr.indexOf("px"));
//獲取模板高
String style3 = style.substring(style.indexOf("height"));
String style4 = style3.substring(0, style3.indexOf(";"));
String attr1 = style4.substring(style4.indexOf(":") + 2);
String height = attr1.substring(0, attr.indexOf("px"));
//獲取模板第二個div
Element streetHtml = document.getElementById("street");
//獲取文字值
//獲取第一個div樣式
String styleStreet = streetHtml.attr("style");
String styleStreet1 = styleStreet.substring(styleStreet.indexOf("font-size"));
String styleStreet2 = styleStreet1.substring(0, styleStreet1.indexOf(";"));
String attr2 = styleStreet2.substring(styleStreet2.indexOf(":") + 2);
String streetFontSize = attr2.substring(0, attr2.indexOf("px"));
//subStreet
Element subStreetHtml = document.getElementById("subStreet");
String styleSubStreet = subStreetHtml.attr("style");
String styleSubStreet1 = styleSubStreet.substring(styleSubStreet.indexOf("font-size"));
String styleSubStreet2 = styleSubStreet1.substring(0, styleSubStreet1.indexOf(";"));
String attrSubStreet = styleSubStreet2.substring(styleSubStreet2.indexOf(":") + 2);
String subStreetFontSize = attrSubStreet.substring(0, attrSubStreet.indexOf("px"));
//streetNo
Element streetNoHtml = document.getElementById("streetNo");
String styleStreetNo = streetNoHtml.attr("style");
String styleStreetNo1 = styleStreetNo.substring(styleStreetNo.indexOf("font-size"));
String styleStreetNo2 = styleStreetNo1.substring(0, styleStreetNo1.indexOf(";"));
String attrStreetNo = styleStreetNo2.substring(styleStreetNo2.indexOf(":") + 2);
String streetNoFontSize = attrStreetNo.substring(0, attrStreetNo.indexOf("px"));
//判斷是否包含中文
String streetNo = map.get("streetNo");
Matcher m = p.matcher(streetNo);
if (m.find()) {
streetNoFontSize = "90";
}
String streetNoWidth1 = styleStreetNo.substring(styleStreetNo.indexOf("width"));
String streetNoWidth2 = streetNoWidth1.substring(0, streetNoWidth1.indexOf(";"));
String attrStreetNoWidth = streetNoWidth2.substring(streetNoWidth2.indexOf(":") + 2);
String streetNoWidth = attrStreetNoWidth.substring(0, attrStreetNoWidth.indexOf("px"));
//subStreetNo
Element subStreetNoHtml = document.getElementById("subStreetNo");
String styleSubStreetNo = subStreetNoHtml.attr("style");
String styleSubStreetNo1 = styleSubStreetNo.substring(styleSubStreetNo.indexOf("font-size"));
String styleSubStreetNo2 = styleSubStreetNo1.substring(0, styleSubStreetNo1.indexOf(";"));
String attrSubStreetNo = styleSubStreetNo2.substring(styleSubStreetNo2.indexOf(":") + 2);
String subStreetNoFontSize = attrSubStreetNo.substring(0, attrSubStreetNo.indexOf("px"));
//unit
Element unitHtml = document.getElementById("unit");
String styleUnit = unitHtml.attr("style");
String styleUnit1 = styleUnit.substring(styleUnit.indexOf("font-size"));
String styleUnit2 = styleUnit1.substring(0, styleUnit1.indexOf(";"));
String attrUnit = styleUnit2.substring(styleUnit2.indexOf(":") + 2);
String unitFontSize = attrUnit.substring(0, attrUnit.indexOf("px"));
//城市拼音div 寬高
Element cityHtml = document.getElementById("cityCode");
//獲取文字值
//獲取第一個div樣式
String styleCityCode = cityHtml.attr("style");
String styleCityCode1 = styleCityCode.substring(styleCityCode.indexOf("width"));
String styleCityCode2 = styleCityCode1.substring(0, styleCityCode1.indexOf(";"));
String attr3 = styleCityCode2.substring(styleCityCode2.indexOf(":") + 2);
String cityWidth = attr3.substring(0, attr3.indexOf("px"));
String styleCityCode3 = styleCityCode.substring(styleCityCode.indexOf("height"));
String styleCityCode4 = styleCityCode3.substring(0, styleCityCode3.indexOf(";"));
String attr4 = styleCityCode4.substring(styleCityCode4.indexOf(":") + 2);
String cityHeight = attr4.substring(0, attr4.indexOf("px"));
String styleCityCode11 = styleCityCode.substring(styleCityCode.indexOf("font-size"));
String styleCityCode22 = styleCityCode11.substring(0, styleCityCode11.indexOf(";"));
String attrCityCode = styleCityCode22.substring(styleCityCode22.indexOf(":") + 2);
String cityCodeFontSize = attrCityCode.substring(0, attrCityCode.indexOf("px"));
//二維碼div 寬高
Element qrcodeHtml = document.getElementById("qrcode");
//獲取第一個div樣式
String styleQrcode = qrcodeHtml.attr("style");
String styleQrcode1 = styleQrcode.substring(styleQrcode.indexOf("width"));
String styleQrcode2 = styleQrcode1.substring(0, styleQrcode1.indexOf(";"));
String attrCode = styleQrcode2.substring(styleQrcode2.indexOf(":") + 2);
String qrcodeWidth = attrCode.substring(0, attrCode.indexOf("px"));
String styleQrcode3 = styleQrcode.substring(styleQrcode.indexOf("height"));
String styleQrcode4 = styleQrcode3.substring(0, styleQrcode3.indexOf(";"));
String attrCode2 = styleQrcode4.substring(styleQrcode4.indexOf(":") + 2);
String qrcodeHeight = attrCode2.substring(0, attrCode2.indexOf("px"));
resultMap.put("width",width);
resultMap.put("height",height);
resultMap.put("cityWidth",cityWidth);
resultMap.put("cityHeight",cityHeight);
resultMap.put("qrcodeWidth",qrcodeWidth);
resultMap.put("qrcodeHeight",qrcodeHeight);
resultMap.put("streetFontSize",streetFontSize);
resultMap.put("subStreetFontSize",subStreetFontSize);
resultMap.put("streetNoFontSize",streetNoFontSize);
resultMap.put("subStreetNoFontSize",subStreetNoFontSize);
resultMap.put("unitFontSize",unitFontSize);
resultMap.put("cityCodeFontSize",cityCodeFontSize);
resultMap.put("streetNoWidth",streetNoWidth);
}else if(TaskPackageConstants.DOOR_CLASS_MIDDLE_M.equals(type)){
Document document = Jsoup.parse(html);
Element postList = document.getElementById("panelNode");
//獲取模板第一個div樣式
String style = postList.attr("style");
//獲取模板寬高
String style1 = style.substring(style.indexOf("width"));
String style2 = style1.substring(0, style1.indexOf(";"));
String attr = style2.substring(style2.indexOf(":") + 2);
String width = attr.substring(0, attr.indexOf("px"));
//獲取模板高
String style3 = style.substring(style.indexOf("height"));
String style4 = style3.substring(0, style3.indexOf(";"));
String attr1 = style4.substring(style4.indexOf(":") + 2);
String height = attr1.substring(0, attr.indexOf("px"));
//獲取模板第二個div
Element streetHtml = document.getElementById("street");
//獲取第2個div樣式
String styleStreet = streetHtml.attr("style");
String styleStreet1 = styleStreet.substring(styleStreet.indexOf("font-size"));
String styleStreet2 = styleStreet1.substring(0, styleStreet1.indexOf(";"));
String attr2 = styleStreet2.substring(styleStreet2.indexOf(":") + 2);
String streetFontSize = attr2.substring(0, attr2.indexOf("px"));
//subStreet
Element subStreetHtml = document.getElementById("subStreet");
String styleSubStreet = subStreetHtml.attr("style");
String styleSubStreet1 = styleSubStreet.substring(styleSubStreet.indexOf("font-size"));
String styleSubStreet2 = styleSubStreet1.substring(0, styleSubStreet1.indexOf(";"));
String attrSubStreet = styleSubStreet2.substring(styleSubStreet2.indexOf(":") + 2);
String subStreetFontSize = attrSubStreet.substring(0, attrSubStreet.indexOf("px"));
//subStreetNo
Element subStreetNoHtml = document.getElementById("subStreetNo");
String styleSubStreetNo = subStreetNoHtml.attr("style");
String styleSubStreetNo1 = styleSubStreetNo.substring(styleSubStreetNo.indexOf("font-size"));
String styleSubStreetNo2 = styleSubStreetNo1.substring(0, styleSubStreetNo1.indexOf(";"));
String attrSubStreetNo = styleSubStreetNo2.substring(styleSubStreetNo2.indexOf(":") + 2);
String subStreetNoFontSize = attrSubStreetNo.substring(0, attrSubStreetNo.indexOf("px"));
//streetNo
Element streetNoHtml = document.getElementById("streetNo");
String styleStreetNo = streetNoHtml.attr("style");
String styleStreetNo1 = styleStreetNo.substring(styleStreetNo.indexOf("font-size"));
String styleStreetNo2 = styleStreetNo1.substring(0, styleStreetNo1.indexOf(";"));
String attrStreetNo = styleStreetNo2.substring(styleStreetNo2.indexOf(":") + 2);
String streetNoFontSize = attrStreetNo.substring(0, attrStreetNo.indexOf("px"));
//判斷是否包含中文或標點符號
String streetNo = map.get("streetNo");
Matcher m = p.matcher(streetNo);
if (m.find()) {
streetNoFontSize = "50";
}
String streetNoWidth1 = styleStreetNo.substring(styleStreetNo.indexOf("width"));
String streetNoWidth2 = streetNoWidth1.substring(0, streetNoWidth1.indexOf(";"));
String attrStreetNoWidth = streetNoWidth2.substring(streetNoWidth2.indexOf(":") + 2);
String streetNoWidth = attrStreetNoWidth.substring(0, attrStreetNoWidth.indexOf("px"));
//unit
Element unitHtml = document.getElementById("unit");
String styleUnit = unitHtml.attr("style");
String styleUnit1 = styleUnit.substring(styleUnit.indexOf("font-size"));
String styleUnit2 = styleUnit1.substring(0, styleUnit1.indexOf(";"));
String attrUnit = styleUnit2.substring(styleUnit2.indexOf(":") + 2);
String unitFontSize = attrUnit.substring(0, attrUnit.indexOf("px"));
//城市拼音div 寬高
Element cityHtml = document.getElementById("cityCode");
//獲取第一個div樣式
String styleCityCode = cityHtml.attr("style");
String styleCityCode1 = styleCityCode.substring(styleCityCode.indexOf("width"));
String styleCityCode2 = styleCityCode1.substring(0, styleCityCode1.indexOf(";"));
String attr3 = styleCityCode2.substring(styleCityCode2.indexOf(":") + 2);
String cityWidth = attr3.substring(0, attr3.indexOf("px"));
String styleCityCode3 = styleCityCode.substring(styleCityCode.indexOf("height"));
String styleCityCode4 = styleCityCode3.substring(0, styleCityCode3.indexOf(";"));
String attr4 = styleCityCode4.substring(styleCityCode4.indexOf(":") + 2);
String cityHeight = attr4.substring(0, attr4.indexOf("px"));
String styleCityCode11 = styleCityCode.substring(styleCityCode.indexOf("font-size"));
String styleCityCode22 = styleCityCode11.substring(0, styleCityCode11.indexOf(";"));
String attrCityCode = styleCityCode22.substring(styleCityCode22.indexOf(":") + 2);
String cityCodeFontSize = attrCityCode.substring(0, attrCityCode.indexOf("px"));
//二維碼div 寬高
Element qrcodeHtml = document.getElementById("qrcode");
//獲取第一個div樣式
String styleQrcode = qrcodeHtml.attr("style");
String styleQrcode1 = styleQrcode.substring(styleQrcode.indexOf("width"));
String styleQrcode2 = styleQrcode1.substring(0, styleQrcode1.indexOf(";"));
String attrCode = styleQrcode2.substring(styleQrcode2.indexOf(":") + 2);
String qrcodeWidth = attrCode.substring(0, attrCode.indexOf("px"));
String styleQrcode3 = styleQrcode.substring(styleQrcode.indexOf("height"));
String styleQrcode4 = styleQrcode3.substring(0, styleQrcode3.indexOf(";"));
String attrCode2 = styleQrcode4.substring(styleQrcode4.indexOf(":") + 2);
String qrcodeHeight = attrCode2.substring(0, attrCode2.indexOf("px"));
resultMap.put("width",width);
resultMap.put("height",height);
resultMap.put("cityWidth",cityWidth);
resultMap.put("cityHeight",cityHeight);
resultMap.put("qrcodeWidth",qrcodeWidth);
resultMap.put("qrcodeHeight",qrcodeHeight);
resultMap.put("streetFontSize",streetFontSize);
resultMap.put("subStreetFontSize",subStreetFontSize);
resultMap.put("subStreetNoFontSize",subStreetNoFontSize);
resultMap.put("streetNoFontSize",streetNoFontSize);
resultMap.put("unitFontSize",unitFontSize);
resultMap.put("cityCodeFontSize",cityCodeFontSize);
resultMap.put("streetNoWidth",streetNoWidth);
}else if(TaskPackageConstants.DOOR_CLASS_ROOM.equals(type)){
Document document = Jsoup.parse(html);
Element postList = document.getElementById("panelNode");
//獲取模板第一個div樣式
String style = postList.attr("style");
//獲取模板寬高
String style1 = style.substring(style.indexOf("width"));
String style2 = style1.substring(0, style1.indexOf(";"));
String attr = style2.substring(style2.indexOf(":") + 2);
String width = attr.substring(0, attr.indexOf("px"));
//獲取模板高
String style3 = style.substring(style.indexOf("height"));
String style4 = style3.substring(0, style3.indexOf(";"));
String attr1 = style4.substring(style4.indexOf(":") + 2);
String height = attr1.substring(0, attr.indexOf("px"));
//獲取模板第二個div
Element streetHtml = document.getElementById("street");
//獲取第2個div樣式
String styleStreet = streetHtml.attr("style");
String styleStreet1 = styleStreet.substring(styleStreet.indexOf("font-size"));
String styleStreet2 = styleStreet1.substring(0, styleStreet1.indexOf(";"));
String attr2 = styleStreet2.substring(styleStreet2.indexOf(":") + 2);
String streetFontSize = attr2.substring(0, attr2.indexOf("px"));
//room
Element roomHtml = document.getElementById("room");
String styleRoom = roomHtml.attr("style");
String styleRoom1 = styleRoom.substring(styleRoom.indexOf("font-size"));
String styleRoom2 = styleRoom1.substring(0, styleRoom1.indexOf(";"));
String attrRoom = styleRoom2.substring(styleRoom2.indexOf(":") + 2);
String roomFontSize = attrRoom.substring(0, attrRoom.indexOf("px"));
//城市拼音div 寬高
Element cityHtml = document.getElementById("cityCode");
//獲取第一個div樣式
String styleCityCode = cityHtml.attr("style");
String styleCityCode1 = styleCityCode.substring(styleCityCode.indexOf("width"));
String styleCityCode2 = styleCityCode1.substring(0, styleCityCode1.indexOf(";"));
String attr3 = styleCityCode2.substring(styleCityCode2.indexOf(":") + 2);
String cityWidth = attr3.substring(0, attr3.indexOf("px"));
String styleCityCode3 = styleCityCode.substring(styleCityCode.indexOf("height"));
String styleCityCode4 = styleCityCode3.substring(0, styleCityCode3.indexOf(";"));
String attr4 = styleCityCode4.substring(styleCityCode4.indexOf(":") + 2);
String cityHeight = attr4.substring(0, attr4.indexOf("px"));
String styleCityCode11 = styleCityCode.substring(styleCityCode.indexOf("font-size"));
String styleCityCode22 = styleCityCode11.substring(0, styleCityCode11.indexOf(";"));
String attrCityCode = styleCityCode22.substring(styleCityCode22.indexOf(":") + 2);
String cityCodeFontSize = attrCityCode.substring(0, attrCityCode.indexOf("px"));
//二維碼div 寬高
Element qrcodeHtml = document.getElementById("qrcode");
//獲取第一個div樣式
String styleQrcode = qrcodeHtml.attr("style");
String styleQrcode1 = styleQrcode.substring(styleQrcode.indexOf("width"));
String styleQrcode2 = styleQrcode1.substring(0, styleQrcode1.indexOf(";"));
String attrCode = styleQrcode2.substring(styleQrcode2.indexOf(":") + 2);
String qrcodeWidth = attrCode.substring(0, attrCode.indexOf("px"));
String styleQrcode3 = styleQrcode.substring(styleQrcode.indexOf("height"));
String styleQrcode4 = styleQrcode3.substring(0, styleQrcode3.indexOf(";"));
String attrCode2 = styleQrcode4.substring(styleQrcode4.indexOf(":") + 2);
String qrcodeHeight = attrCode2.substring(0, attrCode2.indexOf("px"));
resultMap.put("width",width);
resultMap.put("height",height);
resultMap.put("cityWidth",cityWidth);
resultMap.put("cityHeight",cityHeight);
resultMap.put("qrcodeWidth",qrcodeWidth);
resultMap.put("qrcodeHeight",qrcodeHeight);
resultMap.put("streetFontSize",streetFontSize);
resultMap.put("roomFontSize",roomFontSize);
resultMap.put("cityCodeFontSize",cityCodeFontSize);
}
return resultMap;
}
/**
* 獲取字型大小
* @param map
* @param type
* @return
*/
public static Map<String,String> getFontSize(Map<String, String> map, String type){
Map<String,String> resultMap = new HashMap<>(16);
Map<String,String> result = getHtmlContext(map,type);
String width = result.get("width");
String height = result.get("height");
String cityWidth = result.get("cityWidth");
String cityHeight = result.get("cityHeight");
String qrcodeWidth = result.get("qrcodeWidth");
String qrcodeHeight = result.get("qrcodeHeight");
String streetNoWidth = result.get("streetNoWidth");
String street = map.get("street");
String streetNo = map.get("streetNo");
String subStreet = map.get("subStreet");
String subStreetNo = map.get("subStreetNo");
String unit = map.get("unit");
String aoiGate = map.get("aoiGate");
String cityCode = map.get("cityCode");
String room = map.get("room");
resultMap.put("width",width);
resultMap.put("height",height);
resultMap.put("cityWidth",cityWidth);
resultMap.put("cityHeight",cityHeight);
resultMap.put("qrcodeWidth",qrcodeWidth);
resultMap.put("qrcodeHeight",qrcodeHeight);
resultMap.put("street",street);
resultMap.put("streetFontSize",result.get("streetFontSize"));
resultMap.put("streetNo",streetNo);
resultMap.put("streetNoFontSize",result.get("streetNoFontSize"));
resultMap.put("subStreet",subStreet);
resultMap.put("subStreetFontSize",result.get("subStreetFontSize"));
resultMap.put("subStreetNo",subStreetNo);
resultMap.put("subStreetNoFontSize",result.get("subStreetNoFontSize"));
resultMap.put("unit",unit);
resultMap.put("unitFontSize",result.get("unitFontSize"));
resultMap.put("aoiGate",aoiGate);
resultMap.put("aoiGateFontSize",result.get("aoiGateFontSize"));
resultMap.put("cityCode",cityCode);
resultMap.put("cityCodeFontSize",result.get("cityCodeFontSize"));
resultMap.put("room",room);
resultMap.put("roomFontSize",result.get("roomFontSize"));
resultMap.put("streetNoWidth",streetNoWidth);
return resultMap;
}
/**
* 設定圖片字型大小
* @param image
* @param width
* @param fontSizeStr
* @param str
*/
public static void setFontSize(BufferedImage image,String width,String fontSizeStr,String str){
Graphics2D g2d2 = image.createGraphics();
//字型最大 字尺寸
int fontSize = Double.valueOf(fontSizeStr).intValue();
//初始化一下字型
g2d2.setFont(new Font("宋體",Font.PLAIN,fontSize));
//圖片寬度限制
int xz = Double.valueOf(width).intValue();
int ss = g2d2.getFontMetrics().stringWidth(str);
while(g2d2.getFontMetrics().stringWidth(str)>xz) {
fontSize = fontSize-1;
if(fontSize < 0){
fontSize =10;
g2d2.setFont(new Font("宋體",Font.PLAIN,fontSize));
}
}
}
}
3.效果圖
三張圖片疊加在一起的(二維碼圖片,城市拼音圖片,底圖)
相關文章
- html頁面轉PDF、圖片操作記錄HTML
- JavaScript獲取背景圖片定位值JavaScript
- shopify 屬性新增圖片及樣式
- Unity從圖片的位元組資料裡面獲取圖片的寬和高Unity
- Chrome 獲取網頁顏色(文字、圖片)Chrome網頁
- 『ios』view和tableview的截圖和圖片拼接iOSView
- 織夢kindeditor編輯器圖片上傳增加圖片alt屬性和title屬性的方法
- DDGScreenShot —iOS 圖片處理--多圖片拼接 (swift)iOSSwift
- go語言實現簡單爬蟲獲取頁面圖片Go爬蟲
- 在讀取資料時拼接圖片域名
- HTML 連結和圖片HTML
- iOS 獲取視訊圖片iOS
- 獲取本地圖片/視訊地圖
- 【API】隨機獲取圖片API隨機
- Java 圖片裁剪,擷取Java
- 成品直播原始碼,JAVA獲取圖片的寬、高和大小原始碼Java
- Java 獲取Word批註所標記的文字和圖片Java
- python批量ppt轉圖片,pdf轉圖片,word轉圖片指令碼Python指令碼
- Java pdf轉高清圖片Java
- PDF 轉換圖片然後識別圖片內容
- 【HTML】04圖片HTML
- 獲取網路圖片的大小
- C#反射設定屬性值和獲取屬性值C#反射
- Java 讀取PDF中的文字和圖片Java
- HTML5獲取圖片的原始高度簡單介紹HTML
- Wordpress自動給圖片新增alt和title屬性
- bootstrap的圖片自適應屬性boot
- java 爬取網頁內容。 標題、圖片等Java網頁
- PDF 和圖片互轉
- python 爬蟲之requests爬取頁面圖片的url,並將圖片下載到本地Python爬蟲
- 頁面圖片自動滾動
- 使用httpclient下載 頁面、圖片HTTPclient
- html input type=file 選擇圖片,圖片預覽 純html js實現圖片預覽HTMLJS
- 圖片上傳及圖片處理
- 帝國cms內容頁圖片自動獲取alt和title的方法
- 直播帶貨小程式原始碼中,商品詳情頁是如何獲取html圖片的原始碼HTML
- python實現橫向拼接圖片Python
- node:爬蟲爬取網頁圖片爬蟲網頁