java实现二维码([带]logo)的绘制和解析(zxing by google)

2024-05-01 02:48

本文主要是介绍java实现二维码([带]logo)的绘制和解析(zxing by google),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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

--百度百科

注:遇到问题:

①:二维码logo图片加入为黑白色,已经解决(测试代码中)。

②:二维码背景色和显示颜色色差大,扫描快,但如果显示颜色设置为黑色外其他颜色,可能无法扫描。

使用到的jar包zxing.jar,源码twodimensioncode,颜色转换:java实现颜色Color对象和16进制之间的转换

1:绘制二维码:

package com.tsxs.tools.twodimensioncode;import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.EnumMap;import javax.imageio.ImageIO;import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
/*** 通过google的zxing实现二维码(加入logo图片)* @author tskk* @version 2015-6-26 13:30:20* */
public final class EncodeImgZxing { //二维码颜色private static final int BLACK = 0xFF000000;//0xFFFF0000,红色//二维码背景色private static final int WHITE = 0xFFFFFFFF;//0xFF0000FF,蓝色//注:二维码颜色色差大,扫描快,但如果"BLACK'设置为黑色外其他颜色,可能无法扫描//二维码图片宽度private static final int width = 300;//二维码图片高度private static final int height = 300;//二维码格式参数private static final EnumMap<EncodeHintType, Object> hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);static{/*二维码的纠错级别(排错率),4个级别:L (7%)、M (15%)、Q (25%)、H (30%)(最高H)纠错信息同样存储在二维码中,纠错级别越高,纠错信息占用的空间越多,那么能存储的有用讯息就越少;共有四级;选择M,扫描速度快。*/hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 二维码边界空白大小 1,2,3,4 (4为默认,最大)hints.put(EncodeHintType.MARGIN, 1);hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");hints.put(EncodeHintType.MAX_SIZE, 350);hints.put(EncodeHintType.MIN_SIZE, 150);}/*** 绘制二维码* @param contents 二维码内容  * @return image 二维码图片* */public static BufferedImage encodeImg(String contents){BufferedImage image = null;try{BitMatrix matrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);int width = matrix.getWidth();int height = matrix.getHeight();for(int x = 0; x < width; x++){for(int y =0;y < height; y++){image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);}}}catch(Exception e){System.out.println("生成二维码失败"+e.getMessage());}return image;}/*** 二维码输出到文件* 	@param contents 二维码内容* @param format 图片格式* @param file 输出文件* */public static void writeToFile(String contents,String format,File file){BufferedImage image = encodeImg(contents);try {ImageIO.write(image, format, file);} catch (IOException e) {System.out.println("二维码写入文件失败"+e.getMessage());}}/*** 二维码流式输出* 	@param contents 二维码内容* @param format 图片格式* @param stream 输出流* */public static void writeToStream(String contents,String format,OutputStream stream){BufferedImage image = encodeImg(contents);try {ImageIO.write(image, format, stream);} catch (IOException e) {System.out.println("二维码写入流失败"+e.getMessage());}}
}

2:测试代码:

package com.tsxs.test;import java.io.File;import org.junit.Test;import com.tsxs.tools.twodimensioncode.DecodeImgZxing;
import com.tsxs.tools.twodimensioncode.EncodeImgZingLogo;
import com.tsxs.tools.twodimensioncode.EncodeImgZxing;public class EncodeImgZingLogoTest {@Testpublic void testWriteToFile() {String contents = "http://blog.csdn.net/typa01_kk";String format = "jpeg"; //***此处如果格式为"gif",则logo图片为黑色,其他格式ok//生成二维码File logoImg = new File("D:"+File.separator+"logo.jpg");File img = new File("D:"+File.separator+"csdn.jpg");EncodeImgZxing.writeToFile(contents, format, img);
//		//添加logo图片File img1 = new File("D:"+File.separator+"csdnlogo.jpg");EncodeImgZingLogo.writeToFile(img, logoImg, format, img1);//解析二维码String content = DecodeImgZxing.decodeImg(img);System.out.println("1:"+content);//带logoString content1 = DecodeImgZxing.decodeImg(img1);System.out.println("2:"+content1);}}

效果图为:img1:(H 1) img2(M 4) img3(color change ,无法扫描,红色改为黑色,可扫描,速度慢)

3:二维码绘制logo(二维码和logo图片合并绘制,图片尽量为方形,有利扫描和显示)

package com.tsxs.tools.twodimensioncode;import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;import javax.imageio.ImageIO;
/*** 通过google的zxing实现二维码(加入logo图片)* @author tskk* @version 2015-6-28 13:30:20* */
public final class EncodeImgZingLogo {/*** 二维码绘制logo* @param twodimensioncodeImg 二维码图片文件* @param logoImg logo图片文件* */public static BufferedImage encodeImgLogo(File twodimensioncodeImg,File logoImg){BufferedImage twodimensioncode = null;try{if(!twodimensioncodeImg.isFile() || !logoImg.isFile()){System.out.println("输入非图片");return null;}//读取二维码图片twodimensioncode = ImageIO.read(twodimensioncodeImg);//获取画笔Graphics2D g = twodimensioncode.createGraphics();//读取logo图片BufferedImage logo = ImageIO.read(logoImg);//设置二维码大小,太大,会覆盖二维码,此处20%int logoWidth = logo.getWidth(null) > twodimensioncode.getWidth()*2 /10 ? (twodimensioncode.getWidth()*2 /10) : logo.getWidth(null);int logoHeight = logo.getHeight(null) > twodimensioncode.getHeight()*2 /10 ? (twodimensioncode.getHeight()*2 /10) : logo.getHeight(null);//设置logo图片放置位置//中心int x = (twodimensioncode.getWidth() - logoWidth) / 2;int y = (twodimensioncode.getHeight() - logoHeight) / 2;//右下角,15为调整值
//			int x = twodimensioncode.getWidth()  - logoWidth-15;
//			int y = twodimensioncode.getHeight() - logoHeight-15;//开始合并绘制图片g.drawImage(logo, x, y, logoWidth, logoHeight, null);g.drawRoundRect(x, y, logoWidth, logoHeight, 15 ,15);//logo边框大小g.setStroke(new BasicStroke(2));//logo边框颜色g.setColor(Color.WHITE);g.drawRect(x, y, logoWidth, logoHeight);g.dispose();logo.flush();twodimensioncode.flush();}catch(Exception e){System.out.println("二维码绘制logo失败");}return twodimensioncode;}/*** 二维码输出到文件* @param twodimensioncodeImg 二维码图片文件* @param logoImg logo图片文件* @param format 图片格式* @param file 输出文件* */public static void writeToFile(File twodimensioncodeImg,File logoImg,String format,File file){BufferedImage image = encodeImgLogo(twodimensioncodeImg, logoImg);try {ImageIO.write(image, format, file);} catch (IOException e) {System.out.println("二维码写入文件失败"+e.getMessage());}}/*** 二维码流式输出* @param twodimensioncodeImg 二维码图片文件* @param logoImg logo图片文件* @param format 图片格式* @param stream 输出流* */public static void writeToStream(File twodimensioncodeImg,File logoImg,String format,OutputStream stream){BufferedImage image = encodeImgLogo(twodimensioncodeImg, logoImg);try {ImageIO.write(image, format, stream);} catch (IOException e) {System.out.println("二维码写入流失败"+e.getMessage());}}
}

使用上测试代码产生二维码为:

4:二维码内容解析:

package com.tsxs.tools.twodimensioncode;import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.EnumMap;import javax.imageio.ImageIO;import com.google.zxing.Binarizer;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
/*** 通过google的zxing解析二维码* @author tskk* @version 2015-6-26 13:30:20* 注:此代码,不能解析:L纠错级别带logo和H级别的解析* */
public final class DecodeImgZxing {//二维码格式参数private static final EnumMap<DecodeHintType, Object> hints = new EnumMap<DecodeHintType, Object>(DecodeHintType.class);static{hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");}/*** 解析二维码,使用google的zxing* @param imgPath 二维码路径* @return content 二维码内容* */public static String decodeImg(File imgFile){String content = null;if(!imgFile.isFile()){System.out.println("输入非文件");return null;}try {BufferedImage image = ImageIO.read(imgFile);LuminanceSource source = new BufferedImageLuminanceSource(image);Binarizer binarizer = new HybridBinarizer(source);BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);MultiFormatReader reader = new MultiFormatReader();Result result = reader.decode(binaryBitmap, hints);content = result.getText();
//			System.out.println("二维码结果:"+":"+result.toString()+","+result.getBarcodeFormat()+","+result.getText());} catch (NotFoundException e) {System.out.println("二维码解析NotFoundException");e.printStackTrace();} catch (IOException e) {System.out.println("二维码解析IOException");e.printStackTrace();}return content;}
}

使用测试代码,测试结果:

1:http://blog.csdn.net/typa01_kk
2:http://blog.csdn.net/typa01_kk

这篇关于java实现二维码([带]logo)的绘制和解析(zxing by google)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/950549

相关文章

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

全面解析Golang 中的 Gorilla CORS 中间件正确用法

《全面解析Golang中的GorillaCORS中间件正确用法》Golang中使用gorilla/mux路由器配合rs/cors中间件库可以优雅地解决这个问题,然而,很多人刚开始使用时会遇到配... 目录如何让 golang 中的 Gorilla CORS 中间件正确工作一、基础依赖二、错误用法(很多人一开