当前位置: 首页 > 编程笔记 >

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

龚睿
2023-03-14
本文向大家介绍详解ZXing-core生成二维码的方法并解析,包括了详解ZXing-core生成二维码的方法并解析的使用技巧和注意事项,需要的朋友参考一下

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

其实主要是利用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生成二维码的方法并解析的相关知识,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对小牛知识库网站的支持!

 类似资料:
  • 本文向大家介绍使用python调用zxing库生成二维码图片详解,包括了使用python调用zxing库生成二维码图片详解的使用技巧和注意事项,需要的朋友参考一下 (1)安装Jpype 用Python调用jar包需要安装jpype扩展,在Ubuntu上可以直接使用apt-get安装jpype扩展 (2) 得到zxing  jar包 使用zxing第三方库生成二维码图片,关于zxing的介绍可以看其

  • 本文向大家介绍详解二维码生成工厂,包括了详解二维码生成工厂的使用技巧和注意事项,需要的朋友参考一下 本次主要分享的是3个免费的二维码接口的对接代码和测试得出的注意点及区别,有更好处理方式多多交流,相互促进进步;最近在学习JavsScript的扩展TypeScript,感觉语法糖很甜,大部分与C#更为类似,可能都是微软项目的原因吧,有兴趣的朋友可以多多相互交流下; 以上是个人的看法,下面来正式分享今

  • 本文向大家介绍Android中二维码的生成方法(普通二维码、中心Logo 二维码、及扫描解析二维码),包括了Android中二维码的生成方法(普通二维码、中心Logo 二维码、及扫描解析二维码)的使用技巧和注意事项,需要的朋友参考一下 首先声明我们通篇用的都是Google开源框架Zxing,要实现的功能有三个 ,生成普通二维码、生成带有中心图片Logo 的二维码,扫描解析二维码,直接上效果图吧 首

  • 本文向大家介绍Symfony生成二维码的方法,包括了Symfony生成二维码的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Symfony生成二维码的方法。分享给大家供大家参考,具体如下: 现在网上能搜到很多关于使用PHP生成二维码的例子,主要是两种方法: 第一种:google开放api,如下: 附:API接口地址 https://developers.google.com/char

  • 本文向大家介绍java二维码生成的方法,包括了java二维码生成的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了java二维码的实现代码,供大家参考,具体内容如下 这次用到的jar包是zxing,没有用到core的jar包 先导入zxing.jar包 生成二维码 解析二维码 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

  • 本文向大家介绍C#生成二维码的方法,包括了C#生成二维码的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了C#生成二维码的方法。分享给大家供大家参考。具体实现方法如下: 首先引用ThoughtWorks.QRCode.dll 具体代码如下: 使用示例如下: PS:本站还提供了一个功能非常强大的二维码生成工具,感兴趣的朋友可以参考一下: http://tools.jb51.net/tra