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

本文给大家介绍ZXing-core生成二维码的方法并解析,主要用到goggle发布的jar来实现二维码功能,对此文感兴趣的朋友一起学习吧

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

其实主要是利用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-600"); 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 

现在就可以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 = getHeight()) { throw new IllegalArgumentException("Requested row is outside the image: " + y); } int width = getWidth(); if (row == null || row.length 

解析二维码的代码

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

以上就是详解ZXing-core生成二维码的方法并解析的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java