import os
import jpype
from jpype import *
class ZXQRcode(object):
def __del__(self):
# 关闭JVM
shutdownJVM()
# 生成二维码
def make_qr_code(self, file_data, file_path):
jar_path = os.path.join(os.path.abspath('.'), 'javase-3.4.0.jar')
jar_path2 = os.path.join(os.path.abspath('.'), "core-3.4.0.jar")
# 启动JVM
jpype.startJVM(getDefaultJVMPath(), "-ea", "-Djava.class.path=%s;%s" % (jar_path, jar_path2))
# assert False, 'STOP'
# 加载需要使用到的类型
MultiFormatWriter = JClass("com.google.zxing.MultiFormatWriter")
BarcodeFormat = JClass("com.google.zxing.BarcodeFormat")
File = JClass("java.io.File")
ImageIO = JClass("javax.imageio.ImageIO")
MatrixToImageWriter = JClass("com.google.zxing.client.j2se.MatrixToImageWriter")
EncodeHintType = JClass("com.google.zxing.EncodeHintType")
Hashtable = JClass("java.util.Hashtable")
# 设置Margin=0
hints = Hashtable()
hints.put(EncodeHintType.MARGIN, 0)
matrix = MultiFormatWriter().encode(file_data, BarcodeFormat.QR_CODE, 260, 260, hints)
image = MatrixToImageWriter.toBufferedImage(matrix)
ImageIO.write(image, "png", File(file_path))
def decode_qr_code(self, image_path):
jar_path = os.path.join(os.path.abspath('.'), 'javase-3.4.0.jar')
jar_path2 = os.path.join(os.path.abspath('.'), "core-3.4.0.jar")
# 启动JVM
jpype.startJVM(getDefaultJVMPath(), "-ea", "-Djava.class.path=%s;%s" % (jar_path, jar_path2))
# 加载需要使用到的类型
File = JClass("java.io.File")
ImageIO = JClass("javax.imageio.ImageIO")
BufferedImageLuminanceSource = JClass("com.google.zxing.client.j2se.BufferedImageLuminanceSource")
Hashtable = JClass("java.util.Hashtable")
MultiFormatReader = JClass("com.google.zxing.MultiFormatReader")
HybridBinarizer = JClass("com.google.zxing.common.HybridBinarizer")
DecodeHintType = JClass("com.google.zxing.DecodeHintType")
BinaryBitmap = JClass("com.google.zxing.BinaryBitmap")
imageFile = File(image_path)
image = ImageIO.read(imageFile)
source = BufferedImageLuminanceSource(image)
hybridBinarizer = HybridBinarizer(source)
binaryBitmap = BinaryBitmap(hybridBinarizer)
hints = Hashtable()
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8")
result = MultiFormatReader().decode(binaryBitmap, hints)
return result.getText()
if name == ‘main’:
date = '123456png'
filepath = os.getcwd() + "\\1234.png"
print(filepath)
jar_path = os.path.join(os.path.abspath('.'), 'javase-3.4.0.jar')
print(jar_path)
# zxq_rcode.make_qr_code(date, filepath)
# code = zxq_rcode.decode_qr_code(filepath)
# print(code)