二维码生成和解析

package com.sp.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Hashtable;
import java.util.Map;

import javax.imageio.ImageIO;

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.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;

/**
* 二维码生成器
*
* @blog http://sjsky.iteye.com
* @author Michael
*/
public class ZxingHandler {

/**
* 生成二维码
*
* @param contents
* @param width
* @param height
* @param imgPath
*/
public static void encode(String contents, int width, int height, String imgPath,String path) {
//判断文件夹是否存在
File uploadDir = new File(path);
if (!uploadDir.isDirectory()) {
uploadDir.mkdirs();
}
Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
// 指定纠错等级
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
// 指定编码格式
hints.put(EncodeHintType.CHARACTER_SET, "GBK");
try {
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
BarcodeFormat.QR_CODE, width, height, hints);

MatrixToImageWriter.writeToStream(bitMatrix, "png",new FileOutputStream(imgPath));

} catch (Exception e) {
e.printStackTrace();
}
}


/**
* 解析二维码
*
* @param imgPath
* @return
*/
public static String decode(String imgPath) {
BufferedImage image = null;
Result result = null;
try {
image = ImageIO.read(new File(imgPath));
if (image == null) {
System.out.println("the decode image may be not exit.");
}
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));

Map<DecodeHintType, Object> hints = new Hashtable<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, "GBK");

result = new MultiFormatReader().decode(bitmap, hints);
return result.getText();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String contents ="http://baidu.com";
String imgPath = "E:/500005G.png";
//encode(contents, 800, 800, imgPath,"E:/");
System.out.println("解析结果=="+decode("E:/500005G.png"));
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是实现二维码生成解析的代码示例: 1. 二维码生成 ```python import qrcode # 输入需要生成二维码的信息 data = "https://www.baidu.com" # 创建QRCode对象并生成二维码 qr = qrcode.QRCode(version=1, box_size=10, border=5) qr.add_data(data) qr.make(fit=True) # 将二维码保存为PNG格式图片 img = qr.make_image(fill_color="black", back_color="white") img.save("qrcode.png") ``` 2. 二维码解析 ```python from pyzbar.pyzbar import decode from PIL import Image # 打开图片并解析二维码信息 img = Image.open("qrcode.png") data = decode(img) # 输出解析结果 print(data[0][0].decode('utf-8')) ``` 3. 使用Tkinter设计程序 ```python import tkinter as tk import qrcode from pyzbar.pyzbar import decode from PIL import ImageTk, Image class QRCodeGenerator(tk.Frame): def __init__(self, master=None): super().__init__(master) self.master = master self.pack() self.create_widgets() def create_widgets(self): # 输入框和生成按钮 self.input_label = tk.Label(self, text="输入信息:") self.input_label.grid(row=0, column=0) self.input_entry = tk.Entry(self) self.input_entry.grid(row=0, column=1) self.generate_button = tk.Button(self, text="生成二维码", command=self.generate_qrcode) self.generate_button.grid(row=0, column=2) # 显示生成的二维码 self.qrcode_image = tk.Label(self) self.qrcode_image.grid(row=1, column=0, columnspan=3) # 解析按钮和结果 self.parse_button = tk.Button(self, text="解析二维码", command=self.parse_qrcode) self.parse_button.grid(row=2, column=0) self.result_label = tk.Label(self) self.result_label.grid(row=2, column=1, columnspan=2) def generate_qrcode(self): # 从输入框中获取信息并生成二维码 data = self.input_entry.get() qr = qrcode.QRCode(version=1, box_size=10, border=5) qr.add_data(data) qr.make(fit=True) # 将二维码显示在界面上 img = qr.make_image(fill_color="black", back_color="white") img = ImageTk.PhotoImage(img) self.qrcode_image.configure(image=img) self.qrcode_image.image = img def parse_qrcode(self): # 解析二维码并显示结果 img = Image.open("qrcode.png") data = decode(img) if data: self.result_label.config(text=data[0][0].decode('utf-8')) else: self.result_label.config(text="无法解析二维码!") root = tk.Tk() app = QRCodeGenerator(master=root) app.mainloop() ``` 这个程序包含了生成二维码、显示二维码、解析二维码和显示解析结果等功能。你可以根据需要进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值