使用Zxing来对二维码生成和解析
package com.whl.qrcode;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Hashtable;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.ReaderException;
import com.google.zxing.Result;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class CreateQrCodeTest {
public static void createQrCode(OutputStream outputStream, String content, int qrCodeSize, String imageFormat){
try{
// Create the ByteMatrix for the QR-Code that encodes the given String.
Hashtable<EncodeHintType, ErrorCorrectionLevel> hintMap = new Hashtable<EncodeHintType, ErrorCorrectionLevel>();
hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
QRCodeWriter qrCodeWriter = new QRCodeWriter();
BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
// Make the BufferedImage that are to hold the QRCode
int matrixWidth = byteMatrix.getWidth();
BufferedImage image = new BufferedImage(matrixWidth, matrixWidth, BufferedImage.TYPE_INT_RGB);
image.createGraphics();
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, matrixWidth, matrixWidth);
// Paint and save the image using the ByteMatrix
graphics.setColor(Color.BLACK);
for (int i = 0; i < matrixWidth; i++){
for (int j = 0; j < matrixWidth; j++){
if (byteMatrix.get(i, j)){
graphics.fillRect(i, j, 1, 1);
}
}
}
ImageIO.write(image, imageFormat, outputStream);
}catch (Exception ex) {
ex.printStackTrace();
}
}
public static void readQrCode(InputStream inputStream) throws IOException{
//get the data from the input stream
BufferedImage image = ImageIO.read(inputStream);
//convert the image to a binary bitmap source
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
//decode the barcode
QRCodeReader reader = new QRCodeReader();
Result result = null ;
try {
result = reader.decode(bitmap);
} catch (ReaderException e) {
//the data is improperly formatted
e.printStackTrace();
}
System.out.println(result.getText());
}
public static void main(String[] args) throws IOException {
// createQrCode(new FileOutputStream(new File("d:\\qrcode.jpg")),"name:whl",300,"JPEG");
readQrCode(new FileInputStream(new File("d:\\qrcode.jpg")));
}
}
效果图: 附件为zxing jar文件
并且很方便的设置前景 背景颜色 及logo上传
相关推荐
二维码生成及解析java源码
二维码生成及解析内涵两个java类原码与QRCode所需的jar包。
生成二维码,并对生成的二维码进行解析。把要用的jar包下下来,导入到项目里去。qrcode需要设置一个版本号,这个版本号代表你生成的二维码的像素的大小。版本1是21*21的,版本号每增加1,边长增加4。也就是说版本7的...
这是一个基于java开发的二维码生成与解析 可以根据程序配置的参数设置生成二维码的图片大小 以及密度 这是一个基于java开发的二维码生成与解析 可以根据程序配置的参数设置生成二维码的图片大小 以及密度 这是一个...
java 生成二维码的三种方式 zwing ,Qrcode ,jquery.qrcode.js
java二维码的生成,和二维码的解析,主要实现二维码生成和解析
利用google的zwing 进行生成QRcode类型的二维码,以及如何解析二维码。
二维码生成、解析源码,网上下载的资源,修改了错误,纠错能力很强
基于Spring Boot的二维码生成与解析接口.zip
Google二维码生成解析zxing库 Android 代码,可以实现二维码生成,亲测可用。 代码为2016年6月9日从GitHub下载的,版本为3.2.1 GitHub对应地址代码为: https://github.com/zxing/zxing jar包原始下载地址为:...
QRCodeUtils使用文档详见博客https://blog.csdn.net/qq_24091555/article/details/81263472
二维码生成与解析代码实现,同时提供封装好的方法,提供一个操作二维码核心类供大家直接使用。
二维码生成、解析(C#源码)
利用Google ZXing 实现Java 二维码生成、解析。
小程序项目 二维码生成器 《微信小程序》 软件架构 软件架构说明 原生微信小程序 基础功能 二维码生成器,文字、url等生成二维码 二维码反向解析器。 二维码反向解析出来字符串 微信名片,本地存储的一个微信...
JAVA实现二维码生成和解析。QRCode