条形码和二维码的解析

鉴于条形码相对于二维码有很多不足的地方,存储信息上有较大的区别,而且随着当下智能手机照相功能的不断发展,对二维码有非常好的识别。
这里主要讲解对二维码的解析

条形码和二维码

条形码这是上个世纪40年代发明的老古董了,在当今仍有很广泛的用途。

处理Jar包有

1.Zxing(Zebra Crossing)
这是Google出品的的,性能就不必多说
主要类
• BitMatrix 位图矩阵
• MultiFormatWriter 位图编写器
• MatrixToImageWriter 写入图片

2.Barcode4J
– 只负责生成,不负责解析
http://barcode4j.sourceforge.net/
主要类
• BarcodeUtil
• BarcodeGenerator
• DefaultConfiguration

这里主要以Google的包演示为主,由于安卓手机占比较大,所以这个包应用得较为广泛

一维码解析
pom

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.test</groupId><artifactId>MOOC16-05</artifactId><version>0.0.1-SNAPSHOT</version><dependencies><!-- https://mvnrepository.com/artifact/com.google.zxing/core --><dependency><groupId>com.google.zxing</groupId><artifactId>core</artifactId><version>3.3.3</version></dependency><!-- https://mvnrepository.com/artifact/com.google.zxing/javase --><dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.3.3</version></dependency><!-- https://mvnrepository.com/artifact/net.sf.barcode4j/barcode4j --><dependency><groupId>net.sf.barcode4j</groupId><artifactId>barcode4j</artifactId><version>2.1</version></dependency>  	<!-- https://mvnrepository.com/artifact/org.apache.avalon.framework/avalon-framework-api --><dependency><groupId>org.apache.avalon.framework</groupId><artifactId>avalon-framework-api</artifactId><version>4.3.1</version></dependency></dependencies><properties><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion></properties></project>
package zxing;import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.LuminanceSource;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;import javax.imageio.ImageIO;import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;public class BarCodeTest {/*** generateCode 根据code生成相应的一维码* @param file 一维码目标文件* @param code 一维码内容* @param width 图片宽度* @param height 图片高度*/public static void generateCode(File file, String code, int width, int height) {//定义位图矩阵BitMatrixBitMatrix matrix = null;try {// 使用code_128格式进行编码生成100*25的条形码MultiFormatWriter writer = new MultiFormatWriter();matrix = writer.encode(code,BarcodeFormat.CODE_128, width, height, null);//matrix = writer.encode(code,BarcodeFormat.EAN_13, width, height, null);} catch (WriterException e) {e.printStackTrace();}//将位图矩阵BitMatrix保存为图片try (FileOutputStream outStream = new FileOutputStream(file)) {ImageIO.write(MatrixToImageWriter.toBufferedImage(matrix), "png",outStream);outStream.flush();} catch (Exception e) {e.printStackTrace();}}/*** readCode 读取一张一维码图片* @param file 一维码图片名字*/public static void readCode(File file){try {BufferedImage image = ImageIO.read(file);if (image == null) {return;}LuminanceSource source = new BufferedImageLuminanceSource(image);BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));Map<DecodeHintType, Object> hints = new HashMap<>();hints.put(DecodeHintType.CHARACTER_SET, "GBK");hints.put(DecodeHintType.PURE_BARCODE, Boolean.TRUE);hints.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);Result result = new MultiFormatReader().decode(bitmap, hints);//解析System.out.println("条形码内容: "+result.getText());} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) throws Exception {//generateCode(new File("1dcode.png"), "123456789012", 500, 250);readCode(new File("1dcode.png"));}
}

二维码解析

package zxing;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
public class QRCodeTest{/*定义二维码的宽度和高度*/private static int WIDTH=300;private static int HEIGHT=300;private static String FORMAT="png";//二维码图片的格式//生成二维码public static void generateQRCode(File file,String content){//定义二维码的一些参数Map<EncodeHintType,Object>hints=new HashMap<>();hints.put(EncodeHintType.CHARACTER_SET,"utf-8");//设置编码hints.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);//设置容错的等级hints.put(EncodeHintType.MARGIN,2);//设置边距默认为5try{BitMatrix bitMatrix=new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,WIDTH,HEIGHT,hints);//提醒一下:二维码并不单单指QRcode,其他的形式也可以类似的生成Path path=file.toPath();MatrixToImageWriter.writeToPath(bitMatrix,FORMAT,path);//写下指定的路径}catch(Exception e) {e.printStackTrace();}}//读取二维码public static void readQrCode(File file){MultiFormatReader reader=new MultiFormatReader();try{BufferedImage image=ImageIO.read(file);BinaryBitmap binaryBitmap=new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));Map<DecodeHintType,Object>hints=new HashMap<>();hints.put(DecodeHintType.CHARACTER_SET,"utf-8");//设置编码Result result=reader.decode(binaryBitmap,hints);System.out.println("解析结果为:"+result.toString());System.out.println("二维码的格式为"+result.getBarcodeFormat());System.out.println("二维码的文本内容为:"+result.getText());}catch(Exception e) {e.printStackTrace();}}public static void main(String[] args) {generateQRCode(new File("Garycode.png"),"https://blog.csdn.net/Garyboyboy");readQrCode(new File("Garycode.png"));//readQrCode(new File("Garycode.jpg"));}
}

好啦好啦,我把二维码放上来了还不关注咩
在这里插入图片描述

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注