可以将本地图片导入然后传到前端,也可以生成一个随意长度的二维码保存到本地,也可以传到前端,可以直接通过地址栏访问。要记得导入相关pom依赖包。

获取本地图片传到前端

    /*** @param* @paramm param* @功能描述  传图片*/@GetMapping("/GenerateCode")public voidGenerateCode(HttpServletRequest request, HttpServletResponse response) {//读取文件   在用BufferedImage 读取File file = new File("H:\\IMAGEA\\qrcode_test.png");BufferedImage bu = null;try {bu = ImageIO.read(file);} catch (IOException e) {e.printStackTrace();}//传到浏览器上try {ImageIO.write( bu, "PNG", response.getOutputStream());} catch (IOException e) {e.printStackTrace();}}

生成二维码传到前端

@GetMapping("/GenerateCode1")public void getCode(HttpServletRequest request, HttpServletResponse response) {// 二维码内容String url = "https://www.baidu.com/";// 生成二维码并指定宽高BufferedImage generate = QrCodeUtil.generate(url, 300, 300);// 转换流信息写出FastByteArrayOutputStream os = new FastByteArrayOutputStream();try {ImageIO.write(generate, "jpg", response.getOutputStream());} catch (IOException e) {e.printStackTrace();}}

生成二维码保存到本地后在传到前端

    /*** @param* @paramm param* @功能描述 二维码*/@GetMapping("/GenerateCode")public Result GenerateCode(HttpServletRequest request, HttpServletResponse response) {String content = "http://www.baidu.com";//这里content为二维码数据。为地址将直接访问,为text将直接解析int width = 200; // 图像宽度int height = 200; // 图像高度String format = "png";// 图像类型Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");hints.put(EncodeHintType.MARGIN, 4);//设置二维码边的空度,非负数,默认值为4。BitMatrix bitMatrix = null;// 生成矩阵try {bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);} catch (WriterException e) {e.printStackTrace();}//MatrixToImageWriter提供多种方式输出二维码,并同时提供二维码其他信息配置的重载方法try {MatrixToImageWriter.writeToFile(bitMatrix, format, new File("H:\\IMAGEA\\qrcode_test.png"));// 输出图像} catch (IOException e) {e.printStackTrace();}//读取文件   在用BufferedImage 读取File file = new File("H:\\IMAGEA\\qrcode_test.png");`在这里插入代码片`BufferedImage bu = null;try {bu = ImageIO.read(file);} catch (IOException e) {e.printStackTrace();}//传到浏览器上try {ImageIO.write( bu, "PNG", response.getOutputStream());} catch (IOException e) {e.printStackTrace();}return Result.ok("H:\\IMAGEA\\qrcode_test.png");}

二维码相关依赖

pom文件

<!-- 导入zxing的依赖 --><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.2.1</version></dependency><dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.2.1</version></dependency>//hutool工具包<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.0.7</version></dependency>

MatrixToImageConfig

package com.example.md.util;public final class MatrixToImageConfig
{public static final int BLACK = -16777216;public static final int WHITE = -1;private final int onColor;private final int offColor;public MatrixToImageConfig(){this(-16777216, -1);}public MatrixToImageConfig(int onColor, int offColor){this.onColor = onColor;this.offColor = offColor;}public int getPixelOnColor(){return this.onColor;}public int getPixelOffColor(){return this.offColor;}int getBufferedImageColorModel(){if ((this.onColor == -16777216) && (this.offColor == -1)) {return 12;}if ((hasTransparency(this.onColor)) || (hasTransparency(this.offColor))) {return 2;}return 1;}private static boolean hasTransparency(int argb){return (argb & 0xFF000000) != -16777216;}
}

MatrixToImageWriter

package com.example.md.util;import com.google.zxing.common.BitMatrix;import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Path;
import javax.imageio.ImageIO;public final class MatrixToImageWriter
{private static final MatrixToImageConfig DEFAULT_CONFIG = new MatrixToImageConfig();public static BufferedImage toBufferedImage(BitMatrix matrix){return toBufferedImage(matrix, DEFAULT_CONFIG);}public static BufferedImage toBufferedImage(BitMatrix matrix, MatrixToImageConfig config){int width = matrix.getWidth();int height = matrix.getHeight();BufferedImage image = new BufferedImage(width, height, config.getBufferedImageColorModel());int onColor = config.getPixelOnColor();int offColor = config.getPixelOffColor();for (int x = 0; x < width; x++) {for (int y = 0; y < height; y++) {image.setRGB(x, y, matrix.get(x, y) ? onColor : offColor);}}return image;}@Deprecatedpublic static void writeToFile(BitMatrix matrix, String format, File file)throws IOException{writeToPath(matrix, format, file.toPath());}public static void writeToPath(BitMatrix matrix, String format, Path file)throws IOException{writeToPath(matrix, format, file, DEFAULT_CONFIG);}@Deprecatedpublic static void writeToFile(BitMatrix matrix, String format, File file, MatrixToImageConfig config)throws IOException{writeToPath(matrix, format, file.toPath(), config);}public static void writeToPath(BitMatrix matrix, String format, Path file, MatrixToImageConfig config)throws IOException{BufferedImage image = toBufferedImage(matrix, config);if (!ImageIO.write(image, format, file.toFile())) {throw new IOException("Could not write an image of format " + format + " to " + file);}}public static void writeToStream(BitMatrix matrix, String format, OutputStream stream)throws IOException{writeToStream(matrix, format, stream, DEFAULT_CONFIG);}public static void writeToStream(BitMatrix matrix, String format, OutputStream stream, MatrixToImageConfig config)throws IOException{BufferedImage image = toBufferedImage(matrix, config);if (!ImageIO.write(image, format, stream)) {throw new IOException("Could not write an image of format " + format);}}
}
效果

Java获取图片传到前端,生成二维码给前端相关推荐

  1. 前端生成二维码与图片合成

    首先前端生成二维码 使用插件完成,插件为DrawQRCode.unitypackage  合成二维码直接使用DrawQRCode 类里提供的方法即可 生成二维码的方法 DrawCode_Color32 ...

  2. vue 前端生成二维码,并转换为图片

    这篇文章主要是分享下自己的收获,也是自己遇到的问题: 前端如何自己生成二维码? 前端如何将生成的二维码转成图片并展示? 如何控制二维码的显隐? 话不多说,直接上干货 base64如何转换成图片 npm ...

  3. 微信小程序前端生成二维码并保存(海报同理)

    这里写自定义目录标题 1.前端生成二维码并保存 1.前端生成二维码并保存 1.下载weapp.qrcode.js文件并引入项目中 2.先在wxml文件中构建canvas画布 <canvas st ...

  4. 前端生成二维码qrcode.js,并下载

    利用qrcode.js在前端生成二维码,并下载 一.引入js包 <script type="text/javascript" src="//static.runoo ...

  5. 通过web系统实现淘宝营销引流,小说或者视频上传之后自动生成二维码,通过二维码分享之后引入微信或者扣扣加群,群满自动切换到下一个,以及数据统计分析和若干个小工具集成

    通过web系统实现淘宝营销引流,小说或者视频上传之后自动生成二维码,通过二维码分享之后引入微信或者扣扣加群,群满自动切换到下一个,以及数据统计分析和若干个小工具集成. 主要实现技术: 1.大文件视频分 ...

  6. vue前端生成二维码,实现扫码下载功能

    vue前端生成二维码,实现扫码下载功能 首先需要安装一个插件 npm install --save qrcodejs2 然后在需要的页面引入插件,最后使用 <template><di ...

  7. 前端生成二维码(借助草料)

    前端生成二维码. 使用草料API: generateQrCode(qrCode) {window.location.href = `https://cli.im/api/qrcode/code?tex ...

  8. 前端生成二维码及把页面转为图片保存到本地

    以vue项目为例,其他类型项目其实也是一样的 所需components: QRCode html2canvas npm install qrcodejs2 -- save npm install ht ...

  9. java将一个url链接或者文字生成二维码并且转成base64

    我们在开发的时候,肯定有这样的需求.把一个url链接转成二维码图片.提供给用户扫描,然后跳转到相应的页面. 三个问题(前提:没有用统一的图片服务器如:fastdfs.) (1):把url链接转成图片保 ...

最新文章

  1. 设计师你们还坐的住吗?2021 PS 进入人工智能 P 图时代
  2. linux刷新挂载信息,Linux系统下如何挂载FAT32的U盘
  3. C语言 · 身份证号码升级
  4. python篮球-用Python把蔡徐坤打篮球视频转换成字符动画!
  5. 【Storm篇】--Storm基础概念
  6. ZOJ 38727(贪心)
  7. matlab计算曲线形心,并将y轴移动到形心上,使图形居中布置
  8. iscsi:IO操作流程(三)
  9. WebService调用
  10. linux 查看各个cpu使用率
  11. 2012-10-29 → 2012-11-11 周总结:项目试运行(考验的时候到了),总算解决了WCF慢的问题了...
  12. 考勤 指纹 php,折腾中控指纹签到SDK始末(.net监听) | quericy Eden*
  13. SFDC数据导出的几种方式
  14. 快速处理-小程序/uniapp,showToast没有效果
  15. SDN入门第五篇——交换机与控制器之间的交互流程
  16. duilib最新版地址
  17. 在firefox中 屏蔽CSDN博客广告 + 添加百度搜索引擎
  18. latex 中手写l的输入方法
  19. 使用 NoSQL 数据库提供云级别数据可伸缩性
  20. python组件化软件策划_Vue组件化开发

热门文章

  1. 用友漏洞php,Phpwind GET型CSRF任意代码执行漏洞公开
  2. html 链接长宽,CSS实现长宽比的几种方案【转载】
  3. Peekaboo——项目系统设计与数据库设计
  4. Peekaboo——代码规范、冲刺任务与计划
  5. nginx实现反向代理及负载均衡
  6. mongodb不等于某个值_mongodb条件查询不等于
  7. 用计算机玩游戏教程,Wegame怎么用手机玩电脑游戏 Wegame手机玩电脑游戏教程
  8. 记一次亚马逊扩容报错data size unchanged, skipping问题
  9. linux rhel dns配置,RedHat Linux DNS配置指南
  10. Latex同时合并表格的多行多列