今天心情好,研究了一下二维码,说起来二维码用起来,真的挺方便的,我用的是Google提供的 zxing包,下面可以简单看一下我做的效果实现的带头像,不带文字的,没有使用Bitmap这个类,使用的是BufferedImage来写图片文件

实现效果

带头像,小号的二维码

带头像的二维码

没有头像的二维码

设置红色的二维码

环境搭建

<dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.3.0</version>
</dependency><!-- https://mvnrepository.com/artifact/com.google.zxing/javase -->
<dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.3.0</version>
</dependency>

工具类代码

工具类使用的时候,需要自己更改一下工具类里面配置的字体以及生成的二维码大小等参数

package com.yellowcong.test;import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;
import java.util.UUID;import javax.imageio.ImageIO;import com.google.zxing.BarcodeFormat;
import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;public class QRCodeUtils {// 二维码宽度private static Integer WIDTH_PIX = 300;// 二维码高度private static Integer HEIGHT_PIX = 300;// 生成文件类型private static String TYPE = "png";// 编码格式private static String CHAR_TYPE = "UTF-8";/** 二维码填充颜色 BEGIN*/// 黑色private static Integer COLOR_BLACK = 0XFF000000;// 白色 0xFF是补码private static Integer COLOR_WHITE = 0XFFFFFFFF;// 红色private static Integer COLOR_RED = 0xFFFF0000;// 蓝色 ,0xFF  #FFD700private static Integer COLOR_BLUE = 0xFFFFD700;/** 二维码填充颜色 END*//**字体设定 BEGIN*///设定写在二维码上的字体 样式private static String FONT_NAME = "Consolas";//字体颜色private static Color FONT_COLOR =  Color.gray;//字体 大小private static Integer FONT_SIZE =12;/**字体设定 END*//*** 读取二维码的文件里面的信息* * @param filePath* @return* @throws Exception*/public static String readQRImg(String filePath) throws Exception {// 读取图片BufferedImage image = ImageIO.read(new File(filePath));LuminanceSource source = new BufferedImageLuminanceSource(image);Binarizer binarizer = new HybridBinarizer(source);BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();hints.put(DecodeHintType.CHARACTER_SET, CHAR_TYPE);Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 对图像进行解码return result.getText();}/*** 通过BufferedImage 来写入图片的操作,没有用到Google 的j2se包* * @param text*            写入的信息* @param filePath*            输出的文件地址* @return* @throws Exception*/public static File writeQRImg(String text, String filePath,String... logoPath) throws Exception {// 配置参数Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();// 字符编码hints.put(EncodeHintType.CHARACTER_SET, CHAR_TYPE);// 容错级别hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 设置空白边距的宽度hints.put(EncodeHintType.MARGIN, 3); // 默认是4// 1、生成二维码BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, WIDTH_PIX, HEIGHT_PIX, hints);// 2、获取二维码宽高int codeWidth = bitMatrix.getWidth();int codeHeight = bitMatrix.getHeight();// 3、将二维码放入缓冲流BufferedImage image = new BufferedImage(codeWidth, codeHeight, BufferedImage.TYPE_INT_RGB);for (int i = 0; i < codeWidth; i++) {for (int j = 0; j < codeHeight; j++) {// 4、循环将二维码内容定入图片//判断 BitMatrix 是否存在像素image.setRGB(i, j, bitMatrix.get(i, j) ? COLOR_BLACK : COLOR_WHITE);}}//判断是否写入logo图片 if(logoPath != null && logoPath.length>0){File logoPic = new File(logoPath[0]);  if (logoPic.exists()) {  //在原来基础上,再添加一个图片Graphics2D g = image.createGraphics();  BufferedImage logo = ImageIO.read(logoPic);  int widthLogo = logo.getWidth(null) > image.getWidth() * 2 / 10 ?   (image.getWidth() * 2 / 10) : logo.getWidth(null);  int heightLogo = logo.getHeight(null) > image.getHeight() * 2 / 10 ?   (image.getHeight() * 2 / 10) : logo.getHeight(null); //设定在图片中间int x = (image.getWidth() - widthLogo) / 2;   int y = (image.getHeight() - heightLogo) / 2;  // 开始绘制图片  g.drawImage(logo, x, y, widthLogo, heightLogo, null);  //绘制圆角矩形g.drawRoundRect(x, y, widthLogo, heightLogo, 15, 15);  //边框宽度  g.setStroke(new BasicStroke(2));  //边框颜色  g.setColor(Color.WHITE);  g.drawRect(x, y, widthLogo, heightLogo);  //最后一个参数用来设置字体的大小  Font fontStyle = new Font(FONT_NAME,Font.PLAIN,FONT_SIZE);//设定颜色g.setColor(FONT_COLOR); //设定颜色g.setFont(fontStyle); //设定字体Integer fontStartIndex = image.getWidth()-(int) (text.length()*FONT_SIZE*0.7);g.drawString(text,fontStartIndex,image.getHeight()-FONT_SIZE);//释放图像资源g.dispose();  logo.flush();  image.flush();  }  }// 获取输出的图片File outPutImage = getQRImgFile(filePath);// 5、将二维码写入图片ImageIO.write(image, TYPE, outPutImage);return outPutImage;}/*** 获取二维码图片* * @param path*            图片写出的路径比如 C:/ 或者 D:/test/xx* @return* @throws Exception*/private static File getQRImgFile(String filePath) throws Exception {// 获取图片名称String fileName = UUID.randomUUID().toString() + "." + TYPE;File outPutImage = new File(filePath + File.separator + fileName);// 如果图片不存在创建图片if (!outPutImage.exists()) {outPutImage.createNewFile();}return outPutImage;}/*** 创建二维码,通过BitMatrix 这个类来生成二维码<br/>* 用到了gooogle的j2se的包* * @param text*            需要写入的信息,是json数据类型.也可以是 http://yellowcong.com 等* @param filePath*            数据的文件地址路径* @throws Exception*/@SuppressWarnings("deprecation")public static File writeQRImg4J2se(String text, String filePath) throws Exception {// 配置参数Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();// 字符编码hints.put(EncodeHintType.CHARACTER_SET, CHAR_TYPE);// 容错级别hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 设置空白边距的宽度hints.put(EncodeHintType.MARGIN, 3); // 默认是4// 图像数据转换,使用了矩阵转换 ,这种方法适合在Android机器上使用BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, WIDTH_PIX, HEIGHT_PIX, hints);// 获取输出的图片File outPutImage = getQRImgFile(filePath);// 写出成文件,设置文件地址// 这个是google j2se 这个包里面的apiMatrixToImageWriter.writeToFile(bitMatrix, TYPE, outPutImage);return outPutImage;}}

测试类

package com.yellowcong.test;import java.awt.Color;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Random;import org.junit.Test;import com.yellowcong.test.ColorUtils.Colors;public class QRUtilsTest2 {/*** 不带logo的二维码* @throws Exception*/@Testpublic void testWriteQRImg() throws Exception{String outPath = "C:/Users/yellowcong/Desktop/qr/test";QRCodeUtils.writeQRImg("http://baidu.com", outPath);}/*** 带logo的二维码* @throws Exception*/@Testpublic void testWriteQRImgWithLogo() throws Exception{String logoPath = "C:/Users/yellowcong/Desktop/qr/logo/logo.jpg";String outPath = "C:/Users/yellowcong/Desktop/qr/test";QRCodeUtils.writeQRImg("http://www.baidu.com", outPath,logoPath);}/*** 读取二维码信息* @throws Exception*/@Testpublic void testReadQRImg() throws Exception{//读取二维码信息String info =QRCodeUtils.readQRImg("C:/Users/yellowcong/Desktop/qr/test/15e2ff56-14be-4cf0-ad18-891e58152c42.png");System.out.println(info);}}

Java之二维码工具包-yellowcong相关推荐

  1. java生成二维码技术实现

    一.maven依赖 下载google的二维码工具包来进行二维码的生成,下面是jar包的maven下载 <dependency><groupId>com.google.zxing ...

  2. java 生成二维码,解析二维码

    今天遇到需求,使用Java生成二维码图片,网搜之后,大神们早就做过,个人总结一下. 目标:借助Google提供的ZXing Core工具包,使用Java语言实现二维码的生成和解析. 步骤如下: 1.m ...

  3. Java生成二维码,扫描后跳转到指定的网站

    转自:https://suko.iteye.com/blog/2244138 本例我是应用google的二维码工具包来做的,附近提供jar包下载.下面是完整代码,经过测试的. jar下载路径:http ...

  4. java生成二维码图片、转base64

    本文介绍通过java把文字或url生成二维码,使用浏览器或者微信扫一扫即可获得文字或url内容,超简单的方法,两个步骤复制粘贴即可使用. 注意:内容是文字会直接显示,如果内容为url地址那么会直接访问 ...

  5. Java生成二维码的两种方法

    本文介绍Java生成二维码的两种方法,这两种方法都依赖于google提供的二维码依赖包. 一种是自己写工具类,代码可以网上抄. 另一种是使用hutool第三方工具类的依赖包,不用自己抄代码. 一.自定 ...

  6. java 生成二维码 QRCode、zxing 两种方式

    版权声明:本文为 testcs_dn(微wx笑) 原创文章,非商用自由转载-保持署名-注明出处,谢谢. https://blog.csdn.net/testcs_dn/article/details/ ...

  7. Java实现二维码技术探讨。

    Java生成二维码方法有三种: 1: 使用SwetakeQRCode在Java项目中生成二维码  http://swetake.com/qr/ 下载地址  或着http://sourceforge.j ...

  8. Java实现二维码生成

    title: Java实现二维码生成 categories: "Java" 前言 日前生活上存在很多扫描二维码的场景,支付.加好友-等操作都需要扫描二维码,然后我就在想Java能不 ...

  9. 二维码相关---java生成二维码名片,并且自动保存到手机通讯录中...

    二维码相关---java生成二维码名片,并且自动保存到手机通讯录中... 技术qq交流群:JavaDream:251572072 1.首先介绍一个api.   Zxing是Google提供的关于条码 ...

最新文章

  1. java贪吃蛇不能回头,儿时回忆!泪流满面,Java 实现贪吃蛇游戏的示例(附代码)...
  2. Android--应用开发3(Android layout XML属性)
  3. python知识合集
  4. MATLAB怎么查找矩阵中所有0的数据并赋值
  5. 微信小程序页面跳转后清除定时器
  6. mysql消除重复行的关键字_MySQL 消除重复行的一些方法
  7. VS-c# web程序:gridview保存Excel文件遇到的问题
  8. 闪光css,CSS实现的一闪而过的图片闪光效果
  9. 计算机组装技术的心得与体会,计算机技术心得体会
  10. 巩固——React的详细安装教程
  11. 使用MS Test进行单元测试
  12. 2.ELK stack 集群 搭建
  13. python微信语音转发方法_最全的微信语音转发方法,最简单的方式大家已经帮你选出来了...
  14. 怎样用计算机10,win10计算器怎么用_win10如何打开计算器
  15. 太阳光轨迹软件_全球各地太阳全年日行路线图:呈8字曲线轨迹
  16. 每天吃一个核桃好处多多,坚持半年以上,身体会发生五种变化
  17. vue博客模板—Fblog
  18. Java游戏编程不完全详解-2
  19. 相似对角化与二次型的标准化联系
  20. Word中如何快速删除页眉下的横线?教你一招,轻松解决!

热门文章

  1. 可以用来生成连续有逻辑视觉网络训练设计
  2. [Pro.android.3][读书笔记]Android 01 Android简介 连载
  3. 从M1、Grace再到华为,缝合风为何会在芯片大厂中流行
  4. 高德地图搜索结果点击事件
  5. 高德地图搜索以后生成的marker的点击事件
  6. SVM的“三重境界”
  7. 2021-03-4:task04_NFM模型
  8. 新消费到底「新」在哪里?
  9. 预估市场过万亿,“即时零售”到底是什么来头?
  10. “粽子第一股“来了!老字号五芳斋还有新故事?