二维码

二维条形码能够在横向和纵向两个方位同时表达信息,因此能在很小的面积内表达大量的信息。

二维码又称二维条码,常见的二维码为QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型。

二维条码/二维码(2-dimensional bar code)是用某种特定的几何图形按一定规律在平面(二维方向上)分布的、黑白相间的、记录数据符号信息的图形;在代码编制上巧妙地利用构成计算机内部逻辑基础的“0”、“1”比特流的概念,使用若干个与二进制相对应的几何形体来表示文字数值信息,通过图象输入设备或光电扫描设备自动识读以实现信息自动处理:它具有条码技术的一些共性:每种码制有其特定的字符集;每个字符占有一定的宽度;具有一定的校验功能等。同时还具有对不同行的信息自动识别功能、及处理图形旋转变化点。

zxing

zxing项目是谷歌推出的用来识别多种格式条形码的开源项目。在Zxing中,使用BitMatrix来描述一个二维码,在其内部存储一个看似boolean值的矩阵数组。这个类很好的抽象了二维码。

zxing有多个人在维护,覆盖主流编程语言,也是目前还在维护的较受欢迎的二维码扫描开源项目之一,主要的核心代码在core文件夹里面。

项目地址

GitHub - zxing/zxing: ZXing ("Zebra Crossing") barcode scanning library for Java, Androidhttps://github.com/zxing/zxing

帮助文档

https://github.com/zxing/zxing/wiki/Getting-Started-Developinghttps://github.com/zxing/zxing/wiki/Getting-Started-Developing

依赖的引入

只使用zxing-core包,那么我们最多可以得到一个BitMatrix, 我们想要看见二维码,则还需要将其转换成一个图片,而图片在不同的平台则是以不同的形式存在的。如png文件, jpg文件、Android的Bitmap, Java SE的 BufferedImage.

        <dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.1.0</version></dependency><dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.1.0</version></dependency>

具体使用

1.用zxing创建普通二维码

    /*** 用zxing创建普通二维码* @param width* @param height* @param format * @param content* @param path* @return*/public static BufferedImage createImage(int width, int height,String format , String content ,String path) {HashMap map = new HashMap();//设置容错等级map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//定义字符集,我们设置为utf-8map.put(EncodeHintType.CHARACTER_SET, "UTF-8");//设置边距,二维码边距空白宽度为0map.put(EncodeHintType.MARGIN, 2);BufferedImage image = null;try {//生成二维码对象BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, map);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);}}Path file = new File(path).toPath();ImageIO.write(image, format, file.toFile());} catch (WriterException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return image;}

main方法中

    public static void main(String[] args) {createImage(300,300,"png","用zxing创建普通二维码","D:\\QRCode\\zxingNormal.png");}

结果

2.用zxing创建带有logo的二维码

    /*** 给二维码插入logo图片* @param image* @param logoPath* @return*/public static BufferedImage insertLogo(BufferedImage image, InputStream logoPath) {try {BufferedImage logo = ImageIO.read(logoPath);int width = logo.getWidth();int height = logo.getHeight();if (width > 100) {width = 100;}if (height > 100) {height = 100;}//压缩LogoImage logoTemp = logo.getScaledInstance(width, height, Image.SCALE_SMOOTH);BufferedImage bfImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);//获取画布Graphics graphics = bfImage.getGraphics();//画logoTempgraphics.drawImage(logoTemp, 0, 0, null);graphics.dispose();//获取原二维码Graphics2D graphicsQRCode = image.createGraphics();//logo插入二维码,定义位置x,y 中间位置int x = (image.getWidth() - width) / 2;int y = (image.getHeight() - height) / 2;//压缩后的logo画进二维码graphicsQRCode.drawImage(logoTemp, x, y, null);//logo的四个角圆滑  两个6定义弧度Shape shape = new RoundRectangle2D.Float(x, y, width, height, 6, 6);//设置画笔的粗细graphicsQRCode.setStroke(new BasicStroke(3f));//画到画布graphicsQRCode.draw(shape);//释放资源graphicsQRCode.dispose();} catch (IOException e) {e.printStackTrace();}return image;}
insertLogo方法可在创建普通二维码时调用
    /*** 用zxing创建普通二维码* @param width* @param height* @param format* @param content* @param path* @param logoPath* @return*/public static BufferedImage createImage(int width, int height, String format, String content, String path , InputStream logoPath) {HashMap map = new HashMap();//设置容错等级map.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//定义字符集,我们设置为utf-8map.put(EncodeHintType.CHARACTER_SET, "UTF-8");//设置边距,二维码边距空白宽度为0map.put(EncodeHintType.MARGIN, 2);BufferedImage image = null;try {//生成二维码对象BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, map);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(!StringUtils.isEmpty(logoPath)){insertLogo(image,logoPath);}Path file = new File(path).toPath();ImageIO.write(image, format, file.toFile());} catch (WriterException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return image;}

main方法

    public static void main(String[] args) {File file = new File("D:\\QRCode\\logo.png");try {InputStream in = new FileInputStream(file);createImage(300, 300, "png", "用zxing创建带logo二维码", "D:\\QRCode\\zxingLogo.png",in);} catch (FileNotFoundException e) {e.printStackTrace();}}

logo图片

结果

qrcode-plugin

 来源于一个开源项目中一部分,qrcode-plugin 在开源二维码处理库zxing的基础上,重写了二维码渲染的逻辑,以支持更灵活、更个性化的二维码生成规则,封装了二维码的生成和解析,深度定制了二维码的生成,支持各种酷炫二维码的产生。

项目地址mirrors / liuyueyi / quick-media · GitCodehttps://gitcode.net/mirrors/liuyueyi/quick-media

操作文档

QuickMediaDescriptionhttps://liuyueyi.github.io/quick-media/#/%E6%8F%92%E4%BB%B6/%E4%BA%8C%E7%BB%B4%E7%A0%81/%E4%BA%8C%E7%BB%B4%E7%A0%81%E6%8F%92%E4%BB%B6%E4%BD%BF%E7%94%A8%E6%89%8B%E5%86%8C

依赖引入

        <dependency><groupId>com.github.liuyueyi.media</groupId><artifactId>qrcode-plugin</artifactId><version>2.5.2</version></dependency>

具体实现

1.用qrcode-plugin实现普通二维码

    /*** 用qrcode-plugin实现普通二维码* @param content* @param format* @param path* @return*/public static BufferedImage QrCode_Normal(String content ,String format ,String path) {BufferedImage image = null;try {image = QrCodeGenWrapper.of(content).asBufferedImage();Path file = new File(path).toPath();ImageIO.write(image, format, file.toFile());} catch (IOException e) {e.printStackTrace();} catch (WriterException e) {e.printStackTrace();}return image;}

main方法

    public static void main(String[] args) {QrCode_Normal("用qrcode-plugin创建普通二维码","png","D:\\QRCode\\qrcodePluginNormal.png");}

结果

2.用qrcode-plugin实现带logo的二维码

    /*** 用QrCode生成带logo的二维码* @param content* @param logo* @return*/public static BufferedImage QrCode_Logo(String content, String format, String path, InputStream logo) {BufferedImage image = null;try {image = QrCodeGenWrapper.of(content).setLogo(logo).setLogoRate(7) //设置比例.setLogoStyle(QrCodeOptions.LogoStyle.ROUND)  //设置四个角的样式.asBufferedImage();Path file = new File(path).toPath();ImageIO.write(image, format, file.toFile());} catch (IOException e) {e.printStackTrace();} catch (WriterException e) {e.printStackTrace();}return image;}

main方法

    public static void main(String[] args) {File file = new File("D:\\QRCode\\logo.png");try {InputStream in = new FileInputStream(file);QrCode_Logo("用qrcode-plugin创建带logo二维码", "png", "D:\\QRCode\\qrcodePluginLogo.png", in);} catch (FileNotFoundException e) {e.printStackTrace();}}

结果

3.用qrcode-plugin实现彩色二维码

    /*** QrCode生成彩色的二维码* @param content* @return*/public static BufferedImage QrCode_Color(String content,String format ,String path) {BufferedImage image = null;try {image = QrCodeGenWrapper.of(content).setDrawPreColor(Color.RED)  //画的着色点 也就是二维码的黑色设置为其他颜色.asBufferedImage();Path file = new File(path).toPath();ImageIO.write(image, format, file.toFile());} catch (IOException e) {e.printStackTrace();} catch (WriterException e) {e.printStackTrace();}return image;}

main方法

    public static void main(String[] args) {
QrCode_Color("用qrcode-plugin创建彩色二维码","png","D:\\QRCode\\qrcodePluginColor.png");}

结果

4.用qrcode-plugin实现有背景的二维码

    /*** QrCode实现背景图片的二维码* @param content* @param bgImage* @return*/public static BufferedImage QrCode_Baground(String content, InputStream bgImage ,String format ,String path) {BufferedImage image = null;try {image = QrCodeGenWrapper.of(content).setBgImg(bgImage)  //默认是背景全覆盖二维码.setBgOpacity(0.5f)   //设置透明度.asBufferedImage();Path file = new File(path).toPath();ImageIO.write(image, format, file.toFile());} catch (IOException e) {e.printStackTrace();} catch (WriterException e) {e.printStackTrace();}return image;}

main方法

    public static void main(String[] args) {File file = new File("D:\\QRCode\\background.png");try {InputStream in = new FileInputStream(file);QrCode_Baground("用qrcode-plugin创建带背景二维码",in,"png","D:\\QRCode\\qrcodePluginBaground.png");} catch (FileNotFoundException e) {e.printStackTrace();}}

背景图片

结果

4.用qrcode-plugin实现特殊形状的二维码

    /*** QrCode实现特殊形状的二维码* @param content* @param format * @param path * @return*/public static BufferedImage QrCode_Shape(String content ,String format ,String path) {BufferedImage image = null;try {image = QrCodeGenWrapper.of(content).setDrawEnableScale(true)//黑色方块变原点.setDrawStyle(QrCodeOptions.DrawStyle.CIRCLE).asBufferedImage();Path file = new File(path).toPath();ImageIO.write(image, format, file.toFile());} catch (IOException e) {e.printStackTrace();} catch (WriterException e) {e.printStackTrace();}return image;}

main方法

    public static void main(String[] args) {QrCode_Shape("用qrcode-plugin创建特殊形状的二维码","png","D:\\QRCode\\qrcodePluginShape.png");}

结果

5.用qrcode-plugin实现用图片取代方块填充二维码

    /*** QrCode实现用图片取代方块填充二维码,用小图标* @param content* @param tcImage* @param format * @param path * @return*/public static BufferedImage QrCode_imgTC(String content, InputStream tcImage ,String format ,String path) {BufferedImage image = null;try {image = QrCodeGenWrapper.of(content).setDrawEnableScale(true)//黑色方块变图片.setDrawStyle(QrCodeOptions.DrawStyle.IMAGE)//设置精度.setErrorCorrection(ErrorCorrectionLevel.H)//设置一个坐标 从哪开始.addImg(1, 1, tcImage).asBufferedImage();Path file = new File(path).toPath();ImageIO.write(image, format, file.toFile());} catch (IOException e) {e.printStackTrace();} catch (WriterException e) {e.printStackTrace();}return image;}

main方法

    public static void main(String[] args) {File file = new File("D:\\QRCode\\logo.png");InputStream in = null;try {in = new FileInputStream(file);QrCode_imgTC("用qrcode-plugin创建用图片取代方块填充二维码",in,"png","D:\\QRCode\\qrcodePluginTcImg.png");} catch (FileNotFoundException e) {e.printStackTrace();}}

这里我用logo图片来取代方块

结果

最后

如果二维码需要显示到前端页面,需要先把二维码转换为字符串传给前端,然后前端把返回的数据再回显二维码展示在页面上。

二维码图片转成base64的字符串

    public static String imageToString(BufferedImage image) {ByteArrayOutputStream os = new ByteArrayOutputStream();try {ImageIO.write(image, "png", os);} catch (IOException e) {e.printStackTrace();}//转base64的字符串return Base64.encodeBase64String(os.toByteArray());}

Java分别使用zxing及qrcode-plugin生成各种样式二维码相关推荐

  1. Zxing和QR CODE 生成与解析二维码实例(普通篇)

    首先下载对应的jar包,本实例用的是Zxing2.2jar 下载地址:http://download.csdn.net/detail/gao36951/8161861 Zxing是Google提供的关 ...

  2. Zxing和QR CODE 生成与解析二维码实例(带logo篇)

    上一篇介绍了普通的二位码的生成与解析,本篇来介绍两种工具类生成带Logo的二维码的实例 下载jar包地址:http://download.csdn.net/detail/gao36951/816186 ...

  3. vue生成自定义样式二维码

    使用vue-qr生成自定义二维码 vue-qr具有自定义二维码背景.logo.实点颜色等特性,能够生成更炫酷的二维码. 文章目录 使用vue-qr生成自定义二维码 效果图 安装 使用 部分参数说明 v ...

  4. java利用zxing来生成和解析二维码,支持中文

    java在二维码的生成和解析上,网上有些人说如果要解析中文,得去修改工具包的Encoder类中的 static final String DEFAULT_BYTE_MODE_ENCODING = &q ...

  5. java关于Zxing 生成带Logo 二维码图片失真问题

    java关于Zxing 生成带Logo 二维码图片失真问题 问题点 logo本身是高清图片,但是Zxing生成的二维码中,logo像素失真,感觉被严重压缩一样. 排查问题 是Graphics2D 绘制 ...

  6. Java实现生成和解析二维码

    Java实现生成和解析二维码 文章目录 Java实现生成和解析二维码 一.建立项目 二.创建工具类 三.创建启动类 一.建立项目 首先需要创建一个普通的 Maven 项目,在这里我用的是 google ...

  7. JAVA使用barcode4j生成条形码和二维码图片以及带logo的二维码,验证码图片

    二维码 1.Maven引入barcode4j依赖 <!-- 条形码生成 --><dependency><groupId>net.sf.barcode4j</g ...

  8. SpringBoot 整合zxing生成或解析二维码

    生成无Logo二维码 . 有Logo二维码 和 解析二维码内容 一.导包 二.Demo 三.结果 一.导包 zxing地址:https://mvnrepository.com/artifact/com ...

  9. Java生成和解析二维码工具类(简单经典)

    Java生成和解析二维码工具类 开箱即用,简单不废话. pom.xml引入依赖 <!-- https://mvnrepository.com/artifact/com.google.zxing/ ...

最新文章

  1. 【2012年终总结】之一 opencv + ds采集摄像头视频 MFC点点滴滴
  2. mysql limit不要1_切记!MySQL中ORDER BY与LIMIT 不要一起用,有大坑
  3. java linkedhashmap_java学习-hashMap和linkedHashMap
  4. Spark _12_每个网址的每个地区访问量 ,由大到小排序
  5. hybris backoffice创建product遇到的synchronization问题和解答
  6. 百度首席科学家吴恩达宣布将从百度离职
  7. linux系统操作大全,Linux系统的常用操作命令大全
  8. 锁大全与 GDB调试
  9. Easyui validatebox修改——1.当text发生变化时在校验,2.取消校验,3扩展自定义验证
  10. MacOS:Shell工具-Royal TSX
  11. 杭电oj部分新手入门题目全解(1089-1096)
  12. 分享美容护肤门店预约下单小程序开发制作功能介绍
  13. XML文件约束之DTD详解
  14. Linux——vi编辑器及文件内容操作
  15. java开发app_使用java制作app教程
  16. Ubuntu18.04搭建源码搜索引擎Opengrok
  17. GEO数据库的使用(一)
  18. 在Java中将二进制数转化成十进制数
  19. python一个数的阶乘_python整数阶乘计算
  20. PHP5 session 详解【经典】

热门文章

  1. 学习乐器的好处(1)
  2. 详解AUTOSAR:什么是AUTOSAR?(理论篇—1)
  3. 计算机考研评分标准,考研复试评分标准来啦!
  4. 六种方法提升营销和文案水平的有效方法
  5. SketchUp + Photoshop:别墅平面图制作教程
  6. idea修改回默认字体_设置 IntelliJ IDEA 主题和字体的方法
  7. SQL查找时间记录最近一条
  8. PAT_乙级_1007_筱筱
  9. [DEFCON全球黑客大会] CTF(Capture The Flag)
  10. candidate expects 1 argument, 0 provided 错误解决