jayhust

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

[硬件环境]

Win10 64位

[软件环境]

Python版本:2.7.3

Python库:

1.1) Pillow

1.2) Pytesseract

2.1) Jpype

其他:

1.1) Tesseract-OCR的可执行文件

2.1) JDK 1.6

2.2) ZXing Jar包:javase-2.2.jar和core-2.2.jar

[搭建过程]

Tesseract-OCR:

1. 安装Tesseract-OCR的可执行文件

2. 安装Pillow库

3. 安装Pytesseract库

ZXing:

1. 安装JDK 1.6

2. 下载ZXing Jar包:javase-2.2.jar和core-2.2.jar供使用

3. 安装Jpype库以供Python调用Jar包

[相关代码]

# 1.1.Install tesseract-ocr*.exe from http://jaist.dl.sourceforge.net/project/tesseract-ocr-alt/tesseract-ocr-setup-3.02.02.exe
# 1.2.Install pillow as "pip install form *.whl" in the zip file directory in powershell
# 1.3.Install pytesseract as "pip install form *.whl" in the zip file directory in powershell
# 2.1.Install jpype as "python setup.py install" in the zip file directory in powershell
import os.path
import pytesseract
from PIL import Image
from jpype import *

##################################################### Tesserace-ocr ####################################################
image = Image.open(r"D:\Project\FirstPython\OCR2.png")
code = pytesseract.image_to_string(image)
code = code.replace(' ', '');
code = code.replace('-', '');
code = code.replace('\r', '');
code = code.replace('\n', '');
print(code)

##################################################### Zxing Decode #####################################################
# Start jvm
jarpath = os.path.join(os.path.abspath('.'), 'D:/Project/FirstPython/')
startJVM("C:/Program Files/Java/jre6/bin/server/jvm.dll", "-ea", ("-Djava.class.path=%s" % (jarpath + "javase-2.2.jar" + ";" + jarpath + "core-2.2.jar")))
# Load the useful library classes
File = JClass("java.io.File")
BufferedImage = JClass("java.awt.image.BufferedImage")
ImageIO = JClass("javax.imageio.ImageIO")
BinaryBitmap = JClass("com.google.zxing.BinaryBitmap")
DecodeHintType = JClass("com.google.zxing.DecodeHintType")
LuminanceSource = JClass("com.google.zxing.LuminanceSource")
BufferedImageLuminanceSource = JClass("com.google.zxing.client.j2se.BufferedImageLuminanceSource")
MultiFormatReader = JClass("com.google.zxing.MultiFormatReader")
NotFoundException = JClass("com.google.zxing.NotFoundException")
Result = JClass("com.google.zxing.Result")
HybridBinarizer = JClass("com.google.zxing.common.HybridBinarizer")
Hashtable = JClass("java.util.Hashtable")
# Do the qrcode decode
image = ImageIO.read(File("D:/Project/FirstPython/Qrcode1.jpg"))
source = BufferedImageLuminanceSource(image)
bitmap = BinaryBitmap(HybridBinarizer(source))
hints = Hashtable()
hints.put(DecodeHintType.CHARACTER_SET, "GBK")
result = MultiFormatReader().decode(bitmap, hints)
print(result)
# Shutdown JVM
shutdownJVM()

 

posted on 2017-02-14 16:45  jayhust  阅读(865)  评论(0编辑  收藏  举报