二维码生成工具类java版
注意:这里我不提供所需jar包的路径,我会把所有引用的jar包显示出来,大家自行Google
- package com.net.util;
- import java.awt.BasicStroke;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Image;
- import java.awt.Shape;
- import java.awt.geom.RoundRectangle2D;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.OutputStream;
- import java.util.Hashtable;
- import java.util.Random;
- import javax.imageio.ImageIO;
- import com.google.zxing.BarcodeFormat;
- import com.google.zxing.BinaryBitmap;
- import com.google.zxing.DecodeHintType;
- import com.google.zxing.EncodeHintType;
- import com.google.zxing.MultiFormatReader;
- import com.google.zxing.MultiFormatWriter;
- import com.google.zxing.Result;
- import com.google.zxing.common.BitMatrix;
- import com.google.zxing.common.HybridBinarizer;
- import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
- /********************************************************************************
- * Description: 二维码制作工具类
- * @author zhangdi
- * @version 1.0
- */
- 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;
- /**
- * 生成二维码
- * @param content 源内容
- * @param imgPath 生成二维码保存的路径
- * @param needCompress 是否要压缩
- * @return 返回二维码图片
- * @throws Exception
- */
- private static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception {
- Hashtable hints = new Hashtable();
- 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;
- }
- /**
- * 在生成的二维码中插入图片
- * @param source
- * @param imgPath
- * @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
- * @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 file;
- }
- public static void mkdirs(String destPath) {
- File file = new File(destPath);
- // 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir。(mkdir如果父目录不存在则会抛出异常)
- if (!file.exists() && !file.isDirectory()) {
- file.mkdirs();
- }
- }
- public static void encode(String content, String imgPath, String destPath) throws Exception {
- QRCodeUtil.encode(content, imgPath, destPath, false);
- }
- public static void encode(String content, String destPath, boolean needCompress) throws Exception {
- QRCodeUtil.encode(content, null, destPath, needCompress);
- }
- public static void encode(String content, String destPath) throws Exception {
- QRCodeUtil.encode(content, null, destPath, false);
- }
- 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);
- }
- 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 hints = new Hashtable();
- hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
- result = new MultiFormatReader().decode(bitmap, hints);
- String resultStr = result.getText();
- return resultStr;
- }
- public static String decode(String path) throws Exception {
- return QRCodeUtil.decode(new File(path));
- }
- // try {
- ////String text = "http://weifeng.nethangzhou.com/wf/weixin/getUserInfoCode";
- //String text = "http://www.lechuan100.com/plugin.php?id=haven_article&mod=my&aid=21";
- ////生成带logo 的二维码
- ////QRCodeUtil.encode(text, "d:/730.png", "d:/wfqrcode", true);
- ////生成不带logo 的二维码
- //QRCodeUtil.encode(text,"","d:/wfqrcode",true);
- //
- ////指定二维码图片,解析返回数据
- ////System.out.println(QRCodeUtil.decode("D:/WPS/75040887.jpg"));
- //} catch (Exception e) {
- //e.printStackTrace();
- //}
- }
二维码生成工具类java版的更多相关文章
- Java 二维码生成工具类
/** * 二维码 工具 * * @author Rubekid * */ public class QRcodeUtils { /** * 默认version */ public static fi ...
- 你不可错过的二维码生成与解析-java后台与前端js都有
1.二维码分类 二维条码也有许多不同的码制,就码制的编码原理而言,通常分为三种类型. 线性堆叠式二维码 编码原理: 建立在一维条码基础之上,按需要堆积成两行或多行. 图示: 矩阵式二维码 最常用编 ...
- 谷歌zxing 二维码生成工具
一.加入maven依赖 <!-- 谷歌zxing 二维码 --> <dependency> <groupId>com.google.zxing</groupI ...
- vue项目条形码和二维码生成工具试用
项目开发需要,优惠券分不同类型,简单的使用id生成条形码供店铺使用,麻烦点的需要多个字段的就需要使用二维码来展示了,对应的效果如下 条形码(一维码)使用工具code128 需引入code128.js ...
- java二维码生成工具
import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.ut ...
- iOS 原生实现生成二维码(CoreImage)工具类,不依赖第三方库,可自定义背景颜色,添加logo(Swift 4.0)
import Foundation import CoreImage import UIKit extension UIColor { var coreImageColor: CIColor { re ...
- 开发ASP.NET MVC 开发名片二维码生成工具 (原创)
在网上找了很多,都只能生成网址,不能生成名片二维码,于是自己动手. 第一步,写视图界面,主要代码如下: <script type="text/javascript"> ...
- 二维码生成工具——QRCode
下载QRCode的源代码:https://github.com/davidshimjs/qrcodejs 引入项目中:<script type="text/javascript&quo ...
- 把url链接转换成二维码的工具类
import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io ...
随机推荐
- AtCoder Beginner Contest 132
目录 Contest Info Solutions A. Fifty-Fifty B. Ordinary Number C. Divide the Problems D. Blue and Red B ...
- 异步时钟FIFO(一)
FIFO一般用于通过两个不同时钟域的数据传输.一个水池有进和出两个通道,由于进出口水流不一致所以需要水池加以缓冲.堆栈也是相当于水池的作用.如果输入端不是连续的数据流,可以通过堆栈来调节使数据以稳定的 ...
- Selenium结合BeautifulSoup4编写简单爬虫
在学会了抓包,接口请求(如requests库)和Selenium的一些操作方法后,基本上就可以编写爬虫,爬取绝大多数网站的内容. 在爬虫领域,Selenium永远是最后一道防线.从本质上来说,访问网页 ...
- UOJ #164 [清华集训2015]V (线段树)
题目链接 http://uoj.ac/problem/164 题解 神仙线段树题. 首先赋值操作可以等价于减掉正无穷再加上\(x\). 假设某个位置从前到后的操作序列是: \(x_1,x_2,..., ...
- Linux设备驱动程序 之 get_free_page
get_free_page 如果模块需要分配大块的内存,使用面向页的分配会有很多优点: 分配页面可使用下面的函数: unsigned long get_zeroed_page(gfp_t gfp_ma ...
- ArcGIS Python 唯一值专题
import arcpy mxd = arcpy.mapping.MapDocument("current") lyr = arcpy.mapping.ListLayers(mxd ...
- [GIT]比较不同分支的差异
比如我们有 2 个分支:master, dev,现在想查看这两个 branch 的区别,有以下几种方式: undefined 1.查看 dev 有,而 master 中没有的: 1.查看 de ...
- ForkJoinPool 源码分析
ForkJoinPool ForkJoinPool 是一个运行 ForkJoinTask 任务.支持工作窃取和并行计算的线程池 核心参数+创建实例 // 工作者线程驻留任务队列索引位 static f ...
- linux:解决SSH连接Linux超时自动断开
用SSH登录到Linux的时候,由于默认的连接超时时间很短,经常断开! 1.修改文件 # vi /etc/ssh/sshd_config 2.重启sshd服务 # /etc/init.d/sshd r ...
- mongodb download
https://www.mongodb.org/dl/win32/x86_64-2008plus-ssl