java生成和解析二维码实战——QRCode
直接上代码,以下程序可直接运行:
package qrcode;import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import jp.sourceforge.qrcode.QRCodeDecoder;
import jp.sourceforge.qrcode.exception.DecodingFailedException;
import com.swetake.util.Qrcode;public class TwoDimensionCode {public static void main(String[] args) { // 二维码输出地址String imgPath = "C:/Users/Administrator/Desktop/myqrcode.png";// 二维码内容,此处放的是跳转地址,扫码后自动进入String content = "http://blog.csdn.net/yushouling";TwoDimensionCode handler = new TwoDimensionCode(); handler.encoderQRCode(content, imgPath, "png", 12); System.out.println("生成二维码成功");String decoderContent = handler.decoderQRCode(imgPath); System.out.println("解析结果:");System.out.println(decoderContent); }/** * 解析二维码(QRCode) * @param imgPath 图片路径 * @return */ public String decoderQRCode(String imgPath) { // QRCode 二维码图片的文件 File imageFile = new File(imgPath); BufferedImage bufImg = null; String content = null; try { bufImg = ImageIO.read(imageFile); QRCodeDecoder decoder = new QRCodeDecoder(); content = new String(decoder.decode(new TwoDimensionCodeImage(bufImg)), "utf-8"); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); e.printStackTrace(); } catch (DecodingFailedException dfe) { System.out.println("Error: " + dfe.getMessage()); dfe.printStackTrace(); } return content; } /** * 生成二维码(QRCode)图片 * @param content 存储内容 * @param imgPath 图片路径 */ public void encoderQRCode(String content, String imgPath) { this.encoderQRCode(content, imgPath, "png", 7); } /** * 生成二维码(QRCode)图片 * @param content 存储内容 * @param output 输出流 */ public void encoderQRCode(String content, OutputStream output) { this.encoderQRCode(content, output, "png", 7); } /** * 生成二维码(QRCode)图片 * @param content 存储内容 * @param imgPath 图片路径 * @param imgType 图片类型 */ public void encoderQRCode(String content, String imgPath, String imgType) { this.encoderQRCode(content, imgPath, imgType, 7); } /** * 生成二维码(QRCode)图片 * @param content 存储内容 * @param output 输出流 * @param imgType 图片类型 */ public void encoderQRCode(String content, OutputStream output, String imgType) { this.encoderQRCode(content, output, imgType, 7); } /** * 生成二维码(QRCode)图片 * @param content 存储内容 * @param imgPath 图片路径 * @param imgType 图片类型 * @param size 二维码尺寸 */ public void encoderQRCode(String content, String imgPath, String imgType, int size) { try { BufferedImage bufImg = this.qRCodeCommon(content, imgType, size); File imgFile = new File(imgPath); // 生成二维码QRCode图片 ImageIO.write(bufImg, imgType, imgFile); } catch (Exception e) { e.printStackTrace(); } } /** * 生成二维码(QRCode)图片 * @param content 存储内容 * @param output 输出流 * @param imgType 图片类型 * @param size 二维码尺寸 */ public void encoderQRCode(String content, OutputStream output, String imgType, int size) { try { BufferedImage bufImg = this.qRCodeCommon(content, imgType, size); // 生成二维码QRCode图片 ImageIO.write(bufImg, imgType, output); } catch (Exception e) { e.printStackTrace(); } } /** * 生成二维码(QRCode)图片的公共方法 * @param content 存储内容 * @param imgType 图片类型 * @param size 二维码尺寸 * @return */ private BufferedImage qRCodeCommon(String content, String imgType, int size) { BufferedImage bufImg = null; try { Qrcode qrcodeHandler = new Qrcode(); // 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小 qrcodeHandler.setQrcodeErrorCorrect('M'); qrcodeHandler.setQrcodeEncodeMode('B'); // 设置设置二维码尺寸,取值范围1-40,值越大尺寸越大,可存储的信息越大 qrcodeHandler.setQrcodeVersion(size); // 获得内容的字节数组,设置编码格式 byte[] contentBytes = content.getBytes("utf-8"); // 图片尺寸 int imgSize = 67 + 12 * (size - 1); bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB); Graphics2D gs = bufImg.createGraphics(); // 设置背景颜色 gs.setBackground(Color.WHITE); gs.clearRect(0, 0, imgSize, imgSize); // 设定图像颜色> BLACK gs.setColor(Color.BLACK); // 设置偏移量,不设置可能导致解析出错 int pixoff = 2; // 输出内容> 二维码 if (contentBytes.length > 0 && contentBytes.length < 800) { boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes); for (int i = 0; i < codeOut.length; i++) { for (int j = 0; j < codeOut.length; j++) { if (codeOut[j][i]) { gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3); } } } } else { throw new Exception("QRCode content bytes length = " + contentBytes.length + " not in [0, 800]."); } gs.dispose(); bufImg.flush(); } catch (Exception e) { e.printStackTrace(); } return bufImg; } /** * 解析二维码(QRCode) * @param input 输入流 * @return */ public String decoderQRCode(InputStream input) { BufferedImage bufImg = null; String content = null; try { bufImg = ImageIO.read(input); QRCodeDecoder decoder = new QRCodeDecoder(); content = new String(decoder.decode(new TwoDimensionCodeImage(bufImg)), "utf-8"); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); e.printStackTrace(); } catch (DecodingFailedException dfe) { System.out.println("Error: " + dfe.getMessage()); dfe.printStackTrace(); } return content; } }
生成的二维码:
扫码后,自动跳转到我的博客。
所需jar包下载(不用积分):
http://download.csdn.net/download/yushouling/9751484
java生成和解析二维码实战——QRCode相关推荐
- java生成以及解析二维码
java生成以及解析二维码 欢迎使用Markdown编辑器 新的改变 功能快捷键 合理的创建标题,有助于目录的生成 如何改变文本的样式 插入链接与图片 如何插入一段漂亮的代码片 生成一个适合你的列表 ...
- Java生成和解析二维码工具类(简单经典)
Java生成和解析二维码工具类 开箱即用,简单不废话. pom.xml引入依赖 <!-- https://mvnrepository.com/artifact/com.google.zxing/ ...
- Java生成和解析二维码
前言:曾经有做过不少微信公众号和移动网站的项目,对二维码还算有点了解,刚收到这个任务的时候就想着竟然要用二维码存文本,那就得先考究一下这小小的二维码到底能存多少的东西了. 需求:使用二维码存放文本(x ...
- Java生成与解析二维码
1.下载支持二维码的jar包qrcode.jar和qrcode_swetake.jar, 其中qrcode_swetake.jar用于生成二维码,rcode.jar用于解析二维码,jar包下载地址(免 ...
- java生成与解析二维码 支持插入图片与文字
1.依赖: <!-- https://mvnrepository.com/artifact/com.google.zxing/core --><dependency><g ...
- java 生成、解析二维码并在二维码中添加样式
https://blog.csdn.net/yxj13935213026/article/details/81017902
- Java实现生成和解析二维码
Java实现生成和解析二维码 文章目录 Java实现生成和解析二维码 一.建立项目 二.创建工具类 三.创建启动类 一.建立项目 首先需要创建一个普通的 Maven 项目,在这里我用的是 google ...
- java利用zxing来生成和解析二维码,支持中文
java在二维码的生成和解析上,网上有些人说如果要解析中文,得去修改工具包的Encoder类中的 static final String DEFAULT_BYTE_MODE_ENCODING = &q ...
- 使用zxing生成与解析二维码
随着二维码的普及,二维码在生活中的使用使用的场景也越来越来多,本文章就来介绍使用zxing来生成与解析二维码.生成二维码的开源项目很多,选择zxing则是因为其出自Google并且长期有人进行维护,值 ...
最新文章
- springboot集成rabbitMQ安装+代码
- 转载 程序员上帝视角解读“旅行青蛙”,你的呱真的在旅行嘛? (手机游戏)...
- Visual Studio 常用快捷键 (二)
- 前端学习(1550):$scope和调试工具
- 记录几个CentOS安装包(rpm)的下载地址-离线安装必备
- 常见视频高速接口分析MIPI,LVDS,RGB,HDMI
- AutoJs学习-实现取色器
- ssm电影院订票系统 前端layui
- 基于Tesseract的OCR识别--身份证
- 2016年高校保送生拟录取名单(清华大学)
- 计算机系统与外部交换信息主要通过显示器,微机系统与外部交换信息主要通过什么设备...
- windows 安装SNMP MIB Browser
- Integrator积分器测试(Simulink仿真)
- 深入理解Flutter动画原理,已整理成文档
- linux配置静态IP(手把手)
- Cisco 开启三层交换机ip routing
- java获取凌晨的时间戳
- 常见的语言表示模型(词嵌入、句表示、篇章表示)
- 宛如秋兮 - 个人发展规划(职业规划)
- java web 服务 构建与运行_Java Web 服务:构建与运行(影印版)