python二维码识别读取_python调用zxing项目进行二维码识别

摘要:首先创建一个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。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值