java 二维码编码解码
做一个小项目的时候写了个二维码编码和解码的小工具,感觉可能用得到,有兴趣的朋友可以看下
再次之前,徐需要用到google的zxing相关的jar包,还有javax相关包
以上为可能用到的jar
package org.ink.image.qrimgrz; import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map; import javax.imageio.ImageIO; import com.google.gson.Gson;
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.NotFoundException;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
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; /**
* QRImage recognize Utils imcluding code and decode
* @author ink.Flower
*
*/
public class QRImageUtils {
/**
* create QRImage,default CHARACTER_SET is UTF-8 生成二维码图片
* @param content context of image 内容
* @param filepath target FilePath of new QRImage 目标文件路径
* @param picFormat picture format,like png,jpg .etc 图片类型
* @param height height of picture 图片高度
* @param width width of picture 图片宽度
* @throws WriterException
* @throws IOException
*/
public static void QRCode(String content,String filepath,String picFormat,int height,int width) throws WriterException, IOException{
Map<EncodeHintType,Object> hints=new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
//生成矩阵
BitMatrix bitMatrix=new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,width,height,hints);
Path path = FileSystems.getDefault().getPath(filepath);
MatrixToImageWriter.writeToPath(bitMatrix, picFormat,path);
} /**
* create QRImage,default CHARACTER_SET is UTF-8 将实体类对象通过json的方式生成二维码
* @param obj The Object which is to create QRCode Image 目标实体类对象
* @param filepath target FilePath of new QRImage
* @param picFormat picture format,like png,jpg .etc
* @param height height of picture
* @param width width of picture
* @throws WriterException
* @throws IOException
*/
public static void QRCode(Object obj,String filepath,String picFormat,int height,int width) throws WriterException, IOException{
String json = new Gson().toJson(obj);
QRCode(json, filepath, picFormat, height, width);
} /**
* QRDecode to Content 二维码解码成字符串
* @param filepath 图片文件路径
* @return content 字符串内容
* @throws FileNotFoundException
* @throws IOException
* @throws NotFoundException
*/
public static String QRDecodeToString(String filepath) throws FileNotFoundException, IOException, NotFoundException{
return QRDecodeToString(new FileInputStream(filepath));
} /**
* QRDecode to Content 二维码解码
* @param is InputStream of Image File 图片输入流
* @return content 内容
* @throws FileNotFoundException
* @throws IOException
* @throws NotFoundException
*/
public static String QRDecodeToString(InputStream is) throws FileNotFoundException, IOException, NotFoundException{
BufferedImage image=ImageIO.read(is);
LuminanceSource source=new BufferedImageLuminanceSource(image);
Binarizer binarizer=new HybridBinarizer(source);
BinaryBitmap bitmap=new BinaryBitmap(binarizer);
Map<DecodeHintType,Object> map=new HashMap<DecodeHintType, Object>();
map.put(DecodeHintType.CHARACTER_SET, "UTF-8");
Result result=null;
try{
result = new MultiFormatReader().decode(bitmap,map);
}catch (Exception e) {
e.printStackTrace();
}
if(result!=null)
return result.getText();
else
return null;
} /**
* decode QRCode json格式内容二维码解码成实体类
* @param <T>
* @param filePath 图片文件路径
* @param clazz Class of target Pojo 实体类的Class对象
* @return Pojo 实体类对象
* @throws FileNotFoundException
* @throws NotFoundException
* @throws IOException
*/
public static <T> Object QRDecodeToObject(String filePath,Class<T> clazz) throws FileNotFoundException, NotFoundException, IOException{
return QRDecodeToObject(new FileInputStream(filePath), clazz);
} /**
* decode QRCode
* @param <T>
* @param is 图片输入流
* @param clazz Class of target Pojo
* @return Pojo
* @throws FileNotFoundException
* @throws NotFoundException
* @throws IOException
*/
public static <T> Object QRDecodeToObject(InputStream is,Class<T> clazz) throws FileNotFoundException, NotFoundException, IOException{
InputStreamReader reader=new InputStreamReader(is);
return new Gson().fromJson(reader, clazz);
}
}
以上
@ink
java 二维码编码解码的更多相关文章
- JAVA二维码编码&解码
QRCodeUtil.java package web; import java.awt.AlphaComposite; import java.awt.Color; import java.awt. ...
- Java二维码的解码和编码
原文:http://www.open-open.com/code/view/1430906793866 import java.io.File; import java.util.Hashtable; ...
- Java利用QRCode.jar包实现二维码编码与解码
QRcode是日本人94年开发出来的.首先去QRCode的官网http://swetake.com/qrcode/java/qr_java.html,把要用的jar包下下来,导入到项目里去.qrcod ...
- Java二维码生成与解码
基于google zxing 的Java二维码生成与解码 一.添加Maven依赖(解码时需要上传二维码图片,所以需要依赖文件上传包) <!-- google二维码工具 --> &l ...
- 二维码编码与解码类库ThoughtWorks.QRCode
官方地址:https://www.codeproject.com/Articles/20574/Open-Source-QRCode-Library 有源代码和示例程序 支持二维码编码(生成)和解码( ...
- Atitit java 二维码识别 图片识别
Atitit java 二维码识别 图片识别 1.1. 解码11.2. 首先,我们先说一下二维码一共有40个尺寸.官方叫版本Version.11.3. 二维码的样例:21.4. 定位图案21.5. 数 ...
- Java 二维码--转载
周末试用下Android手机的二维码扫描软件,扫描了下火车票.名片等等,觉得非常不错很有意思的.当然Java也可以实现这些,现在就分享下如何简单用Java实现二维码中QRCode的编码和解码(可以手机 ...
- java二维码生成-谷歌(Google.zxing)开源二维码生成学习及实例
java二维码生成-谷歌(Google.zxing)开源二维码生成的实例及介绍 我们使用比特矩阵(位矩阵)的QR码编码在缓冲图片上画出二维码 实例有以下一个传入参数 OutputStream ou ...
- Halcon一维码和二维码的解码步骤和技巧——第11讲
针对Halcon中一维码和二维码的解码,我分别写了两篇文章,参见: <Halcon的一维条码解码步骤和解码技巧>:https://www.cnblogs.com/xh6300/p/1048 ...
随机推荐
- 很好用的谷歌字体以及Gravatar头像一键替换WordPress插件----WP Acceleration for China 插件
WordPress总是被新上手的朋友诟病说速度慢,其实多半都要归功于谷歌字体的功劳.在应对字体这个问题的时候,大家都会有各种不同的解决方案.今天我给大家推荐一款插件,它集合了多个替代方案,可以方便的替 ...
- OpenStack 存储服务 Cinder存储节点部署LVM (十四)
部署在block(10.0.0.103)主机 一)配置lvm 1.安装lvm2软件包 yum install lvm2 -y 2.启动LVM的metadata服务并且设置该服务随系统启动 system ...
- 最短路-Floyd
简介: 算法的特点: 弗洛伊德算法是解决任意两点间的最短路径的一种算法,可以正确处理有向图或有向图或负权(但不可存在负权回路)的最短路径问题,同时也被用于计算有向图的传递闭 包. 算法思想: 通过F ...
- The 18th Zhejiang University Programming Contest Sponsored by TuSimple -C Mergeable Stack
题目链接 题意: 题意简单,就是一个简单的数据结构,对栈的模拟操作,可用链表实现,也可以用C++的模板类来实现,但是要注意不能用cin cout,卡时间!!! 代码: #include <std ...
- Python urllib2 设置超时时间并处理超时异常
可以使用 except: 捕获任何异常,包括 SystemExit 和 KeyboardInterupt,不过这样不便于程序的调试和使用 最简单的情况是捕获 urllib2.URLError try: ...
- go chapter 8 - 初始化对象
http://blog.haohtml.com/archives/14239 struct定义的属性如果是小写开头的,那么该属性不是public的,不能跨包调用 (implicit assignmen ...
- Poj1151&HDU1542 Atlantis(扫描线+线段树)
题意 给定\(n\)个矩形\((x_1,y_1,x_2,y_2)\),求这\(n\)个矩形的面积并 题解 扫描线裸题,可以不用线段树维护,\(O(n^2)\)是允许的. #include < ...
- Codeforces Beta Round #14 (Div. 2) Two Paths (树形DP)
Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input outp ...
- C++中cin、cin.get()、cin.getline()、getline()、gets()等函数的用法(转)
学C++的时候,这几个输入函数弄的有点迷糊:这里做个小结,为了自己复习,也希望对后来者能有所帮助,如果有差错的地方还请各位多多指教(本文所有程序均通过VC 6.0运行) 1.cin 2.cin.get ...
- [BZOJ4819][SDOI2017]新生舞会(分数规划+费用流,KM)
4819: [Sdoi2017]新生舞会 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1097 Solved: 566[Submit][Statu ...