二维码生成,可以在后端生成图片,存储在服务器,然后把虚拟路径返回到前端,也可以把数据传输到前端,通过前端框架生成在线二维码,在前端生成二维码不占用内存,但是网络原因会影响显示。

一、后端:

String name = UUID.randomUUID()+".png";//通过UUID先生成图片的名称
File logoFile = new File(path+File.separator+"4.png"); //文件创建图片
File QrCodeFile = new File(path+File.separator+name); //路径加名称
String sample_id = sam.getStr("xxxx"); //获取到需要存到图片的内容
ZXingCode.drawLogoQRCode(logoFile, QrCodeFile, sample_id, ""); //创建进去

//把name返回给前端就好

ZXingCode:

package xxxx;import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.io.FileInputStream;
import java.io.FileOutputStream;import java.util.Hashtable;import javax.imageio.ImageIO;import org.apache.commons.lang3.StringUtils;import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import com.google.zxing.BinaryBitmap;import com.google.zxing.LuminanceSource;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;import com.google.zxing.client.j2se.BufferedImageLuminanceSource;/*** 画制定logo和制定描述的二维码** @author songyz**/
public class ZXingCode {private static final int QRCOLOR = 0xFF000000; // 默认是黑色private static final int BGWHITE = 0xFFFFFFFF; // 背景颜色private static final int WIDTH = 300; // 二维码宽private static final int HEIGHT = 300; // 二维码高// 用于设置QR二维码参数private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {private static final long serialVersionUID = 1L;{put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 设置QR二维码的纠错级别(H为最高级别)具体级别信息put(EncodeHintType.CHARACTER_SET, "utf-8");// 设置编码方式put(EncodeHintType.MARGIN, 0);}};/*** 生成包含字符串信息的二维码图片* @param outputStream 文件输出流路径* @param content 二维码携带信息* @param qrCodeSize 二维码图片大小* @param imageFormat 二维码的格式* @throws WriterException* @throws*/public static boolean createQrCode(OutputStream outputStream, String content, int qrCodeSize, String imageFormat) throws WriterException, IOException{//设置二维码纠错级别MAPHashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);  // 矫错级别QRCodeWriter qrCodeWriter = new QRCodeWriter();//创建比特矩阵(位矩阵)的QR码编码的字符串BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);// 使BufferedImage勾画QRCode  (matrixWidth 是行二维码像素点)int matrixWidth = byteMatrix.getWidth();BufferedImage image = new BufferedImage(matrixWidth-200, matrixWidth-200, BufferedImage.TYPE_INT_RGB);image.createGraphics();Graphics2D graphics = (Graphics2D) image.getGraphics();graphics.setColor(Color.WHITE);graphics.fillRect(0, 0, matrixWidth, matrixWidth);// 使用比特矩阵画并保存图像graphics.setColor(Color.BLACK);for (int i = 0; i < matrixWidth; i++){for (int j = 0; j < matrixWidth; j++){if (byteMatrix.get(i, j)){graphics.fillRect(i-100, j-100, 1, 1);}}}return ImageIO.write(image, imageFormat, outputStream);}/*** 读二维码并输出携带的信息*/public static void readQrCode(InputStream inputStream) throws IOException{//从输入流中获取字符串信息BufferedImage image = ImageIO.read(inputStream);//将图像转换为二进制位图源LuminanceSource source = new BufferedImageLuminanceSource(image);BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));QRCodeReader reader = new QRCodeReader();Result result = null ;try {result = reader.decode(bitmap);} catch (ReaderException e) {e.printStackTrace();}System.out.println(result.getText());}public static void main(String[] args) throws WriterException {
//        try {
//            String content = "41000040288;D041E05001000H;2018.06.27;80;SC786;7861862701";
//            createQrCode(new FileOutputStream(new File("d:\\qrcode.jpg")),content,900,"JPEG");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        try {
//            readQrCode(new FileInputStream(new File("d:\\qrcode.jpg")));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }File logoFile = new File("D://logo4.png");File QrCodeFile = new File("D://08.png");String url = "41000040288;D041E05001000H;2018.06.27;80;SC786;7861862701";String note = "";drawLogoQRCode(logoFile, QrCodeFile, url, note);}// 生成带logo的二维码图片public static void drawLogoQRCode(File logoFile, File codeFile, String qrUrl, String note) {try {MultiFormatWriter multiFormatWriter = new MultiFormatWriter();// 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);// 开始利用二维码数据创建Bitmap图片,分别设为黑(0xFFFFFFFF)白(0xFF000000)两色for (int x = 0; x < WIDTH; x++) {for (int y = 0; y < HEIGHT; y++) {image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);}}int width = image.getWidth();int height = image.getHeight();if (logoFile!=null && logoFile.exists()) {// 构建绘图对象Graphics2D g = image.createGraphics();// 读取Logo图片BufferedImage logo = ImageIO.read(logoFile);// 开始绘制logo图片g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);g.dispose();logo.flush();}// 自定义文本描述if (StringUtils.isNotEmpty(note)) {// 新的图片,把带logo的二维码下面加上文字BufferedImage outImage = new BufferedImage(400, 445, BufferedImage.TYPE_4BYTE_ABGR);Graphics2D outg = outImage.createGraphics();// 画二维码到新的面板outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);// 画文字到新的面板outg.setColor(Color.BLACK);outg.setFont(new Font("楷体", Font.BOLD, 30)); // 字体、字型、字号int strWidth = outg.getFontMetrics().stringWidth(note);if (strWidth > 399) {// //长度过长就截取前面部分// 长度过长就换行String note1 = note.substring(0, note.length() / 2);String note2 = note.substring(note.length() / 2, note.length());int strWidth1 = outg.getFontMetrics().stringWidth(note1);int strWidth2 = outg.getFontMetrics().stringWidth(note2);outg.drawString(note1, 200 - strWidth1 / 2, height + (outImage.getHeight() - height) / 2 + 12);BufferedImage outImage2 = new BufferedImage(400, 485, BufferedImage.TYPE_4BYTE_ABGR);Graphics2D outg2 = outImage2.createGraphics();outg2.drawImage(outImage, 0, 0, outImage.getWidth(), outImage.getHeight(), null);outg2.setColor(Color.BLACK);outg2.setFont(new Font("宋体", Font.BOLD, 30)); // 字体、字型、字号outg2.drawString(note2, 200 - strWidth2 / 2,outImage.getHeight() + (outImage2.getHeight() - outImage.getHeight()) / 2 + 5);outg2.dispose();outImage2.flush();outImage = outImage2;} else {outg.drawString(note, 200 - strWidth / 2, height + (outImage.getHeight() - height) / 2 + 12); // 画文字}outg.dispose();outImage.flush();image = outImage;}image.flush();ImageIO.write(image, "png", codeFile); // TODO} catch (Exception e) {e.printStackTrace();}}}

二、前端

需要导入:
<script type="text/javascript" src="../qrcode.min.js"></script>
<script type="text/javascript" src="../jquery.min.js"></script>【JS资源】

<div id="code1" class="barcode2"  style="width: 100px; height: 100px;" ></div>
<script type="text/javascript">new QRCode(document.getElementById("code1"),  {minVersion:5,text: '二维码的内容',width: "100px",height: "100px",useSVG: true,colorDark : "#000000",colorLight : "#ffffff",correctLevel : QRCode.CorrectLevel.L});
</script>

二维码生成【前端,后端】相关推荐

  1. 前端(Vue)怎么实现二维码生成

    功能需求: 输入链接或文字,生成二维码 效果演示: 输入网址 使用手机扫码 输入文字 扫描二维码 好,现在我们开始编程,简单介绍一下思路:找到一个免费的二维码生成api接口,进行对接 我用到的是一个叫 ...

  2. 微信小程序二维码生成工具,后端二维码生成工具类。

    微信小程序开发二维码生成工具类 前言 业务背景 设计思路 具体实现 接下来我们进行工具的改造 前言 或许这是你搜寻的第n篇文章来解决你项目中的问题,虽然我不能去替你完全适配你的业务需求,但是我可以给你 ...

  3. html,vue, react,angular 前端实现二维码生成 ,二维码解析

    本文的背景 近期,由于项目开发的需求,需要前端实现图片二维码的解析. 由于需求的需要,这边调研了一下,发现很多人都有着类似的需求,网上给的解决方案也很多,但是感觉还是有些..... 又想到之前做过前端 ...

  4. 二维码生成及下载(前端)

    最近在项目中用到二维码生成及下载,做个笔记记录下 首先引入js文件, 这是在网上找的 <script src="http://cdn.staticfile.org/jquery.qrc ...

  5. 前端vue uni-app基于uQRCode封装简单快速实用全端二维码生成插件

    快速实现基于uQRCode封装简单快速实用全端二维码生成插件; 下载完整代码请访问uni-app插件市场地址:https://ext.dcloud.net.cn/plugin?id=12677 效果图 ...

  6. 聊聊 Web 项目二维码生成的最佳姿势

    在设计和实现的过程之后,你永远不知道部署上去的程序会已什么样的姿势运行. 本篇借一次生成二维码逻辑的不同实现,阐述 Web 项目中二维码生成的正确姿势. 文中如有批量,欢迎各位看客老爷拍砖.试运行前5 ...

  7. java 二维码生成及其标签打印

    本文主要内容 二维码生成 二维码标签预览及打印 二维码生成 笔者此次的二维码是通过调用第三方接口生成的,具体流程如下: 根据规范要求调用第三方接口,返回二维码下载地址及二维码图片的属性值(图片大小等) ...

  8. 微信支付:支付流程分析、微信扫码支付(HttpClient)、微信支付二维码生成、检测支付状态、订单状态操作准备工作、支付信息回调、MQ处理支付回调状态、定时处理订单状态

    微信支付 微信支付开发的整体思路 生成支付二维码 查询支付状态(微信的服务器) 实现订单状态的修改.删除订单 支付状态回查->微信服务器将支付状态返回给支付微服务 MQ处理支付回调状态 Rabb ...

  9. Java 快速开发二维码生成服务

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 来源 | 公众号「码农小胖哥」 1. 前言 不知道从什么 ...

最新文章

  1. ExtJs选项卡,求大神解答
  2. 飞向天国:儿童智力单机游戏6岁-8岁
  3. hbuilderx制作简单网页_网页制作的基本步骤是怎样的?制作简单网页的具体操作有哪些呢?...
  4. python 物理学中的应用_大学物理中Python的应用
  5. oracle监控工具ignite使用图解
  6. 基于Linux的集群系统(三)
  7. 移动通信原理学习笔记之三——抗衰落和链路性能增强技术
  8. Java 开发必看的 5个 Github 开源项目
  9. 11、权重残差图、RLE和NUSE
  10. 【CSS】background各属性一览汇总
  11. 扯淡扯着扯着就远了----关键字;宁静致远
  12. 人工智能之我见(1)
  13. python随手记自动记账_随手记的5个常用功能,让你记账省心,高效,又安全
  14. 武汉理工大龙芯计算机学院2000级,信息学院学生团队获第四届龙芯杯计算机系统能力培养大赛三等奖...
  15. AUTOCAD使用心得
  16. Redis Lua拓展及使用示例
  17. 我是如何在2年内通过注册会计师考试的
  18. Java中带标签的break和continue
  19. mongo连接命令 windows_Windows 平台安装
  20. 通过Vue绑定zoom样式值实现禁止页面放大缩小

热门文章

  1. Java学习-Java语言基础
  2. 无人驾驶大巴试车_中国无人驾驶汽车高速公路试车成功
  3. Ubuntu安装中文出现Transaction failed:Package dependencies cannot be resolved
  4. 从“弃儿”到大神,神经网络之父Hiton说人类就是非常精美的机器 | AI英雄
  5. Java如何定义三维数组
  6. (记录)华为机试练习题13—句子逆序
  7. shell特殊符号含义(新手上路,请多关照)
  8. XXX售后服务解决方案
  9. 刻度标尺精确定位系统-更为人性化的位置检测系统
  10. 留数定理 含 数学物理方法(吴崇试 第三版)答案详解