diff --git a/src/main/java/com/lovenav/controller/QRCodeController.java b/src/main/java/com/lovenav/controller/QRCodeController.java index 5c4e0a7..3865f0d 100644 --- a/src/main/java/com/lovenav/controller/QRCodeController.java +++ b/src/main/java/com/lovenav/controller/QRCodeController.java @@ -1,27 +1,25 @@ package com.lovenav.controller; import com.alibaba.fastjson.JSON; +import com.lovenav.entity.CollectIconList; +import com.lovenav.service.QRCService; import com.lovenav.utils.QRCodeUtil; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; -@RestController +import java.util.Collection; +@RestController +@RequestMapping("/qrcode") public class QRCodeController { - private QRCodeUtil qrCodeUtil; -@RequestMapping(method = RequestMethod.GET, value = "/qrc") - public String QRCode(String url){ - String text = url; - String logoPath ="src/main/resources/static/logo/NAV.png"; - String destPath = "src/main/resources/static/qr"; - try { - String url2 = qrCodeUtil.encode(text, logoPath, destPath, true); - String base64 = qrCodeUtil.getBase64(url2); - return JSON.toJSONString(base64); - } catch (Exception e) { - throw new RuntimeException(e); - } + @Autowired + private QRCService qrcService; + + @RequestMapping(method = RequestMethod.POST, value = "/qrc_return") + public String QRCode(@RequestBody CollectIconList collect) throws Exception { + return qrcService.QR(collect); } -} +} \ No newline at end of file diff --git a/src/main/java/com/lovenav/dao/CollectIconListDao.java b/src/main/java/com/lovenav/dao/CollectIconListDao.java index 8b25ea7..b115607 100644 --- a/src/main/java/com/lovenav/dao/CollectIconListDao.java +++ b/src/main/java/com/lovenav/dao/CollectIconListDao.java @@ -1,9 +1,15 @@ package com.lovenav.dao; import com.lovenav.entity.CollectIconList; +import org.apache.ibatis.annotations.Mapper; +import org.mybatis.spring.annotation.MapperScan; import org.springframework.stereotype.Repository; + +import java.util.List; + //dao @Repository +@Mapper public interface CollectIconListDao { int deleteByPrimaryKey(Long id); @@ -16,4 +22,6 @@ public interface CollectIconListDao { int updateByPrimaryKeySelective(CollectIconList record); int updateByPrimaryKey(CollectIconList record); + + List selectByUrlid(int urlid); } \ No newline at end of file diff --git a/src/main/java/com/lovenav/dao/UrlListDao.java b/src/main/java/com/lovenav/dao/UrlListDao.java index 6492868..e1189fc 100644 --- a/src/main/java/com/lovenav/dao/UrlListDao.java +++ b/src/main/java/com/lovenav/dao/UrlListDao.java @@ -3,8 +3,8 @@ package com.lovenav.dao; import com.lovenav.entity.UrlList; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; +import org.mybatis.spring.annotation.MapperScan; import org.springframework.stereotype.Repository; - import java.util.List; @Repository @@ -29,4 +29,5 @@ public interface UrlListDao { public UrlList selectUrlListByUrlId(Long urlId); + } \ No newline at end of file diff --git a/src/main/java/com/lovenav/entity/CollectIconList.java b/src/main/java/com/lovenav/entity/CollectIconList.java index b140c2d..96a15b4 100644 --- a/src/main/java/com/lovenav/entity/CollectIconList.java +++ b/src/main/java/com/lovenav/entity/CollectIconList.java @@ -10,12 +10,55 @@ import lombok.Data; */ @Data public class CollectIconList implements Serializable { + CollectIconList(){ + this.status = 0; + } + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Integer getUrl_id() { + return url_id; + } + + public void setUrl_id(int url_id) { + this.url_id = url_id; + } + + public String getIcon_url() { + return icon_url; + } + + public void setIcon_url(String icon_url) { + this.icon_url = icon_url; + } + + public String getQr_url() { + return qr_url; + } + + public void setQr_url(String qr_url) { + this.qr_url = qr_url; + } + + public Integer getStatus() { + return status; + } + + public void setStatus(Integer status) { + this.status = status; + } + private Long id; /** * 网址id */ - private String url_id; + private Integer url_id; /** * 图片地址 diff --git a/src/main/java/com/lovenav/entity/UrlCateList.java b/src/main/java/com/lovenav/entity/UrlCateList.java index b5a6599..514a367 100644 --- a/src/main/java/com/lovenav/entity/UrlCateList.java +++ b/src/main/java/com/lovenav/entity/UrlCateList.java @@ -8,6 +8,86 @@ import lombok.Data; */ @Data public class UrlCateList implements Serializable { + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getCreatetime() { + return createtime; + } + + public void setCreatetime(Long createtime) { + this.createtime = createtime; + } + + public Long getUrlNumber() { + return urlNumber; + } + + public void setUrlNumber(Long urlNumber) { + this.urlNumber = urlNumber; + } + + public Long getWeigh() { + return weigh; + } + + public void setWeigh(Long weigh) { + this.weigh = weigh; + } + + public Byte getStatus() { + return status; + } + + public void setStatus(Byte status) { + this.status = status; + } + + public Byte getNeedLogin() { + return needLogin; + } + + public void setNeedLogin(Byte needLogin) { + this.needLogin = needLogin; + } + + public Integer getUserId() { + return userId; + } + + public void setUserId(Integer userId) { + this.userId = userId; + } + + public Integer getRootCateId() { + return rootCateId; + } + + public void setRootCateId(Integer rootCateId) { + this.rootCateId = rootCateId; + } + + public String getIco() { + return ico; + } + + public void setIco(String ico) { + this.ico = ico; + } + /** * id */ diff --git a/src/main/java/com/lovenav/entity/UrlList.java b/src/main/java/com/lovenav/entity/UrlList.java index bce8c64..f7bc354 100644 --- a/src/main/java/com/lovenav/entity/UrlList.java +++ b/src/main/java/com/lovenav/entity/UrlList.java @@ -8,6 +8,142 @@ import lombok.Data; */ @Data public class UrlList implements Serializable { + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getIcon() { + return icon; + } + + public void setIcon(String icon) { + this.icon = icon; + } + + public Integer getCateId() { + return cateId; + } + + public void setCateId(Integer cateId) { + this.cateId = cateId; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public Long getCreatetime() { + return createtime; + } + + public void setCreatetime(Long createtime) { + this.createtime = createtime; + } + + public Long getViews() { + return views; + } + + public void setViews(Long views) { + this.views = views; + } + + public String getTags() { + return tags; + } + + public void setTags(String tags) { + this.tags = tags; + } + + public Long getWeigh() { + return weigh; + } + + public void setWeigh(Long weigh) { + this.weigh = weigh; + } + + public Byte getStatus() { + return status; + } + + public void setStatus(Byte status) { + this.status = status; + } + + public String getDesc() { + return desc; + } + + public void setDesc(String desc) { + this.desc = desc; + } + + public Byte getNeedLogin() { + return needLogin; + } + + public void setNeedLogin(Byte needLogin) { + this.needLogin = needLogin; + } + + public String getAgentHint() { + return agentHint; + } + + public void setAgentHint(String agentHint) { + this.agentHint = agentHint; + } + + public Byte getIsNeedAgent() { + return isNeedAgent; + } + + public void setIsNeedAgent(Byte isNeedAgent) { + this.isNeedAgent = isNeedAgent; + } + + public Byte getIsAd() { + return isAd; + } + + public void setIsAd(Byte isAd) { + this.isAd = isAd; + } + + public Byte getIsTop() { + return isTop; + } + + public void setIsTop(Byte isTop) { + this.isTop = isTop; + } + + public Byte getIsEncrypt() { + return isEncrypt; + } + + public void setIsEncrypt(Byte isEncrypt) { + this.isEncrypt = isEncrypt; + } + /** * id */ diff --git a/src/main/java/com/lovenav/service/QRCService.java b/src/main/java/com/lovenav/service/QRCService.java new file mode 100644 index 0000000..850cc98 --- /dev/null +++ b/src/main/java/com/lovenav/service/QRCService.java @@ -0,0 +1,7 @@ +package com.lovenav.service; + +import com.lovenav.entity.CollectIconList; + +public interface QRCService { + public String QR(CollectIconList collect) throws Exception; +} diff --git a/src/main/java/com/lovenav/service/serviceImpl/QRCServiceImpl.java b/src/main/java/com/lovenav/service/serviceImpl/QRCServiceImpl.java new file mode 100644 index 0000000..b7c1c8e --- /dev/null +++ b/src/main/java/com/lovenav/service/serviceImpl/QRCServiceImpl.java @@ -0,0 +1,49 @@ +package com.lovenav.service.serviceImpl; + +import com.alibaba.fastjson.JSON; +import com.lovenav.dao.CollectIconListDao; +import com.lovenav.dao.UrlListDao; +import com.lovenav.entity.CollectIconList; +import com.lovenav.entity.UrlList; +import com.lovenav.service.QRCService; +import com.lovenav.utils.QRCodeUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.List; + + +@Service +public class QRCServiceImpl implements QRCService{ + @Autowired + private UrlListDao urlListDao; + @Autowired + private CollectIconListDao collectIconListDao; + + public String QR(CollectIconList collect) throws Exception { + String logoPath ="src/main/resources/static/logo/NAV.png"; + String destPath = "src/main/resources/static/qr"; +// 通过传入collect的url_id查找相同网址图片列表 +// List collectIconList = collectIconListDao.selectByUrlid(collect.getUrl_id()); + +// 通过collect的url_id查找网址id对应网址 + UrlList urlList = urlListDao.selectByPrimaryKey(Long.valueOf(collect.getUrl_id())); + if(urlList != null){ +// 获取网址url + String url = urlList.getUrl(); +// 将网址生成二维码并返回本地路径 + String url_wait = QRCodeUtil.encode(url, logoPath, destPath, true); +// 为collect设置二维码本地路径 + collect.setQr_url(url_wait); + collectIconListDao.insert(collect); + String base64 = QRCodeUtil.ImageToBase64(url_wait); + return base64; + }else{ + HashMap result = new HashMap<>(); + result.put("code",500); + result.put("msg", "找不到对应网址"); + return JSON.toJSONString(result); + } + } +} diff --git a/src/main/java/com/lovenav/utils/QRCodeUtil.java b/src/main/java/com/lovenav/utils/QRCodeUtil.java index 05e9330..57c0896 100644 --- a/src/main/java/com/lovenav/utils/QRCodeUtil.java +++ b/src/main/java/com/lovenav/utils/QRCodeUtil.java @@ -5,23 +5,24 @@ import com.google.zxing.client.j2se.BufferedImageLuminanceSource; import com.google.zxing.common.BitMatrix; import com.google.zxing.common.HybridBinarizer; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; -import org.apache.tomcat.util.codec.binary.Base64; +import com.lovenav.dao.CollectIconListDao; +import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; import javax.imageio.ImageIO; import java.awt.*; import java.awt.geom.RoundRectangle2D; import java.awt.image.BufferedImage; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.IOException; -import java.io.OutputStream; +import java.io.*; import java.util.HashMap; import java.util.Hashtable; import java.util.Map; import java.util.Random; +import java.util.Base64; - +@Component +@Service public class QRCodeUtil { private static final String CHARSET = "utf-8"; private static final String FORMAT_NAME = "JPG"; @@ -313,24 +314,14 @@ public class QRCodeUtil { return image; } - public static void main(String[] args) throws Exception { - String text = "https://www.baidu.com"; //这里设置自定义网站url - String logoPath ="src/main/resources/static/logo/NAV.png"; - String destPath = "src/main/resources/static/qr"; - String url=QRCodeUtil.encode(text, logoPath, destPath, true); - System.out.println(url); - /* String file="D:\\11.jpg"; - String info=QRCodeUtil.decode(file); - System.out.println(info);*/ - } /** * 将图片url转换成base64流 * @param url * @return */ - public String getBase64(String url){ + public static String getBase64(String url){ MultiFormatWriter multiFormatWriter = new MultiFormatWriter(); Map hints = new HashMap(); hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); //设置字符集编码类型 @@ -343,8 +334,8 @@ public class QRCodeUtil { //输出二维码图片流 try { ImageIO.write(image, "jpg",outputStream); - String base = new Base64().encodeAsString(outputStream.toByteArray()); -// new Base64().encode(outputStream.toByteArray()); + Base64.Encoder encoder = Base64.getEncoder(); + String base =encoder.encodeToString(outputStream.toByteArray()); return base; } catch (IOException e) { // TODO Auto-generated catch block @@ -371,4 +362,38 @@ public class QRCodeUtil { return image; } +// 获取本地图片路径,转为base64 + public static String ImageToBase64(String imgPath) { + byte[] data = null; + // 读取图片字节数组 + try { + InputStream in = new FileInputStream(imgPath); + data = new byte[in.available()]; + in.read(data); + in.close(); + } catch (IOException e) { + e.printStackTrace(); + } + // 对字节数组Base64编码 + Base64.Encoder encoder = Base64.getEncoder(); + // 返回Base64编码过的字节数组字符串 +// System.out.println("本地图片转换Base64:" + encoder.encodeToString(data)); + return encoder.encodeToString(data); + } + + + public static void main(String[] args) throws Exception { + String text = "https://www.baidu.com"; //这里设置自定义网站url + String logoPath ="src/main/resources/static/logo/NAV.png"; + String destPath = "src/main/resources/static/qr"; + String url = QRCodeUtil.encode(text, logoPath, destPath, true); + QRCodeUtil.ImageToBase64("src/main/resources/static/qr/44672405.jpg"); +// System.out.println(url); +// String base64 = getBase64(url); +// System.out.println(base64); + + /* String file="D:\\11.jpg"; + String info=QRCodeUtil.decode(file); + System.out.println(info);*/ + } } \ No newline at end of file diff --git a/src/main/resources/mybatis/CollectIconListDao.xml b/src/main/resources/mybatis/CollectIconListDao.xml index 95db15c..257fdc8 100644 --- a/src/main/resources/mybatis/CollectIconListDao.xml +++ b/src/main/resources/mybatis/CollectIconListDao.xml @@ -3,7 +3,7 @@ - + @@ -25,7 +25,7 @@ insert into ln_collect_icon_list (url_id, icon_url, qr_url, `status`) - values (#{url_id,jdbcType=VARCHAR}, #{icon_url,jdbcType=VARCHAR}, #{qr_url,jdbcType=VARCHAR}, + values (#{url_id,jdbcType=INTEGER}, #{icon_url,jdbcType=VARCHAR}, #{qr_url,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER}) @@ -46,7 +46,7 @@ - #{url_id,jdbcType=VARCHAR}, + #{url_id,jdbcType=INTEGER}, #{icon_url,jdbcType=VARCHAR}, @@ -63,7 +63,7 @@ update ln_collect_icon_list - url_id = #{url_id,jdbcType=VARCHAR}, + url_id = #{url_id,jdbcType=INTEGER}, icon_url = #{icon_url,jdbcType=VARCHAR}, @@ -79,10 +79,18 @@ update ln_collect_icon_list - set url_id = #{url_id,jdbcType=VARCHAR}, + set url_id = #{url_id,jdbcType=INTEGER}, icon_url = #{icon_url,jdbcType=VARCHAR}, qr_url = #{qr_url,jdbcType=VARCHAR}, `status` = #{status,jdbcType=INTEGER} where id = #{id,jdbcType=BIGINT} + + + \ No newline at end of file diff --git a/src/main/resources/mybatis/UrlListDao.xml b/src/main/resources/mybatis/UrlListDao.xml index 5a5f0e7..407f13d 100644 --- a/src/main/resources/mybatis/UrlListDao.xml +++ b/src/main/resources/mybatis/UrlListDao.xml @@ -282,4 +282,6 @@ where id = #{id,jdbcType=BIGINT} + + \ No newline at end of file