diff --git a/module-building-pictures/src/main/java/cn/com/tenlion/buildingpictures/service/picturestemplatebuilding/impl/PicturesTemplateBuildingServiceImpl.java b/module-building-pictures/src/main/java/cn/com/tenlion/buildingpictures/service/picturestemplatebuilding/impl/PicturesTemplateBuildingServiceImpl.java index c660aac..872a306 100644 --- a/module-building-pictures/src/main/java/cn/com/tenlion/buildingpictures/service/picturestemplatebuilding/impl/PicturesTemplateBuildingServiceImpl.java +++ b/module-building-pictures/src/main/java/cn/com/tenlion/buildingpictures/service/picturestemplatebuilding/impl/PicturesTemplateBuildingServiceImpl.java @@ -5,11 +5,14 @@ import cn.com.tenlion.buildingpictures.pojo.dtos.picturestemplatearea.PicturesTe import cn.com.tenlion.buildingpictures.service.picturestemplate.IPicturesTemplateService; import cn.com.tenlion.buildingpictures.service.picturestemplatearea.IPicturesTemplateAreaService; import cn.com.tenlion.buildingpictures.service.picturestemplatebuilding.IPicturesTemplateBuildingService; +import cn.com.tenlion.buildingpictures.util.BaiDuMapUtil; +import cn.com.tenlion.buildingpictures.util.BarCodeUtils; import cn.com.tenlion.buildingpictures.util.CreateImageBean; import ink.wgink.common.base.DefaultBaseService; import ink.wgink.module.file.service.IFileService; import ink.wgink.pojo.pos.FilePO; import ink.wgink.properties.FileProperties; +import ink.wgink.util.QRCodeUtil; import org.apache.commons.codec.binary.Base64; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Primary; @@ -50,7 +53,13 @@ public class PicturesTemplateBuildingServiceImpl extends DefaultBaseService impl // 创建画布 BufferedImage image = new BufferedImage(backWidth, backHeight, Image.SCALE_SMOOTH); // 得到图片操作对象 - Graphics graphics = image.getGraphics(); + Graphics2D graphics = image.createGraphics(); + + //消除文字锯齿 + graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + //消除图片锯齿 + graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + // 对底图进行裁剪 Image backImg = thumbnail(backFile, backWidth + 1, backHeight); // 将底图放入模板中 @@ -91,16 +100,65 @@ public class PicturesTemplateBuildingServiceImpl extends DefaultBaseService impl // 将头像放入模板中 graphics.drawImage(img, bean.getX(), bean.getY(),null); } + // 条形码 + if("3".equals(bean.getType())) { + BufferedImage barcode = BarCodeUtils.getBarcode(bean.getContent(), bean.getWidth(), bean.getHeight(), new Color(bean.getColor()[0],bean.getColor()[1],bean.getColor()[2])); + // 将条形码放入模板中 + graphics.drawImage(barcode, bean.getX(), bean.getY(),null); + } + // 二维码 + if("4".equals(bean.getType())) { + BufferedImage rq = BarCodeUtils.getRQ(bean.getContent(), bean.getWidth(), bean.getHeight(), new Color(bean.getColor()[0],bean.getColor()[1],bean.getColor()[2])); + // 将二维码放入模板中 + graphics.drawImage(rq, bean.getX(), bean.getY(),null); + } + // 地图坐标 + if("5".equals(bean.getType())) { + BufferedImage map = BaiDuMapUtil.getImage(bean.getContent(), bean.getWidth(), bean.getHeight()); + // 将地图放入模板中 + graphics.drawImage(map, bean.getX(), bean.getY(),null); + } } return image; } + public List createText(Integer width, Integer fontSize, String content) { + char[] c = content.toCharArray(); + double count = 0; + double rowCount = width / fontSize; + List contentList = new ArrayList(); + StringBuffer rowContent = new StringBuffer(); + for(int i = 0; i < c.length; i ++) { + String len = Integer.toBinaryString(c[i]); + rowContent.append(c[i]); + + if(len.length() > 8) { + count++; + }else{ + count = (count + 0.5); + } + + if((count+1) >= rowCount || i == c.length-1 || '\n' == c[i]) { + count = 0; + contentList.add(rowContent.toString().replace("\n", "")); + rowContent.setLength(0); + } + } + return contentList; + } + @Override public String createImage(List list, File backFile, int backWidth, int backHeight) throws IOException { // 创建画布 BufferedImage image = new BufferedImage(backWidth, backHeight, Image.SCALE_SMOOTH); // 得到图片操作对象 - Graphics graphics = image.getGraphics(); + Graphics2D graphics = image.createGraphics(); + + //消除文字锯齿 + graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + //消除图片锯齿 + graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + // 对底图进行裁剪 Image backImg = thumbnail(backFile, backWidth + 1, backHeight); // 将底图放入模板中 @@ -126,7 +184,15 @@ public class PicturesTemplateBuildingServiceImpl extends DefaultBaseService impl // 左侧 }else{ // 将文字放入模板中 - graphics.drawString(bean.getContent(), bean.getX(), ( bean.getY() + ( bean.getHeight() / 2) + ( bean.getFontSize() / 2) ) - 3); + List rowList = createText(bean.getWidth(), bean.getFontSize(), bean.getContent()); + int index = 0; + for (String fontContent : rowList) { + /** + * 对内容进行封装换行 + */ + graphics.drawString(fontContent, bean.getX(), bean.getY() + (bean.getFontSize() * index) + 10); + index++; + } } } // 图片 @@ -141,6 +207,24 @@ public class PicturesTemplateBuildingServiceImpl extends DefaultBaseService impl // 将头像放入模板中 graphics.drawImage(img, bean.getX(), bean.getY(),null); } + // 条形码 + if("3".equals(bean.getType())) { + BufferedImage barcode = BarCodeUtils.getBarcode(bean.getContent(), bean.getWidth(), bean.getHeight(), new Color(bean.getColor()[0],bean.getColor()[1],bean.getColor()[2])); + // 将条形码放入模板中 + graphics.drawImage(barcode, bean.getX(), bean.getY(),null); + } + // 二维码 + if("4".equals(bean.getType())) { + BufferedImage rq = BarCodeUtils.getRQ(bean.getContent(), bean.getWidth(), bean.getHeight(), new Color(bean.getColor()[0],bean.getColor()[1],bean.getColor()[2])); + // 将二维码放入模板中 + graphics.drawImage(rq, bean.getX(), bean.getY(),null); + } + // 地图坐标 + if("5".equals(bean.getType())) { + BufferedImage map = BaiDuMapUtil.getImage(bean.getContent(), bean.getWidth(), bean.getHeight()); + // 将地图放入模板中 + graphics.drawImage(map, bean.getX(), bean.getY(),null); + } } ByteArrayOutputStream stream = new ByteArrayOutputStream(); ImageIO.write(image, "png", stream); diff --git a/module-building-pictures/src/main/java/cn/com/tenlion/buildingpictures/util/BaiDuMapUtil.java b/module-building-pictures/src/main/java/cn/com/tenlion/buildingpictures/util/BaiDuMapUtil.java new file mode 100644 index 0000000..5db5efe --- /dev/null +++ b/module-building-pictures/src/main/java/cn/com/tenlion/buildingpictures/util/BaiDuMapUtil.java @@ -0,0 +1,49 @@ +package cn.com.tenlion.buildingpictures.util; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.*; +import java.net.HttpURLConnection; +import java.net.URL; + +/** + * 百度地图相关工具类 + * ClassName: BaiDuMapUtil + * @Description: TODO + * 创建工具: MyEclipse2014 + * 运行环境: [Tomcat7以上,MySql5.6以上,JDK7以上] + * @author 崔宝铖 + * @date 2019年6月19日 + */ +public class BaiDuMapUtil { + + /** 百度地图开发Key */ + private static String ak = "VchhMAkuOzvkaI0NcZSNNippil968u9G"; + + public static BufferedImage getImage(String center, Integer width, Integer height) throws IOException { + URL url = new URL("http://api.map.baidu.com/staticimage/v2?ak=" + ak + "&width=" + width + "&height=" + ( height + 15) + "¢er=" + center + "&zoom=15©right=1"); + HttpURLConnection conn = (HttpURLConnection)url.openConnection(); + //设置超时间为5秒 + conn.setConnectTimeout(5*1000); + //防止屏蔽程序抓取而返回403错误 + conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); + //得到输入流 + InputStream inputStream = conn.getInputStream(); + //获取自己数组 + byte[] getData = readInputStream(inputStream); + BufferedImage image = ImageIO.read(new ByteArrayInputStream(getData)); + return image.getSubimage(0, 0, width, height); + } + + private static byte[] readInputStream(InputStream inputStream) throws IOException { + byte[] buffer = new byte[1024]; + int len = 0; + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + while((len = inputStream.read(buffer)) != -1) { + bos.write(buffer, 0, len); + } + bos.close(); + return bos.toByteArray(); + } + +} diff --git a/module-building-pictures/src/main/java/cn/com/tenlion/buildingpictures/util/BarCodeUtils.java b/module-building-pictures/src/main/java/cn/com/tenlion/buildingpictures/util/BarCodeUtils.java new file mode 100644 index 0000000..fdbba59 --- /dev/null +++ b/module-building-pictures/src/main/java/cn/com/tenlion/buildingpictures/util/BarCodeUtils.java @@ -0,0 +1,146 @@ +package cn.com.tenlion.buildingpictures.util; + +import com.google.zxing.*; +import com.google.zxing.common.BitMatrix; +import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; +import com.google.zxing.qrcode.encoder.QRCode; + +import javax.imageio.ImageIO; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.ByteArrayOutputStream; +import java.io.OutputStream; +import java.util.Hashtable; + +/** + * 创建二维码工具类 + * ClassName: BarCodeUtils + * @Description: TODO + * 创建工具: MyEclipse2014 + * 运行环境: [Tomcat7以上,MySql5.6以上,JDK7以上] + * @author 崔宝铖 + * @date 2019年3月1日 + */ +public class BarCodeUtils { + + public static String CODE = "utf-8"; + public static int SUBCODE = 10; // 取uuid的前10位 + public static int BLACK = 0xff000000; + public static int WHITE = 0xFFFFFFFF; + public static int WIDTH = 180; + public static int HEIGHT = 100; + + /** + * 生成RQ二维码 + * + * @param str 内容 + * @param height 高度(px) + * @author wuhongbo + */ + public static BufferedImage getRQ(String str, Integer width, Integer height, Color color) { + try { + /** 定义Map集合封装二维码配置信息 */ + Hashtable hints = new Hashtable(); + /** 设置二维码图片的内容编码 */ + hints.put(EncodeHintType.CHARACTER_SET, CODE); + /** 设置二维码的纠错级别 */ + hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); + /** 设置二维码图片的上、下、左、右间隙 */ + hints.put(EncodeHintType.MARGIN, 0); + /** + * 创建二维码字节转换对象 + * 第一个参数:二维码图片中的内容 + * 第二个参数:二维码格式器 + * 第三个参数:生成二维码图片的宽度 + * 第四个参数:生成二维码图片的高度 + * 第五个参数:生成二维码需要配置信息 + * */ + BitMatrix bitMatrix = new MultiFormatWriter().encode(str, BarcodeFormat.QR_CODE, width, height, hints); + + BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + // 设置背景为透明颜色 + Graphics2D graphics = image.createGraphics(); + image = graphics.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT); + Graphics imageGraphics = image.getGraphics(); + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + if(bitMatrix.get(x, y)) { + // 自定义颜色 + imageGraphics.setColor(color); + imageGraphics.drawRect ( x, y, 1, 1); + } + } + } + return image; + // return toBufferedImage(bitMatrix); + // 输出方式 + // 网页 + // ImageIO.write(image, "png", response.getOutputStream()); + // 文件 + // ImageIO.write(image, "png", file); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + /** + * 生成一维码(128) + * + * @param str + * @param width + * @param height + * @return + * @author wuhongbo + */ + public static BufferedImage getBarcode(String str, Integer width, Integer height, Color color) { + try { + // 文字编码 + Hashtable hints = new Hashtable(); + hints.put(EncodeHintType.CHARACTER_SET, CODE); + hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); + hints.put(EncodeHintType.MARGIN, 0); // 一维码的大小 + BitMatrix bitMatrix = new MultiFormatWriter().encode(str, BarcodeFormat.CODE_128, width, height, hints); + + BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + // 设置背景为透明颜色 + Graphics2D graphics = image.createGraphics(); + image = graphics.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT); + Graphics imageGraphics = image.getGraphics(); + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + if(bitMatrix.get(x, y)) { + // 自定义颜色 + imageGraphics.setColor(color); + imageGraphics.drawRect ( x, y, 1, 1); + } + } + } + return image; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + + /** + * 转换成图片 + * + * @param matrix + * @return + * @author wuhongbo + */ + private static BufferedImage toBufferedImage(BitMatrix matrix) { + int width = matrix.getWidth(); + int height = matrix.getHeight(); + BufferedImage image = new BufferedImage(width, height, + BufferedImage.TYPE_INT_ARGB); + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE); + } + } + return image; + } + +} diff --git a/module-building-pictures/src/main/resources/templates/picturestemplatearea/save-picturestemplatearea.html b/module-building-pictures/src/main/resources/templates/picturestemplatearea/save-picturestemplatearea.html index fb9238a..150b120 100644 --- a/module-building-pictures/src/main/resources/templates/picturestemplatearea/save-picturestemplatearea.html +++ b/module-building-pictures/src/main/resources/templates/picturestemplatearea/save-picturestemplatearea.html @@ -62,22 +62,27 @@ +