package ljk.QRcode.ZXing.addImage;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
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.ReaderException;
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;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
/**
* @ClassName: Test
* @Description: zxing生成彩色带logo小图片的二维码示例
* 本代码依赖zxing-2.x.jar,请自己google下载jar包并导入到classpath中
* @author allen
* @date 2013-5-26 上午10:41:32
*/
@SuppressWarnings({"rawtypes","unchecked","finally"})
public class Test {
public static void main(String[] args) throws IOException {
Test test = new Test();
String content = "www.wodsy.com";
String inPath = "C:/Users/Administrator/Desktop/";
String outPath = "E:/MyeclipsProgect/QRCode/QRCode/img/";
// test.createQrCode(content, 300, 300, "C:/imgQRCode.jpg","C:/logo.png","png",0xFFE30022,0xFFF4C2C2);
test.createQrCode(content, 300, 300, inPath+"QRcode1.png",inPath+"logo.png","png",0x000000,0xFFFFFF);
// test.createQrCode(content, 300, 300, inPath+"QRcode2.png",inPath+"logost.png","png",0x000000,0xFFFFFF);
}
/**
* @throws IOException
* @Title: createQrCode
* @Description: 生成二维码图片
* @param @param content 二维码文字内容
* @param @param width 二维码图片宽度
* @param @param height 二维码图片高度
* @param @param imagePath 二维码图片保存路径
* @param @param formate 二维码图片格式,jpg/png等等
* @return void
* @throws
*/
public void createQrCode(String content,int width,int height,String imageSavePath,String formate) throws IOException {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 指定纠错级别
/*共有四个级别L M Q H,
L 7%
M 15%
Q 25%
H 30%
*/
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
BitMatrix matrix = null;
try {
//BarcodeFormat.QR_CODE:设置条形码类型
/*支持如下几种条形码
public static final BarcodeFormat AZTEC;
public static final BarcodeFormat CODABAR;
public static final BarcodeFormat CODE_39;
public static final BarcodeFormat CODE_93;
public static final BarcodeFormat CODE_128;
public static final BarcodeFormat DATA_MATRIX;
public static final BarcodeFormat EAN_8;
public static final BarcodeFormat EAN_13;
public static final BarcodeFormat ITF;
public static final BarcodeFormat MAXICODE;
public static final BarcodeFormat PDF_417;
public static final BarcodeFormat QR_CODE;
public static final BarcodeFormat RSS_14;
public static final BarcodeFormat RSS_EXPANDED;
public static final BarcodeFormat UPC_A;
public static final BarcodeFormat UPC_E;
public static final BarcodeFormat UPC_EAN_EXTENSION;
*/
matrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE, width, height, hints);
writeToFile(matrix, formate, imageSavePath);
} catch (WriterException e) {
e.printStackTrace();
}
}
/**
* @Title: createQrCode
* @Description: 生成二维码图片[带logo小图片]
* @param @param content 二维码文字内容[为了信息安全性,一般都要先进行数据加密]
* @param @param width 二维码图片宽度
* @param @param height 二维码图片高度
* @param @param imageSavePath 二维码保存路径
* @param @param logoPath logo小图片路径
* @param @param formate 二维码图片格式,jpg/png等等
* @param @param fontColor 字体颜色
* @param @param bgColor 二维码图片背景色
* @param @throws IOException 可能会抛出磁盘IO读写异常
* @return void
* @throws
*/
public void createQrCode(String content,int width,int height,String imageSavePath,String logoPath,String formate,int fontColor,int bgColor) throws IOException {
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
//设置字符编码
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
// 指定纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
BitMatrix matrix = null;
try {
matrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE, width, height, hints);
writeToFile(matrix, formate, imageSavePath, logoPath, fontColor, bgColor);
} catch (WriterException e) {
e.printStackTrace();
}
}
/**
* @Title: writeToFile
* @Description: 图片写入到磁盘文件
* @param @param matrix
* @param @param format
* @param @param imagePath
* @param @throws IOException
* @return void
* @throws
*/
public static void writeToFile(BitMatrix matrix, String format, String imagePath) throws IOException {
File file = new File(imagePath);
MatrixToImageWriter.writeToFile(matrix, format, file);
}
/**
* @Title: writeToFile
* @Description: 图片写入到磁盘文件[中间添加logo图片]
* @param @param matrix
* @param @param format
* @param @param imagePath
* @param @throws IOException
* @return void
* @throws
*/
public static void writeToFile(BitMatrix matrix, String formate, String imageSavePath,String logoPath,int fontColor,int bgColor) throws IOException {
BufferedImage image = MatrixToImageWriterEx.write2File(matrix, formate, imageSavePath,new MatrixToImageConfigEx(fontColor,bgColor));
//添加logo图片
MatrixToImageWriterEx.overlapImage(image, imageSavePath, logoPath, formate);
}
/**
* @Title: decode
* @Description: 二维码解析
* @param @param imgPath 二维码图片路径
* @return void
* @throws
*/
public String decode(String imgPath) {
String content = "";
try {
File file = new File(imgPath);
if(!file.exists()) {
return content;
}
BufferedImage image = null;
try {
image = ImageIO.read(file);
if (null == image) {
return content;
}
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Hashtable hints = new Hashtable();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
Result result = new MultiFormatReader().decode(bitmap, hints);
content = result.getText();
} catch (IOException e1) {
content = "";
} catch (ReaderException e2) {
content = "";
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
return content;
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
DemoTest.rar (30个子文件)
DemoTest
bin
ljk
QRcode
ZXing
ok
MatrixToImageWriterExtend.class 3KB
QRCodeZXingDemo.class 4KB
sourceCode
BufferedImageLuminanceSource.class 3KB
MatrixToImageWriter.class 2KB
addImage
Test.class 6KB
MatrixToImageWriterEx.class 5KB
MatrixToImageConfigEx.class 904B
EncodeDecode
QRCodeZXingDemo.class 4KB
swetake
QRCodeSwetakeDemo.class 5KB
TwoDimensionCodeImage.class 838B
Base64Test.class 759B
.settings
org.eclipse.core.resources.prefs 151B
org.eclipse.jdt.core.prefs 629B
src
ljk
Base64Test.java 398B
QRcode
ZXing
ok
MatrixToImageWriterExtend.java 3KB
QRCodeZXingDemo.java 6KB
sourceCode
MatrixToImageWriter.java 2KB
BufferedImageLuminanceSource.java 3KB
addImage
MatrixToImageConfigEx.java 1KB
MatrixToImageWriterEx.java 4KB
Test.java 7KB
EncodeDecode
QRCodeZXingDemo.java 3KB
swetake
TwoDimensionCodeImage.java 534B
QRCodeSwetakeDemo.java 5KB
.project 384B
.classpath 467B
lib
swetakeQRcode.jar 1.03MB
javabase64-1.3.1.jar 4KB
javase.jar 33KB
ZXingQRcode_core.jar 239KB
共 30 条
- 1
资源评论
- jy4564872015-06-08JAR包不全,是class文件
- Cryonmyshoulder12015-08-03值得学习,可以从中借鉴好多东西,好资源大家分享
- 云之智能2014-07-08资源很好,学习一下。
oO感悟人生Oo
- 粉丝: 10
- 资源: 15
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- libjpeg 编译所需的 Win32.mak vs编译libjpeg
- 自动驾驶-状态估计和定位-粒子滤波实现和源码.pdf
- 数据可视化-智慧物流服务中心大屏页面.zip
- yolov5,SSD 可能使用到的一些代码
- bbbbbbbbbbbbbbbbbb
- 安卓逆向学习笔记之Frida Stalker 还原OLLVM AES.docx
- 安卓逆向学习笔记之unicorn来trace还原OLLVM Base64.docx
- 基于jquery的自定义表格组件实现
- Nessus最新20240426离线安装插件all-2.0.tar.gz
- 最新版本私钥助记词碰撞器大富豪使用python进行制作通过接口的方式进行验证支持多币种多链多网络一分钟万次验证高出货率
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功