This commit is contained in:
sjm 2023-12-22 17:05:37 +08:00
parent b77f863fb5
commit b2cff4c97a
12 changed files with 557 additions and 1 deletions

16
pom.xml
View File

@ -95,7 +95,23 @@
<scope>test</scope>
</dependency>
<!--url转二维码依赖 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.axiom</groupId>
<artifactId>base64-utils</artifactId>
<version>1.4.0</version>
</dependency>
<!-- jackson 2.x 相关依赖 -->
<dependency>

View File

@ -46,4 +46,10 @@ public class CommentController {
public String View_comment(){
return commentService.View_comment();
}
// 显示回复
@RequestMapping(method = RequestMethod.GET, value = "/view_reply")
public String View_reply(int id){
return commentService.View_Reply(id);
}
}

View File

@ -0,0 +1,27 @@
package com.lovenav.controller;
import com.alibaba.fastjson.JSON;
import com.lovenav.utils.QRCodeUtil;
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
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);
}
}
}

View File

@ -0,0 +1,19 @@
package com.lovenav.dao;
import com.lovenav.entity.CollectIconList;
import org.springframework.stereotype.Repository;
@Repository
public interface CollectIconListDao {
int deleteByPrimaryKey(Long id);
int insert(CollectIconList record);
int insertSelective(CollectIconList record);
CollectIconList selectByPrimaryKey(Long id);
int updateByPrimaryKeySelective(CollectIconList record);
int updateByPrimaryKey(CollectIconList record);
}

View File

@ -23,4 +23,6 @@ public interface CommentDao {
int updateByPrimaryKey(Comment record);
List<Comment> selectByAllComment();
List<Comment> selectByAllReply(int id);
}

View File

@ -0,0 +1,34 @@
package com.lovenav.entity;
import java.io.Serializable;
import lombok.Data;
/**
* 图标表
* ln_collect_icon_list
*/
@Data
public class CollectIconList implements Serializable {
private Long id;
/**
* 网址id
*/
private String urlid;
/**
* 图片地址
*/
private String iconUrl;
/**
* 二维码地址
*/
private String qrUrl;
/**
* 状态
*/
private int status;
private static final long serialVersionUID = 1L;
}

View File

@ -15,4 +15,6 @@ public interface CommentService {
String Delete(int id);
// 显示评论
String View_comment();
// 显示回复
String View_Reply(int id);
}

View File

@ -72,4 +72,10 @@ public class CommentServiceImpl implements CommentService {
List<Comment> list = commentDao.selectByAllComment();
return JSON.toJSONString(list);
}
// 显示回复
public String View_Reply(int id){
List<Comment> list = commentDao.selectByAllReply(id);
return JSON.toJSONString(list);
}
}

View File

@ -0,0 +1,374 @@
package com.lovenav.utils;
import com.google.zxing.*;
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 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.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.Random;
public class QRCodeUtil {
private static final String CHARSET = "utf-8";
private static final String FORMAT_NAME = "JPG";
// 二维码尺寸
private static final int QRCODE_SIZE = 300;
// LOGO宽度
private static final int WIDTH = 60;
// LOGO高度
private static final int HEIGHT = 60;
private static BufferedImage createImage(String content, String imgPath,
boolean needCompress) throws Exception {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000
: 0xFFFFFFFF);
}
}
if (imgPath == null || "".equals(imgPath)) {
return image;
}
// 插入图片
QRCodeUtil.insertImage(image, imgPath, needCompress);
return image;
}
/**
* 插入LOGO
*
* @param source
* 二维码图片
* @param imgPath
* LOGO图片地址
* @param needCompress
* 是否压缩
* @throws Exception
*/
private static void insertImage(BufferedImage source, String imgPath,
boolean needCompress) throws Exception {
File file = new File(imgPath);
if (!file.exists()) {
System.err.println(""+imgPath+" 该文件不存在!");
return;
}
Image src = ImageIO.read(new File(imgPath));
int width = src.getWidth(null);
int height = src.getHeight(null);
if (needCompress) { // 压缩LOGO
if (width > WIDTH) {
width = WIDTH;
}
if (height > HEIGHT) {
height = HEIGHT;
}
Image image = src.getScaledInstance(width, height,
Image.SCALE_SMOOTH);
BufferedImage tag = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(image, 0, 0, null); // 绘制缩小后的图
g.dispose();
src = image;
}
// 插入LOGO
Graphics2D graph = source.createGraphics();
int x = (QRCODE_SIZE - width) / 2;
int y = (QRCODE_SIZE - height) / 2;
graph.drawImage(src, x, y, width, height, null);
Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
graph.setStroke(new BasicStroke(3f));
graph.draw(shape);
graph.dispose();
}
/**
* 生成二维码(内嵌LOGO)
*
* @param content
* 内容
* @param imgPath
* LOGO地址
* @param destPath
* 存放目录
* @param needCompress
* 是否压缩LOGO
* @throws Exception
*/
public static String encode(String content, String imgPath, String destPath,
boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, imgPath,
needCompress);
mkdirs(destPath);
String file = new Random().nextInt(99999999)+".jpg";
ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file));
return destPath+"/"+file;
}
/**
* 当文件夹不存在时mkdirs会自动创建多层目录区别于mkdir(mkdir如果父目录不存在则会抛出异常)
* @param destPath 存放目录
*/
public static void mkdirs(String destPath) {
File file =new File(destPath);
//当文件夹不存在时mkdirs会自动创建多层目录区别于mkdir(mkdir如果父目录不存在则会抛出异常)
if (!file.exists() && !file.isDirectory()) {
file.mkdirs();
}
}
/**
* 生成二维码(内嵌LOGO)
*
* @param content
* 内容
* @param imgPath
* LOGO地址
* @param destPath
* 存储地址
* @throws Exception
*/
public static void encode(String content, String imgPath, String destPath)
throws Exception {
QRCodeUtil.encode(content, imgPath, destPath, false);
}
/**
* 生成二维码
*
* @param content
* 内容
* @param destPath
* 存储地址
* @param needCompress
* 是否压缩LOGO
* @throws Exception
*/
public static void encode(String content, String destPath,
boolean needCompress) throws Exception {
QRCodeUtil.encode(content, null, destPath, needCompress);
}
/**
* 生成二维码
*
* @param content
* 内容
* @param destPath
* 存储地址
* @throws Exception
*/
public static void encode(String content, String destPath) throws Exception {
QRCodeUtil.encode(content, null, destPath, false);
}
/**
* 生成二维码(内嵌LOGO)
*
* @param content
* 内容
* @param imgPath
* LOGO地址
* @param output
* 输出流
* @param needCompress
* 是否压缩LOGO
* @throws Exception
*/
public static void encode(String content, String imgPath,
OutputStream output, boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, imgPath,
needCompress);
ImageIO.write(image, FORMAT_NAME, output);
}
/**
* 生成二维码
*
* @param content
* 内容
* @param output
* 输出流
* @throws Exception
*/
public static void encode(String content, OutputStream output)
throws Exception {
QRCodeUtil.encode(content, null, output, false);
}
/**
* 解析二维码
*
* @param file
* 二维码图片
* @return
* @throws Exception
*/
public static String decode(File file) throws Exception {
BufferedImage image;
image = ImageIO.read(file);
if (image == null) {
return null;
}
BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(
image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Result result;
Hashtable<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
result = new MultiFormatReader().decode(bitmap, hints);
String resultStr = result.getText();
return resultStr;
}
/**
* 解析二维码
*
* @param path
* 二维码图片地址
* @return
* @throws Exception
*/
public static String decode(String path) throws Exception {
return QRCodeUtil.decode(new File(path));
}
/**
*
* @param content
* @param imgPath
* @param destPath
* @param localUrl 当前环境的域名或者ip地址
* @param needCompress
* @return
* @throws Exception
*/
public static String encode(String content, String imgPath, String destPath,String localUrl,
boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtil.createImage(content, imgPath,
needCompress);
mkdirs(destPath);
String file = new Random().nextInt(99999999)+".jpg";
ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file));
return localUrl+file;
}
public static String encodeDZ(String content, String imgPath, String destPath,String localUrl,int length,int width,
boolean needCompress) throws Exception {
BufferedImage image = QRCodeUtil.createImageDZ(content, imgPath,length,width,
needCompress);
mkdirs(destPath);
String file = new Random().nextInt(99999999)+".jpg";
ImageIO.write(image, FORMAT_NAME, new File(destPath+"/"+file));
return localUrl+file;
}
private static BufferedImage createImageDZ(String content, String imgPath,int newlength,int newWidth,
boolean needCompress) throws Exception {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
hints.put(EncodeHintType.MARGIN, 1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, newlength, newWidth, hints);
int width = bitMatrix.getWidth();
int height = bitMatrix.getHeight();
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000
: 0xFFFFFFFF);
}
}
if (imgPath == null || "".equals(imgPath)) {
return image;
}
// 插入图片
QRCodeUtil.insertImage(image, imgPath, needCompress);
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){
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map hints = new HashMap();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); //设置字符集编码类型
hints.put(EncodeHintType.MARGIN, 1); //设置白边
BitMatrix bitMatrix = null;
try {
bitMatrix = multiFormatWriter.encode(url, BarcodeFormat.QR_CODE, 300, 300,hints);
BufferedImage image = toBufferedImage(bitMatrix);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//输出二维码图片流
try {
ImageIO.write(image, "jpg",outputStream);
String base = new Base64().encodeAsString(outputStream.toByteArray());
// new Base64().encode(outputStream.toByteArray());
return base;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (WriterException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return null;
}
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);//0xFF000000黑0xFFFFFFFF白
}
}
return image;
}
}

View File

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<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="urlid" />
<result column="icon_url" jdbcType="VARCHAR" property="iconUrl" />
<result column="icon_url" jdbcType="VARCHAR" property="iconUrl" />
<result column="icon_url" jdbcType="VARCHAR" property="iconUrl" />
</resultMap>
<sql id="Base_Column_List">
id, url_id, icon_url,qrUrl,status
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from ln_collect_icon_list
where id = #{id,jdbcType=BIGINT}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Long">
delete from ln_collect_icon_list
where id = #{id,jdbcType=BIGINT}
</delete>
<insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.lovenav.entity.CollectIconList" useGeneratedKeys="true">
insert into ln_collect_icon_list (url_md5, icon_url)
values (#{urlMd5,jdbcType=VARCHAR}, #{iconUrl,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.lovenav.entity.CollectIconList" useGeneratedKeys="true">
insert into ln_collect_icon_list
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="urlMd5 != null">
url_md5,
</if>
<if test="iconUrl != null">
icon_url,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="urlMd5 != null">
#{urlMd5,jdbcType=VARCHAR},
</if>
<if test="iconUrl != null">
#{iconUrl,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.lovenav.entity.CollectIconList">
update ln_collect_icon_list
<set>
<if test="urlMd5 != null">
url_md5 = #{urlMd5,jdbcType=VARCHAR},
</if>
<if test="iconUrl != null">
icon_url = #{iconUrl,jdbcType=VARCHAR},
</if>
</set>
where id = #{id,jdbcType=BIGINT}
</update>
<update id="updateByPrimaryKey" parameterType="com.lovenav.entity.CollectIconList">
update ln_collect_icon_list
set url_md5 = #{urlMd5,jdbcType=VARCHAR},
icon_url = #{iconUrl,jdbcType=VARCHAR}
where id = #{id,jdbcType=BIGINT}
</update>
</mapper>

View File

@ -153,5 +153,10 @@
where root_comment_id = 0 and comment_status = 0
</select>
<select id="selectByAllReply" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
*
from ln_comment
where root_comment_id = #{rootCommentId,jdbcType=INTEGER} and comment_status = 0
</select>
</mapper>

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB