欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解ZXing-core生成二维码的方法并解析

程序员文章站 2024-03-11 16:19:01
二维码无处不在,扫一扫有礼品哦,现在二维码这么流行,想必大家不是很清楚二维码是怎么生成的吧,现在小编通过给大家分享本文帮助大家学习二维码生成的方法。 其实主要是利用gog...

二维码无处不在,扫一扫有礼品哦,现在二维码这么流行,想必大家不是很清楚二维码是怎么生成的吧,现在小编通过给大家分享本文帮助大家学习二维码生成的方法。

其实主要是利用goggle发布的jar来使用的此功能。

1、二维码的生成

将zxing-core.jar 包加入到classpath下。

二维码的生成需要借助matrixtoimagewriter类,该类是由google提供的,可以将该类拷贝到源码中,这里我将该类的源码贴上,可以直接使用。

直接可以生成二维码的代码

public void test1() throws exception{ 
string content = "www.baidu.com"; 
string path = "d://"; 
multiformatwriter multiformatwriter = new multiformatwriter();//zxing是google提供的关于条码 
map hints = new hashmap(); 
hints.put(encodehinttype.character_set, "utf-8"); 
bitmatrix bitmatrix = multiformatwriter.encode(content, barcodeformat.qr_code, 400, 400,hints);//这里是照片的大小 
file file1 = new file(path,"my.jpg"); 
matrixtoimagewriter.writetofile(bitmatrix, "jpg", file1); 
system.out.println("执行完毕"); 
} 

当我们能发现,这个代码拷贝上后发现有一个matrixtoimagewriter报错,所以需要我们去找,但是这里我贴出代码,可以直接使用。

import com.google.zxing.common.bitmatrix; 
import javax.imageio.imageio; 
import java.io.file; 
import java.io.outputstream; 
import java.io.ioexception; 
import java.awt.image.bufferedimage; 
public final class matrixtoimagewriter { 
private static final int black = 0xff000000; 
private static final int white = 0xffffffff; 
private matrixtoimagewriter() {} 
public static bufferedimage tobufferedimage(bitmatrix matrix) { 
int width = matrix.getwidth(); 
int height = matrix.getheight(); 
bufferedimage image = new bufferedimage(width, height, bufferedimage.type_int_rgb); 
for (int x = 0; x < width; x++) { 
for (int y = 0; y < height; y++) { 
image.setrgb(x, y, matrix.get(x, y) ? black : white); 
} 
} 
return image; 
} 
public static void writetofile(bitmatrix matrix, string format, file file) 
throws ioexception { 
bufferedimage image = tobufferedimage(matrix); 
if (!imageio.write(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 (!imageio.write(image, format, stream)) { 
throw new ioexception("could not write an image of format " + format); 
} 
} 
} 

现在就可以d盘的根目录下载看生成的二维码了

二维码解析

和生成一样,我们需要一个辅助类( bufferedimageluminancesource),同样该类google也提供了,这里我同样将该类的源码贴出来,可以直接拷贝使用个,省去查找的麻烦

bufferedimageluminancesource 
import com.google.zxing.luminancesource; 
import java.awt.graphics2d; 
import java.awt.geom.affinetransform; 
import java.awt.image.bufferedimage; 
public final class bufferedimageluminancesource extends luminancesource { 
private final bufferedimage image; 
private final int left; 
private final int top; 
public bufferedimageluminancesource(bufferedimage image) { 
this(image, 0, 0, image.getwidth(), image.getheight()); 
} 
public bufferedimageluminancesource(bufferedimage image, int left, int top, int width, int height) { 
super(width, height); 
int sourcewidth = image.getwidth(); 
int sourceheight = image.getheight(); 
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 ((image.getrgb(x, y) & 0xff000000) == 0) { 
image.setrgb(x, y, 0xffffffff); // = white 
} 
} 
} 
this.image = new bufferedimage(sourcewidth, sourceheight, bufferedimage.type_byte_gray); 
this.image.getgraphics().drawimage(image, 0, 0, null); 
this.left = left; 
this.top = top; 
} 
@override 
public byte[] getrow(int y, byte[] row) { 
if (y < 0 || y >= getheight()) { 
throw new illegalargumentexception("requested row is outside the image: " + y); 
} 
int width = getwidth(); 
if (row == null || row.length < width) { 
row = new byte[width]; 
} 
image.getraster().getdataelements(left, top + y, width, 1, row); 
return row; 
} 
@override 
public byte[] getmatrix() { 
int width = getwidth(); 
int height = getheight(); 
int area = width * height; 
byte[] matrix = new byte[area]; 
image.getraster().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, this.left + left, this.top + top, width, height); 
} 
@override 
public boolean isrotatesupported() { 
return true; 
} 
@override 
public luminancesource rotatecounterclockwise() { 
int sourcewidth = image.getwidth(); 
int sourceheight = image.getheight(); 
affinetransform transform = new affinetransform(0.0, -1.0, 1.0, 0.0, 0.0, sourcewidth); 
bufferedimage rotatedimage = new bufferedimage(sourceheight, sourcewidth, bufferedimage.type_byte_gray); 
graphics2d g = rotatedimage.creategraphics(); 
g.drawimage(image, transform, null); 
g.dispose(); 
int width = getwidth(); 
return new bufferedimageluminancesource(rotatedimage, top, sourcewidth - (left + width), getheight(), width); 
} 
} 

解析二维码的代码

multiformatreader formatreader = new multiformatreader(); 
string filepath = "图片的路径"; 
file file = new file(filepath); 
bufferedimage image = imageio.read(file);; 
luminancesource source = new bufferedimageluminancesource(image); 
binarizer binarizer = new hybridbinarizer(source); 
binarybitmap binarybitmap = new binarybitmap(binarizer); 
map hints = new hashmap(); 
hints.put(encodehinttype.character_set, "utf-8"); 
result result = formatreader.decode(binarybitmap,hints); 
system.out.println("result = "+ result.tostring()); 
system.out.println("resultformat = "+ result.getbarcodeformat()); 
system.out.println("resulttext = "+ result.gettext()); 
tch (exception e) { 
e.printstacktrace(); 

现在这样可以在控制台看到二维码的内容。

以上所述是小编给大家介绍的zxing-core生成二维码的方法并解析的相关知识,希望对大家有所帮助