做一个小项目的时候写了个二维码编码和解码的小工具,感觉可能用得到,有兴趣的朋友可以看下

再次之前,徐需要用到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 二维码编码解码的更多相关文章

  1. JAVA二维码编码&解码

    QRCodeUtil.java package web; import java.awt.AlphaComposite; import java.awt.Color; import java.awt. ...

  2. Java二维码的解码和编码

    原文:http://www.open-open.com/code/view/1430906793866 import java.io.File; import java.util.Hashtable; ...

  3. Java利用QRCode.jar包实现二维码编码与解码

    QRcode是日本人94年开发出来的.首先去QRCode的官网http://swetake.com/qrcode/java/qr_java.html,把要用的jar包下下来,导入到项目里去.qrcod ...

  4. Java二维码生成与解码

      基于google zxing 的Java二维码生成与解码   一.添加Maven依赖(解码时需要上传二维码图片,所以需要依赖文件上传包) <!-- google二维码工具 --> &l ...

  5. 二维码编码与解码类库ThoughtWorks.QRCode

    官方地址:https://www.codeproject.com/Articles/20574/Open-Source-QRCode-Library 有源代码和示例程序 支持二维码编码(生成)和解码( ...

  6. Atitit java 二维码识别 图片识别

    Atitit java 二维码识别 图片识别 1.1. 解码11.2. 首先,我们先说一下二维码一共有40个尺寸.官方叫版本Version.11.3. 二维码的样例:21.4. 定位图案21.5. 数 ...

  7. Java 二维码--转载

    周末试用下Android手机的二维码扫描软件,扫描了下火车票.名片等等,觉得非常不错很有意思的.当然Java也可以实现这些,现在就分享下如何简单用Java实现二维码中QRCode的编码和解码(可以手机 ...

  8. java二维码生成-谷歌(Google.zxing)开源二维码生成学习及实例

    java二维码生成-谷歌(Google.zxing)开源二维码生成的实例及介绍   我们使用比特矩阵(位矩阵)的QR码编码在缓冲图片上画出二维码 实例有以下一个传入参数 OutputStream ou ...

  9. Halcon一维码和二维码的解码步骤和技巧——第11讲

    针对Halcon中一维码和二维码的解码,我分别写了两篇文章,参见: <Halcon的一维条码解码步骤和解码技巧>:https://www.cnblogs.com/xh6300/p/1048 ...

随机推荐

  1. 说说WordPress的主查询函数-query_posts()

    今天说说WordPress 的主查询函数 -query_posts(),因为我正在制作的主题里面多次用到了这个函数 . query_posts()查询函数决定了哪些文章出现在WordPress 主 循 ...

  2. 无法使用命令 /usr/bin/xdpyinfo 自动检查显示器颜色。请检查是否设置了 DISPLAY

    https://blog.csdn.net/woshigedahaoren/article/details/9493887

  3. ionic3包还原使用yarn命令执行步骤(解决ionic3懒加载报找不到 module的错误)

    使用cnpm 还原ionic3.6的依赖包的时候 可以正常还原,但是使用懒加载就会报找不到 module 的错误.最简单的解决办法是删除node_modules 挂个vpn 重新执行npm insta ...

  4. lr中exit(-1)和return 0的区别

    LR脚本实践:关于lr中exit(-1)和return 0的区别 exit(-1):从当前action里面exit(-1)所在行,当前迭代里面直接退出来,终止运行: return 0:忽略当前acti ...

  5. 关于在windows下部署发布QT程序的总结

    原文请看:http://www.cnblogs.com/javaexam2/archive/2011/05/18/2632916.html 关于在windows下部署发布QT程序的总结 2008-06 ...

  6. HDU 6464.免费送气球-动态开点-权值线段树(序列中第first小至第second小的数值之和)(感觉就是只有一个状态的主席树) (“字节跳动-文远知行杯”广东工业大学第十四届程序设计竞赛)

    免费送气球 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submi ...

  7. Comet OJ CCPC-Wannafly Winter Camp Day1 (Div2, online mirror) F.爬爬爬山-最短路(Dijkstra)(两个板子)+思维(mdzz...) zhixincode

    爬爬爬山 已经提交 已经通过 9.83% Total Submission:417 Total Accepted:41 题目描述 爬山是wlswls最喜欢的活动之一. 在一个神奇的世界里,一共有nn座 ...

  8. 爬楼梯(LintCode)

    爬楼梯 假设你正在爬楼梯,需要n步你才能到达顶部.但每次你只能爬一步或者两步,你能有多少种不同的方法爬到楼顶部? 样例 比如n=3,中不同的方法 返回 3 用递归又超时了..于是又换了DP,dp并不熟 ...

  9. NOIP2018 提高组题解

    Day1 T1 据说是原题积木大赛,但是考场上蠢了,只会写数据结构,于是写了一个线段树\(+\)堆\(+\)贪心,先选出最小的,然后区间修改,然后把左右两端区间的最小值丢进堆里,不停从堆中去最小值更新 ...

  10. HDU 6119 2017百度之星初赛B 小小粉丝度度熊 (二分)

    小小粉丝度度熊 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...