摘要:首先创建一个java的maven项目,加入zxing相关包,编写二维码相关代码,调试运行,打包;然后创建一个python项目,安装jpype,编写代码把相关的jar包加载,运行。
0. 创建一个maven项目
1. 配置pom.xml文件
com.google.zxing
core
3.3.0
com.google.zxing
javase
3.0.0
com.alibaba
fastjson
1.1.29
2. 生成与识别二维码###
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 hints = new HashMap();
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 hints = new HashMap();
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;
}
}
3. 打包
生成qrcode包,命名为qrcode-1.0.0.jar;再把maven下载的两个包(javase-3.0.0.jar,core-3.3.0.jar)与这个包放在一起,一会会把这些包加载到jvm中运行。
4. 安装jpype
5. 创建python项目
编写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。