java代码将链接转换为二维码
本文是短链接生成二维码图片保存到本地的一个实例demo
核心包是
core-3.1.0.jar 自己下载吧
package cn.itcast.action;/*** Created by YYBJ on 2018/7/4.* ZCL*/
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;import javax.imageio.ImageIO;
import javax.swing.filechooser.FileSystemView;import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;public class QrCodeUtil {public static void main(String[] args) {String url = "http://t.cn/RdZMeZJ111111";String path = FileSystemView.getFileSystemView().getHomeDirectory() + File.separator + "testQrcode";System.out.println(path);String fileName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + ".jpg";createQrCode(url, path, fileName);}public static String createQrCode(String url, String path, String fileName) {try {Map<EncodeHintType, String> hints = new HashMap<>();hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 400, 400, hints);File file = new File(path, fileName);if (file.exists() || ((file.getParentFile().exists() || file.getParentFile().mkdirs()) && file.createNewFile())) {writeToFile(bitMatrix, "jpg", file);System.out.println("测试完成:" + file);}} catch (Exception e) {e.printStackTrace();}return null;}static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {BufferedImage image = toBufferedImage(matrix);if (!ImageIO.write(image, format, file)) {throw new IOException("Could not write an image of format " + format + " to " + file);}}static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {BufferedImage image = toBufferedImage(matrix);if (!ImageIO.write(image, format, stream)) {throw new IOException("Could not write an image of format " + format);}}private static final int BLACK = 0xFF000000;private static final int WHITE = 0xFFFFFFFF;private 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) ? BLACK : WHITE);}}return image;}}
转载请标明转载来源:
https://blog.csdn.net/weixin_41092717/article/details/80908649
java代码将链接转换为二维码相关推荐
- JS链接转换为二维码
这里用到一个JQ插件 qrcode.js 下载地址https://github.com/jeromeetienne/jquery-qrcode 先引入 <script src="j ...
- java 根据指定链接生成二维码
需求: 就是需要把一个报告网址链接生成二维码,然后渲染在报告中,扫描二维码的时候能访问到这个报告,效果就达到了 1.首先提前需要添加的maven依赖 <!-- https://mvnreposi ...
- 百万前端之js通过链接生成二维码可以保存下载复制
在前端工作中,会遇到很多邀请好友的需求,这个时候就需要前端对需求进行实现了. 最终效果: 这个是做好以后的成品,根据链接生成二维码,提供保存二维码和复制链接功能,纯前端实现.话不多说直接上代码 htm ...
- 原生js链接转二维码
1.下载jquery.qrcode.min.js文件,文件内容如下: (function(r){r.fn.qrcode=function(h){var s;function u(a){this.mod ...
- java生成二维码-微信支付、支付宝支付链接转二维码解决方案
1.后台返回链接利用js生成二维码供客户使用 需要js文件:qrcode.min.js 下载地址:http://static.runoob.com/assets/qrcode/qrcode.min.j ...
- 【java实现网址转换为二维码】
哔哩哔哩 (゜-゜)つロ 干杯~-bilibilihttps://www.bilibili.com/ 我们可以实现图片二维码转换为网址,或者将网址转换为伪二维码(与普通二维码有区别,因为 ...
- java生成二维码(链接生成二维码)
Java二维码如何生成? awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import com. ...
- js实现将链接生成二维码,并对生成的二维码转换为图片,右击可保存至本地
在vue项目中,实现将链接生成二维码:可识别跳转,将生成的二维码可转换成图片,并可保存至本地.具体操作步骤如下: 1.安装及引用 首先,实现这个功能需要使用QRCode和html2canvas,所以在 ...
- Java(Spring boot)实现生成二维码
文章目录 一.引入spring boot依赖: 二.工具类代码: 三.调用工具类生成二维码 1.将链接生成二维码图片并保存到指定路径 2.将链接生成二维码直接显示在页面 3.将以get请求传参链接生成 ...
最新文章
- Lyft推出一种新的实时地图匹配算法
- biomaRt包下载转录本信息
- Vue 路由router的两种模式
- 我的AutoHotkey配置
- 现代软件工程 教学计划 适应两种难度和重点
- mysql engine 和type_type=INNODB和engine=INNODB的区别
- input之question
- 从零开始刷Leetcode——数组(448.485.509)
- c语言判断字符串的编码,C语言中判断一个char*是不是utf8编码
- 【渝粤教育】国家开放大学2018年春季 0064-21T20世纪欧美文学 参考试题
- 这可能是目前最全的Redis高可用技术解决方案总结
- BUUCTF Misc Page2-6部分题目
- 小程序的云服务器规格,小程序的云服务器规格
- Unity3D Odin Inspector 简单介绍与入门
- (全网最全)律师如何有效获得案源?新律师开拓案源的方法有哪些?
- 计算机桌面设置,电脑怎么设置动态桌面
- windows2016小文件服务器,Windows Server 2016 搭建 SMB 共享文件
- html-HR标签分割线
- 2016.3.24 OneZero站立会议
- Jupyter notebook 配置无问题 但就是无法远程访问,解决方法