diff --git a/module-building-pictures/pom.xml b/module-building-pictures/pom.xml index 3b401e1..818c935 100644 --- a/module-building-pictures/pom.xml +++ b/module-building-pictures/pom.xml @@ -35,6 +35,17 @@ 5.3.3 compile + + + org.apache.httpcomponents + httpcore + 4.4.10 + + + org.apache.httpcomponents + httpclient + 4.5.6 + \ No newline at end of file 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 872a306..aab496e 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 @@ -8,6 +8,7 @@ import cn.com.tenlion.buildingpictures.service.picturestemplatebuilding.IPicture import cn.com.tenlion.buildingpictures.util.BaiDuMapUtil; import cn.com.tenlion.buildingpictures.util.BarCodeUtils; import cn.com.tenlion.buildingpictures.util.CreateImageBean; +import cn.com.tenlion.buildingpictures.util.WeiXinBarCodeUtils; import ink.wgink.common.base.DefaultBaseService; import ink.wgink.module.file.service.IFileService; import ink.wgink.pojo.pos.FilePO; @@ -118,6 +119,14 @@ public class PicturesTemplateBuildingServiceImpl extends DefaultBaseService impl // 将地图放入模板中 graphics.drawImage(map, bean.getX(), bean.getY(),null); } + // 小程序二维码 + if("6".equals(bean.getType())) { + BufferedImage weiXinImage = WeiXinBarCodeUtils.getBarCodeImage(bean.getContent(),bean.getColor()[0],bean.getColor()[1],bean.getColor()[2]); + // 对二维码压缩裁剪 + Image img = thumbnailBufferdImage(weiXinImage, bean.getWidth(), bean.getHeight()); + // 将头像放入模板中 + graphics.drawImage(img, bean.getX(), bean.getY(),null); + } } return image; } @@ -225,6 +234,14 @@ public class PicturesTemplateBuildingServiceImpl extends DefaultBaseService impl // 将地图放入模板中 graphics.drawImage(map, bean.getX(), bean.getY(),null); } + // 小程序二维码 + if("6".equals(bean.getType())) { + BufferedImage weiXinImage = WeiXinBarCodeUtils.getBarCodeImage(bean.getContent(),bean.getColor()[0],bean.getColor()[1],bean.getColor()[2]); + // 对二维码压缩裁剪 + Image img = thumbnailBufferdImage(weiXinImage, bean.getWidth(), bean.getHeight()); + // 将头像放入模板中 + graphics.drawImage(img, bean.getX(), bean.getY(),null); + } } ByteArrayOutputStream stream = new ByteArrayOutputStream(); ImageIO.write(image, "png", stream); @@ -365,6 +382,28 @@ public class PicturesTemplateBuildingServiceImpl extends DefaultBaseService impl } } + /** + * 图片压缩-按照固定宽高原图压缩 + * @Title : thumbnail + * @功能描述 : TODO + * @设定文件 : @param img 本地图片地址 + * @设定文件 : @param width 图片宽度 + * @设定文件 : @param height 图片高度 + * @设定文件 : @return + * @设定文件 : @throws IOException + * @返回类型 : Image + * @throws : + */ + private static Image thumbnailBufferdImage(BufferedImage BI, int width, int height) throws IOException { + Image image = BI.getScaledInstance(width, height, Image.SCALE_SMOOTH); + BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + Graphics g = tag.getGraphics(); + g.setColor(Color.RED); + g.drawImage(image, 0, 0, null); + g.dispose(); + return image; + } + /** * 图片压缩-按照固定宽高原图压缩 * @Title : thumbnail diff --git a/module-building-pictures/src/main/java/cn/com/tenlion/buildingpictures/util/HttpUtil.java b/module-building-pictures/src/main/java/cn/com/tenlion/buildingpictures/util/HttpUtil.java new file mode 100644 index 0000000..33fa823 --- /dev/null +++ b/module-building-pictures/src/main/java/cn/com/tenlion/buildingpictures/util/HttpUtil.java @@ -0,0 +1,106 @@ +package cn.com.tenlion.buildingpictures.util; + +import org.apache.http.Consts; +import org.apache.http.HttpEntity; +import org.apache.http.NameValuePair; +import org.apache.http.client.entity.UrlEncodedFormEntity; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.message.BasicNameValuePair; +import org.apache.http.util.EntityUtils; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +public class HttpUtil { + + private static final CloseableHttpClient httpclient = HttpClients.createDefault(); + + /** + * 发送HttpGet请求 + * @param url + * @return + */ + public static String sendGet(String url) { + HttpGet httpget = new HttpGet(url); + CloseableHttpResponse response = null; + try { + response = httpclient.execute(httpget); + } catch (IOException e1) { + e1.printStackTrace(); + } + String result = null; + try { + HttpEntity entity = response.getEntity(); + if (entity != null) { + result = EntityUtils.toString(entity); + } + } catch (Exception e) { + e.printStackTrace(); + } finally { + try { + response.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return result; + } + + /** + * 发送HttpPost请求,参数为map + * @param url + * @param map + * @return + */ + public static String sendPost(String url, Map map) { + List formparams = new ArrayList(); + for (Map.Entry entry : map.entrySet()) { + formparams.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); + } + UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8); + HttpPost httppost = new HttpPost(url); + httppost.setEntity(entity); + CloseableHttpResponse response = null; + try { + response = httpclient.execute(httppost); + } catch (IOException e) { + e.printStackTrace(); + } + HttpEntity entity1 = response.getEntity(); + String result = null; + try { + result = EntityUtils.toString(entity1); + } catch (Exception e) { + e.printStackTrace(); + } + return result; + } + + /** + * 发送不带参数的HttpPost请求 + * @param url + * @return + */ + public static String sendPost(String url) { + HttpPost httppost = new HttpPost(url); + CloseableHttpResponse response = null; + try { + response = httpclient.execute(httppost); + } catch (IOException e) { + e.printStackTrace(); + } + HttpEntity entity = response.getEntity(); + String result = null; + try { + result = EntityUtils.toString(entity); + } catch (Exception e) { + e.printStackTrace(); + } + return result; + } +} \ No newline at end of file diff --git a/module-building-pictures/src/main/java/cn/com/tenlion/buildingpictures/util/WeiXinBarCodeUtils.java b/module-building-pictures/src/main/java/cn/com/tenlion/buildingpictures/util/WeiXinBarCodeUtils.java new file mode 100644 index 0000000..2b8a91d --- /dev/null +++ b/module-building-pictures/src/main/java/cn/com/tenlion/buildingpictures/util/WeiXinBarCodeUtils.java @@ -0,0 +1,191 @@ +package cn.com.tenlion.buildingpictures.util; + +import com.alibaba.fastjson.JSONObject; +import com.google.zxing.BarcodeFormat; +import com.google.zxing.MultiFormatWriter; +import com.google.zxing.common.BitMatrix; + +import java.awt.*; +import java.io.*; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.util.HashMap; +import java.util.Map; + +/** + * 生成微信小程序二维码 + */ +public class WeiXinBarCodeUtils { + + /** + * 生成小程序二维码 + * @param params 携带参数 + * @return + * @throws IOException + */ + public static BufferedImage getBarCodeImage(String params, int colorR, int colorG, int colorB) throws IOException { + Map paramsMap = new HashMap(); + String[] paramsArray = params.split("&"); + for (String param : paramsArray) { + String[] paramData = param.split("="); + paramsMap.put(paramData[0], paramData[1]); + } + String accessToken = getAccessToken(paramsMap.get("appid"), paramsMap.get("secret")); + URL url = new URL("https://api.weixin.qq.com/wxa/getwxacode?access_token=" + accessToken); + HttpURLConnection conn = (HttpURLConnection)url.openConnection(); + // 发送POST请求必须设置如下两行 + conn.setDoOutput(true); + conn.setDoInput(true); + //设置超时间为5秒 + conn.setConnectTimeout(5*1000); + //防止屏蔽程序抓取而返回403错误 + conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); + conn.connect(); + StringBuffer buffer = new StringBuffer(); + for (Map.Entry k : paramsMap.entrySet()) { + if(!"path".equals(k.getKey()) && !"appid".equals(k.getKey()) && !"secret".equals(k.getKey())) { + if (buffer.length() < 1) { + buffer.append( k.getKey() + "=" + k.getValue()); + }else { + buffer.append("&" + k.getKey() + "=" + k.getValue()); + } + } + } + String param = buffer.toString(); + //POST请求 + DataOutputStream out = new DataOutputStream(conn.getOutputStream()); + JSONObject obj = new JSONObject(); + obj.put("path", paramsMap.get("path") + "?" + param); + obj.put("width", 400); + +// JSONObject colorObject = new JSONObject(); +// colorObject.put("r", colorR + ""); +// colorObject.put("g", colorG + ""); +// colorObject.put("b", colorB + ""); +// obj.put("auto_color", true); +// obj.put("line_color", colorObject); + + out.writeBytes(obj.toString()); + out.flush(); + out.close(); + + //得到输入流 + InputStream inputStream = conn.getInputStream(); + //获取自己数组 + byte[] getData = readInputStream(inputStream); + BufferedImage image = ImageIO.read(new ByteArrayInputStream(getData)); + + + Color color = new Color(colorR, colorG, colorB); + // 设置背景为透明颜色 + BufferedImage weiXinImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB); + Graphics2D graphics = weiXinImage.createGraphics(); + //消除文字锯齿 + graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + //消除图片锯齿 + graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + + weiXinImage = graphics.getDeviceConfiguration().createCompatibleImage(image.getWidth(), image.getHeight(), Transparency.TRANSLUCENT); + Graphics imageGraphics = weiXinImage.getGraphics(); + + for (int x = 0; x < weiXinImage.getWidth(); x++) { + for (int y = 0; y < weiXinImage.getHeight(); y++) { + int clr = image.getRGB(x, y); + // 获得像素对象 + Object data = image.getRaster().getDataElements(x, y, null); + int red = image.getColorModel().getRed(data); + int blue = image.getColorModel().getBlue(data); + int green = image.getColorModel().getGreen(data); + if(red < 250 && blue < 250 && green < 250) { + if(red == 0 && blue == 0 && green == 0 ) { + // 自定义颜色 + imageGraphics.setColor(color); + imageGraphics.drawRect( x, y, 1, 1); + }else { + // 原来的颜色 + imageGraphics.setColor(new Color(clr)); + imageGraphics.drawRect ( x, y, 1, 1); + } + } + } + } + ImageIO.write(image, "jpg", new File("D:\\TEST1.jpg")); + ImageIO.write(weiXinImage, "jpg", new File("D:\\TEST2.jpg")); + return weiXinImage; + } + + /** + * 生成小程序二维码 + * @param appId appId + * @param secret secret + * @param params 携带参数 + * @param width 尺寸 + * @return + * @throws IOException + */ + public static BufferedImage getBarCodeImage(String appId, String secret, Map params, Integer width) throws IOException { + String accessToken = getAccessToken(appId, secret); + URL url = new URL("https://api.weixin.qq.com/wxa/getwxacode?access_token=" + accessToken); + HttpURLConnection conn = (HttpURLConnection)url.openConnection(); + // 发送POST请求必须设置如下两行 + conn.setDoOutput(true); + conn.setDoInput(true); + //设置超时间为5秒 + conn.setConnectTimeout(5*1000); + //防止屏蔽程序抓取而返回403错误 + conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); + conn.connect(); + StringBuffer buffer = new StringBuffer(); + for (Map.Entry k : params.entrySet()) { + buffer.append("&" + k.getKey() + "=" + k.getValue()); + } + String param = buffer.toString(); + //POST请求 + DataOutputStream out = new DataOutputStream(conn.getOutputStream()); + JSONObject obj = new JSONObject(); + obj.put("path", "pages/productdetail/productdetail?" + param ); + obj.put("width", width); + + out.writeBytes(obj.toString()); + out.flush(); + out.close(); + + //得到输入流 + InputStream inputStream = conn.getInputStream(); + //获取自己数组 + byte[] getData = readInputStream(inputStream); + BufferedImage image = ImageIO.read(new ByteArrayInputStream(getData)); + return image; + } + + 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(); + } + + /** + * 获取accessToken + * @param appId + * @param secret + * @return + */ + private static String getAccessToken(String appId, String secret) { + String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + + appId + + "&secret=" + + secret; + String result = HttpUtil.sendGet(url); + JSONObject object = JSONObject.parseObject(result); + String Access_Token = object.getString("access_token"); + return Access_Token; + } +} diff --git a/module-building-pictures/src/main/resources/templates/picturestemplate/save-picturestemplatearea.html b/module-building-pictures/src/main/resources/templates/picturestemplate/save-picturestemplatearea.html index 184ecc4..3aa4ba2 100644 --- a/module-building-pictures/src/main/resources/templates/picturestemplate/save-picturestemplatearea.html +++ b/module-building-pictures/src/main/resources/templates/picturestemplate/save-picturestemplatearea.html @@ -290,8 +290,8 @@ 'y': e.pageY }; $.extend(document, {'move': true, 'call_down': function(e) { - var width = Math.max(30, e.pageX - posix.x + posix.w); - var height = Math.max(30, e.pageY - posix.y + posix.h); + var width = Math.max(10, e.pageX - posix.x + posix.w); + var height = Math.max(10, e.pageY - posix.y + posix.h); if(width > dataFormData.templateWidth) { layer.msg("宽度超出范围") return; @@ -301,8 +301,8 @@ return; } $box.css({ - 'width': Math.max(30, e.pageX - posix.x + posix.w), - 'height': Math.max(30, e.pageY - posix.y + posix.h) + 'width': Math.max(10, e.pageX - posix.x + posix.w), + 'height': Math.max(10, e.pageY - posix.y + posix.h) }); }}); e.stopPropagation(); 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 150b120..f26f91f 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 @@ -63,6 +63,7 @@ + @@ -70,19 +71,19 @@ {{# if(d != '2') { }} - {{ d == 3 ? '条形码数据' : d == 4 ? '二维码数据' : d == 5 ? '地图位置' : '示例文字'}}* + {{ d == 3 ? '条形码数据' : d == 4 ? '二维码数据' : d == 5 ? '地图位置' : d == 6 ? '携带参数' : '示例文字'}}* {{# if(d == '1') { }} {{# } else { }} - + {{# } }} - {{ d == 3 ? '条形码' : d == 4 ? '二维码' : d == 5 ? '地图' : '文字'}}颜色* + {{ d == 3 ? '条形码' : d == 4 ? '二维码' : d == 5 ? '地图' : d == 6 ? '二维码' : '文字'}}颜色* diff --git a/module-building-pictures/src/main/resources/templates/picturestemplatearea/update-picturestemplatearea.html b/module-building-pictures/src/main/resources/templates/picturestemplatearea/update-picturestemplatearea.html index 0eb745a..5e2898b 100644 --- a/module-building-pictures/src/main/resources/templates/picturestemplatearea/update-picturestemplatearea.html +++ b/module-building-pictures/src/main/resources/templates/picturestemplatearea/update-picturestemplatearea.html @@ -63,6 +63,8 @@ + + @@ -70,19 +72,19 @@ {{# if(d != '2') { }} - {{ d == 3 ? '条形码数据' : d == 4 ? '二维码数据' : d == 5 ? '地图位置' : '示例文字'}}* + {{ d == 3 ? '条形码数据' : d == 4 ? '二维码数据' : d == 5 ? '地图位置' : d == 6 ? '携带参数' : '示例文字'}}* {{# if(d == '1') { }} {{# } else { }} - + {{# } }} - {{ d == 3 ? '条形码' : d == 4 ? '二维码' : '文字'}}颜色* + {{ d == 3 ? '条形码' : d == 4 ? '二维码' : d == 5 ? '地图' : d == 6 ? '二维码' : '文字'}}颜色* @@ -94,7 +96,7 @@ 文字字体* - + @@ -114,7 +116,7 @@ 字体大小* - +