Python調用Tesseract-OCR和Zxing完成圖片OCR識別和二維碼解碼

[硬件環境]java

Win10 64位python

[軟件環境]shell

Python版本:2.7.3jvm

Python庫:google

1.1) Pillowspa

1.2) Pytesseract.net

2.1) Jpypecode

其餘:orm

1.1) Tesseract-OCR的可執行文件server

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()
相關文章
相關標籤/搜索