Java利用Zxing生成二维码
Zxing是Google提供的关于条码(一维码二维码)的解析工具提供了二维码的生成与解析的方法现在我简单介绍一下使用Java利用Zxing生成与解析二维码
二维码的生成
将Zxingcorejar 包加入到classpath下
二维码的生成需要借助MatrixToImageWriter类该类是由Google提供的可以将该类拷贝到源码中这里我将该类的源码贴上可以直接使用
import monBitMatrix;
import javaximageioImageIO;
import javaioFile;
import javaioOutputStream;
import javaioIOException;
import javaawtimageBufferedImage;
public final class MatrixToImageWriter {
private static final int BLACK = xFF;
private static final int WHITE = xFFFFFFFF;
private MatrixToImageWriter() {}
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrixgetWidth();
int height = matrixgetHeight();
BufferedImage image = new BufferedImage(width height BufferedImageTYPE_INT_RGB);
for (int x = ; x < width; x++) {
for (int y = ; y < height; y++) {
imagesetRGB(x y matrixget(x y) ? BLACK : WHITE);
}
}
return image;
}
public static void writeToFile(BitMatrix matrix String format File file)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIOwrite(image format file)) {
throw new IOException(Could not write an image of format + format + to + file);
}
}
public static void writeToStream(BitMatrix matrix String format OutputStream stream)
throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIOwrite(image format stream)) {
throw new IOException(Could not write an image of format + format);
}
}
}
编写生成二维码的实现代码
try {
String content = /jtmjx;
String path = C:/Users/Administrator/Desktop/testImage;
MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
Map hints = new HashMap();
hintsput(EncodeHintTypeCHARACTER_SET UTF);
BitMatrix bitMatrix = multiFormatWriterencode(content BarcodeFormatQR_CODE hints);
File file = new File(path餐巾纸jpg);
MatrixToImageWriterwriteToFile(bitMatrix jpg file);
} catch (Exception e) {
eprintStackTrace();
}
现在运行后即可生成一张二维码图片是不是很简单啊? 接下来我们看看如何解析二维码
二维码的解析
将Zxingcorejar 包加入到classpath下
和生成一样我们需要一个辅助类( BufferedImageLuminanceSource)同样该类Google也提供了这里我同样将该类的源码贴出来可以直接拷贝使用个省去查找的麻烦
BufferedImageLuminanceSource
import comgooglezxingLuminanceSource;
import javaawtGraphicsD;
import javaawtgeomAffineTransform;
import javaawtimageBufferedImage;
public final class BufferedImageLuminanceSource extends LuminanceSource {
private final BufferedImage image;
private final int left;
private final int top;
public BufferedImageLuminanceSource(BufferedImage image) {
this(image imagegetWidth() imagegetHeight());
}
public BufferedImageLuminanceSource(BufferedImage image int left int top int width int height) {
super(width height);
int sourceWidth = imagegetWidth();
int sourceHeight = imagegetHeight();
if (left + width > sourceWidth || top + height > sourceHeight) {
throw new IllegalArgumentException(Crop rectangle does not fit within image data);
}
for (int y = top; y < top + height; y++) {
for (int x = left; x < left + width; x++) {
if ((imagegetRGB(x y) & xFF) == ) {
imagesetRGB(x y xFFFFFFFF); // = white
}
}
}
thisimage = new BufferedImage(sourceWidth sourceHeight BufferedImageTYPE_BYTE_GRAY);
thisimagegetGraphics()drawImage(image null);
thisleft = left;
thistop = top;
}
@Override
public byte[] getRow(int y byte[] row) {
if (y < || y >= getHeight()) {
throw new IllegalArgumentException(Requested row is outside the image: + y);
}
int width = getWidth();
if (row == null || rowlength < width) {
row = new byte[width];
}
imagegetRaster()getDataElements(left top + y width row);
return row;
}
@Override
public byte[] getMatrix() {
int width = getWidth();
int height = getHeight();
int area = width * height;
byte[] matrix = new byte[area];
imagegetRaster()getDataElements(left top width height matrix);
return matrix;
}
@Override
public boolean isCropSupported() {
return true;
}
@Override
public LuminanceSource crop(int left int top int width int height) {
return new BufferedImageLuminanceSource(image thisleft + left thistop + top width height);
}
@Override
public boolean isRotateSupported() {
return true;
}
@Override
public LuminanceSource rotateCounterClockwise() {
int sourceWidth = imagegetWidth();
int sourceHeight = imagegetHeight();
AffineTransform transform = new AffineTransform( sourceWidth);
BufferedImage rotatedImage = new BufferedImage(sourceHeight sourceWidth BufferedImageTYPE_BYTE_GRAY);
GraphicsD g = rotatedImagecreateGraphics();
gdrawImage(image transform null);
gdispose();
int width = getWidth();
return new BufferedImageLuminanceSource(rotatedImage top sourceWidth (left + width) getHeight() width);
}
}
编写解析二维码的实现代码
try {
MultiFormatReader formatReader = new MultiFormatReader();
String filePath = C:/Users/Administrator/Desktop/testImage/testjpg;
File file = new File(filePath);
BufferedImage image = ImageIOread(file);;
LuminanceSource source = new BufferedImageLuminanceSource(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
Map hints = new HashMap();
hintsput(EncodeHintTypeCHARACTER_SET UTF);
Result result = formatReaderdecode(binaryBitmaphints);
Systemoutprintln(result = + resulttoString());
Systemoutprintln(resultFormat = + resultgetBarcodeFormat());
Systemoutprintln(resultText = + resultgetText());
} catch (Exception e) {
eprintStackTrace();
}
现在运行后可以看到控制台打印出了二维码的内容