java实现二维码的生成和解析代码

1zxing介绍

二维码的生成与解析。有多种途径。我们选择使用zxing,ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口。Zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码。

2zxing下载

Jar下载路径:

https://codeload.github.com/zxing/zxing/zip/master

整理关于java实现二维码的生成和解析代码供大家参考

3在项目中应用

3.1首先在项目里面引入zxing jar

整理关于java实现二维码的生成和解析代码供大家参考

3.2新建TestZXing类,编写产生二维码代码

package test;

import java.io.File;

import java.io.IOException;

import java.nio.file.Path;

import java.util.HashMap;

import com.google.zxing.BarcodeFormat;

import com.google.zxing.EncodeHintType;

import com.google.zxing.MultiFormatWriter;

import com.google.zxing.WriterException;

import com.google.zxing.client.j2se.MatrixToImageWriter;

import com.google.zxing.common.BitMatrix;

import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class TestZXing {

@SuppressWarnings({ "rawtypes", "unchecked" })

public static void main(String[] args) {

int width=300;

int height=300;

String format="png";

String contents="www.baidu.com";

HashMap map=new HashMap();

map.put(EncodeHintType.CHARACTER_SET, "utf-8");

map.put(EncodeHintType.ERROR_CORRECTION,ErrorCorrectionLevel.M);

map.put(EncodeHintType.MARGIN, 0);

try {

BitMatrix bm = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height);

Path file=new File("D:/img.png").toPath();

MatrixToImageWriter.writeToPath(bm, format, file);

} catch (WriterException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

3.3创建TestRead类,编写解析二维码代码,运行代码,打印测试结果

package test;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.util.HashMap;

import javax.imageio.ImageIO;

import com.google.zxing.BinaryBitmap;

import com.google.zxing.EncodeHintType;

import com.google.zxing.MultiFormatReader;

import com.google.zxing.NotFoundException;

import com.google.zxing.Result;

import com.google.zxing.client.j2se.BufferedImageLuminanceSource;

import com.google.zxing.common.HybridBinarizer;

public class TestRead {

@SuppressWarnings({ "unchecked", "rawtypes" })

public static void main(String[] args) {

try {

MultiFormatReader reader=new MultiFormatReader();

File f=new File("D:/img.png");

BufferedImage image=ImageIO.read(f);

BinaryBitmap bb=new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));

HashMap map =new HashMap();

map.put(EncodeHintType.CHARACTER_SET, "utf-8");

Result result = reader.decode(bb,map);

System.out.println("解析结果:"+result.toString());

System.out.println("二维码格式类型:"+result.getBarcodeFormat());

System.out.println("二维码文本内容:"+result.getText());

} catch (NotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

整理关于java实现二维码的生成和解析代码供大家参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值