Python 官方文档:入门教程 => 点击学习
摘要:首先创建一个java的Maven项目,加入zxing相关包,编写二维码相关代码,调试运行,打包;然后创建一个python项目,安装jpype,编写代码把相关的jar包加载,运行。 0. 创建一个maven项目 1. 配置po
摘要:首先创建一个java的Maven项目,加入zxing相关包,编写二维码相关代码,调试运行,打包;然后创建一个python项目,安装jpype,编写代码把相关的jar包加载,运行。
<!-- https://mvnrepository.com/artifact/com.Google.zxing/core -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastJSON</artifactId>
<version>1.1.29</version>
</dependency>
import java.io.File;
import java.io.IOException;
import java.NIO.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import javax.imageio.ImageIO;
import com.alibaba.fastjson.JSONObject;
import com.google.zxing.BarcodeFORMat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.NotFoundException;
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;
public class QRCode {
public static void main(String[] args) {
QRCode qrcode = new QRCode();
// 内容
JSONObject json = new JSONObject();
json.put("blog", "Http://blog.csdn.net/ld326");
json.put("author", "happyprince");
String content = json.toJSONString();
// 生成二维码
qrcode.EncodeQR("D:\\info-qr.png", content, 200, 200, "png");
// 解释二维码
boolean b = qrcode.DecodeQR("D:\\info-qr.png");
if (b) {
System.out.println("解釋出來是二維碼!!!");
}
}
public void EncodeQR(String filePath, String content, int width,
int height, String format) {
Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
try {
// 生成矩阵
BitMatrix bitMatrix = new MultiFormatWriter().encode(content,
BarcodeFormat.QR_CODE, width, height, hints);
// 输出图像
Path path = FileSystems.getDefault().getPath(filePath);
System.out.println(path);
MatrixToImageWriter.writeToPath(bitMatrix, format, path);
} catch (WriterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("生成二维码成功.");
}
public boolean DecodeQR(String filePath) {
boolean b = false;
try {
// 读入图片,转化成位图
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(
new BufferedImageLuminanceSource(ImageIO.read(new File(
filePath)))));
// 设置额外的一些信息,例如读取入来的提示信息
Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
// 对图像进行解码
Result result = new MultiFormatReader().decode(binaryBitmap, hints);
// 打印信息
// sb.append("content: " + result.getText() + " ");
// sb.append("format: " + result.getBarcodeFormat() + " ");
b = true;
} catch (IOException e) {
// sb.append(e + " " + "filePath=" + filePath);
e.printStackTrace();
} catch (NotFoundException e) {
// sb.append(e + " " + "filePath=" + filePath);
e.printStackTrace();
}
return b;
}
}
生成qrcode包,命名为qrcode-1.0.0.jar;再把maven下载的两个包(javase-3.0.0.jar,core-3.3.0.jar)与这个包放在一起,一会会把这些包加载到JVM中运行。
目前配置的是win7,下载地址:http://www.lfd.uci.edu/~gohlke/Pythonlibs/#jpype
编写python代码
# -*- coding: utf-8 -*-
import logging
from jpype import *
from common.globalVars import JAR_PATH
@singleton
class QRCode:
"""
判断图片是否QR图片
"""
def __init__(self):
logging.info('starJVM..')
startJVM(getDefaultJVMPath(), "-ea", "-Djava.class.path=%s;%s;%s" % (
JAR_PATH + 'core-3.3.0.jar',
JAR_PATH + 'javase-3.0.0.jar',
JAR_PATH + 'qrcode-1.0.0.jar'
))
QRCode = JClass("com.lr.jpype.jpype.QRCode")
self.qr = QRCode()
def is_qrcode(self, file_name):
b = self.qr.DecodeQR(file_name)
return b
def __del__(self):
shutdownJVM()
logging.info('shutdownJVM..')
qrc = QRCode()
print(qrc.is_qrcode('d:/1.jpg'))
其中装饰类,为了完成单例的功能
import logging
def singleton(cls):
instances = {}
def wrapper(*args, **kwargs):
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
logging.info('singleton size %d' % (len(instances)))
return wrapper
总结,这个主要是python依靠jpype,jpype运行JVM,JVM调用相关的包处理QR,主要实现的包为google的zxing。
[happyprince, http://blog.csdn.net/ld326/article/details/78671099]
--结束END--
本文标题: python调用zxing项目进行二维码
本文链接: https://lsjlt.com/news/188235.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0