zxing搞的简单的二维码生成 博客分类: 二维码 zxing二维码
package com.chekn.test.qrcode;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
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.NotFoundException;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
public class QrCodeImageCopy {
private static final String outFilePath="D:\\Users\\cks\\Desktop\\TRANSFILE\\QR_CODE\\";
private static final int width=300;
private static final int height=300;
private static final int blackRGB=0xFF000000;
private static final int whiteRGB=0xFFFFFFFF;
private boolean writeQrCodeImage(String code, String fileName) throws WriterException, IOException{
//编码配置
Map<EncodeHintType,String> map=new HashMap<EncodeHintType,String>();
map.put(EncodeHintType.CHARACTER_SET, "utf-8");
//文本数据转换成图像矩阵数据
BitMatrix bitMatrix=new MultiFormatWriter().encode(code, BarcodeFormat.QR_CODE, width, height,map);
//输出
System.out.println("width->"+bitMatrix.getWidth()+" height->"+bitMatrix.getHeight());
File file=new File(outFilePath+fileName+".png");
BufferedImage bufImage=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
for(int x=0;x<width;x++){
for(int y=0;y<height;y++){
bufImage.setRGB(x, y, bitMatrix.get(x, y)?blackRGB:whiteRGB);
}
}
return ImageIO.write(bufImage, "png", file);
}
private String readQrCodeImage(File file) throws IOException, NotFoundException{
//读图
BufferedImage bufImage=ImageIO.read(file);
//转成位图
LuminanceSource source=new BufferedImageLuminanceSource(bufImage);
BinaryBitmap bitmap=new BinaryBitmap(new HybridBinarizer(source));
//解码配置
Map<DecodeHintType,String> map=new HashMap<DecodeHintType,String>();
map.put(DecodeHintType.CHARACTER_SET, "utf-8");
String parseCode= new MultiFormatReader().decode(bitmap,map).getText();
return parseCode;
}
public static void main(String[] args){
QrCodeImageCopy qci=new QrCodeImageCopy();
try {
System.out.println(Color.GREEN.getRGB()+" black: "+blackRGB+" white: "+whiteRGB);
for(int i=0;i<5;i++){
String uuidStr=UUID.randomUUID().toString();
qci.writeQrCodeImage(uuidStr, Integer.toString(i));
String parseStr=qci.readQrCodeImage(new File(outFilePath+i+".png"));
System.out.println(i+": uuidStr->"+uuidStr+", parseStr->"+parseStr);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
推荐阅读
-
zxing搞的简单的二维码生成 博客分类: 二维码 zxing二维码
-
Zxing版本问题 博客分类: 二维码 ZXing
-
基于zxing-lib三步实现Android二维码应用 博客分类: Android AndroidZXingQRCode
-
java利用zxing编码解码一维码与二维码 博客分类: JAVA、WEB开发 zxing二维码一维码qrcode编码解码
-
[原创] Android 长按识别图中二维码 - Zxing 博客分类: Android androidzxingqrcode
-
QrCode的实现二维码功能 博客分类: 二维码Java技术研究类 二维码QrCodejavabufferImage
-
Zxing方式生产二维码方法 博客分类: 技术研究类二维码Java Java二维码Qrcodezxing
-
ZXing 二维码 Demo 博客分类: 代码演示 zxingqrcodedemo二维码
-
二维码学习(一):概念篇 博客分类: 二维码 二维码概念二维码分类QRCode二维码符号结构二维码的优点和缺点
-
ZXing 生成或读取二维码(解决中文乱码的问题)