This commit is contained in:
sjm 2023-12-22 23:25:19 +08:00
parent 7adff734ea
commit a9a4cbcf83
11 changed files with 398 additions and 41 deletions

View File

@ -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);
}
}

View File

@ -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<CollectIconList> selectByUrlid(int urlid);
}

View File

@ -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);
}

View File

@ -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;
/**
* 图片地址

View File

@ -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
*/

View File

@ -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
*/

View File

@ -0,0 +1,7 @@
package com.lovenav.service;
import com.lovenav.entity.CollectIconList;
public interface QRCService {
public String QR(CollectIconList collect) throws Exception;
}

View File

@ -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> 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<String,Object> result = new HashMap<>();
result.put("code",500);
result.put("msg", "找不到对应网址");
return JSON.toJSONString(result);
}
}
}

View File

@ -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);*/
}
}

View File

@ -3,7 +3,7 @@
<mapper namespace="com.lovenav.dao.CollectIconListDao">
<resultMap id="BaseResultMap" type="com.lovenav.entity.CollectIconList">
<id column="id" jdbcType="BIGINT" property="id" />
<result column="url_id" jdbcType="VARCHAR" property="url_id" />
<result column="url_id" jdbcType="INTEGER" property="url_id" />
<result column="icon_url" jdbcType="VARCHAR" property="icon_url" />
<result column="qr_url" jdbcType="VARCHAR" property="qr_url" />
<result column="status" jdbcType="INTEGER" property="status" />
@ -25,7 +25,7 @@
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.lovenav.entity.CollectIconList" useGeneratedKeys="true">
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})
</insert>
<insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.lovenav.entity.CollectIconList" useGeneratedKeys="true">
@ -46,7 +46,7 @@
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="url_id != null">
#{url_id,jdbcType=VARCHAR},
#{url_id,jdbcType=INTEGER},
</if>
<if test="icon_url != null">
#{icon_url,jdbcType=VARCHAR},
@ -63,7 +63,7 @@
update ln_collect_icon_list
<set>
<if test="url_id != null">
url_id = #{url_id,jdbcType=VARCHAR},
url_id = #{url_id,jdbcType=INTEGER},
</if>
<if test="icon_url != null">
icon_url = #{icon_url,jdbcType=VARCHAR},
@ -79,10 +79,18 @@
</update>
<update id="updateByPrimaryKey" parameterType="com.lovenav.entity.CollectIconList">
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}
</update>
<select id="selectByUrlid" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from ln_collect_icon_list
where url_id = #{url_id,jdbcType=INTEGER}
</select>
</mapper>

View File

@ -282,4 +282,6 @@
</set>
where id = #{id,jdbcType=BIGINT}
</update>
</mapper>