注意:这里我不提供所需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版的更多相关文章

  1. Java 二维码生成工具类

    /** * 二维码 工具 * * @author Rubekid * */ public class QRcodeUtils { /** * 默认version */ public static fi ...

  2. 你不可错过的二维码生成与解析-java后台与前端js都有

    1.二维码分类   二维条码也有许多不同的码制,就码制的编码原理而言,通常分为三种类型. 线性堆叠式二维码 编码原理: 建立在一维条码基础之上,按需要堆积成两行或多行. 图示: 矩阵式二维码 最常用编 ...

  3. 谷歌zxing 二维码生成工具

    一.加入maven依赖 <!-- 谷歌zxing 二维码 --> <dependency> <groupId>com.google.zxing</groupI ...

  4. vue项目条形码和二维码生成工具试用

    项目开发需要,优惠券分不同类型,简单的使用id生成条形码供店铺使用,麻烦点的需要多个字段的就需要使用二维码来展示了,对应的效果如下 条形码(一维码)使用工具code128 需引入code128.js ...

  5. java二维码生成工具

    import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.ut ...

  6. iOS 原生实现生成二维码(CoreImage)工具类,不依赖第三方库,可自定义背景颜色,添加logo(Swift 4.0)

    import Foundation import CoreImage import UIKit extension UIColor { var coreImageColor: CIColor { re ...

  7. 开发ASP.NET MVC 开发名片二维码生成工具 (原创)

    在网上找了很多,都只能生成网址,不能生成名片二维码,于是自己动手. 第一步,写视图界面,主要代码如下: <script type="text/javascript"> ...

  8. 二维码生成工具——QRCode

    下载QRCode的源代码:https://github.com/davidshimjs/qrcodejs 引入项目中:<script type="text/javascript&quo ...

  9. 把url链接转换成二维码的工具类

    import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io ...

随机推荐

  1. 【csp模拟赛6】计数--单调栈

    对于60%的数据:暴力枚举对于100%的数据:因为排列是随机的,所以从每个点向后可能的差值最多2logn个,所以答案最多只可能有nlogn种,用单调队列找出来统计即可 维护对于每个位置,向右能影响到的 ...

  2. LibreOJ #113. 最大异或和

    二次联通门 : LibreOJ #113. 最大异或和 /* LibreOJ #113. 最大异或和 线性基 插入 与 查询最大值 说一下我在学习线性基时遇到的一些问题 1.线性基指的是一个数集 2. ...

  3. ECMAScript 5.0 基础语法(上)

    银子: 一般来说,一门编程语言的基础语法都是大同小异的.比如,python的基础语法,包括:数据类型,变量,作用域,运算符,流程控制(if...else...语句),循环,编码,数据类型的操作(增删改 ...

  4. codeforces gym #101987K -TV ShowGame(2-SAT)

    题目链接: https://codeforces.com/gym/101987 题意: 有长度为$n$的只包含$B,R$的字符串 有m种关系,每个关系说出三个位置的确切字符 这三个位置的字符最多有一个 ...

  5. oracle查找表索引信息

    select owner,index_name,index_type from all_indexes where owner='xxxx' and table_name='xxx' select * ...

  6. python+socket+jq实现web页面实时输出结果

    例如有这样一个需求: 在终端上进行ping操作,现在想把这个这个操作放到web页面上进行,并且实现实时输出的效果. 来分析下具体实现过程 第一步,传统的http请求实现这个有点不太友好,因为这里边是一 ...

  7. spark-submit 参数总结

    spark-submit 可以提交任务到 spark 集群执行,也可以提交到 hadoop 的 yarn 集群执行. 1)./spark-shell --help   :不知道如何使用,可通过它查看命 ...

  8. PHP学习之观察者模式

    <?php //观察者模式涉及到两个类 //男人类 和女朋友类 //男人类对象小明, 女朋友类对象小花.小丽 class Man { //用了存放观察者 protected $observers ...

  9. vim 快捷键 清空文件所有内容

    vim清空文件所有内容 在使用vim编辑器的时候,有时候编辑一个文件,而文件内容比较多,如果需要快速清空整个文件,可以使用一下命令: 在命令模式下,首先执行 gg 这里是跳至文件首行 再执行: dG ...

  10. C++ fill fill_n generate generate_n

    #include <iostream>#include <algorithm>#include <vector>#include <list>#incl ...