Android Studio+Chaquopy开发OCR应用:Tesseract未找到问题求助
问题分析
你遇到的核心问题是配置了Windows系统的Tesseract路径到安卓应用中——安卓是基于Linux的操作系统,Chaquopy运行在安卓的Linux环境下,完全无法识别Windows的C:\路径,因此抛出TesseractNotFoundError。另外还有两个次要问题:Bitmap传递方式错误、tessdata路径配置无效。
解决方案
1. 替换Windows路径,配置安卓环境的Tesseract可执行文件
步骤1:获取安卓版Tesseract二进制文件
从Tesseract官方安卓工具库下载对应你应用架构(arm64-v8a/armeabi-v7a/x86_64等)的tesseract可执行文件,或者自行编译静态版安卓Tesseract。
步骤2:将二进制文件放入项目并复制到应用私有目录
- 将
tesseract文件放到src/main/assets/tesseract/目录下 - 在安卓启动时(比如Application的
onCreate或MainActivity的onCreate),将文件复制到应用私有文件目录并添加可执行权限:
private void copyTesseractBinary() { File destFile = new File(getFilesDir(), "tesseract"); if (!destFile.exists()) { try (InputStream is = getAssets().open("tesseract/tesseract"); OutputStream os = new FileOutputStream(destFile)) { byte[] buffer = new byte[1024]; int length; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } // 添加可执行权限 destFile.setExecutable(true); } catch (IOException e) { e.printStackTrace(); } } }
2. 修正tessdata路径配置
- 将
tessdata文件夹(包含eng.traineddata等语言包)放到src/main/assets/tessdata/目录下 - 同样在启动时复制到应用私有目录:
private void copyTessData() { File destDir = new File(getFilesDir(), "tessdata"); if (!destDir.exists()) { destDir.mkdirs(); try { String[] files = getAssets().list("tessdata"); for (String file : files) { try (InputStream is = getAssets().open("tessdata/" + file); OutputStream os = new FileOutputStream(new File(destDir, file))) { byte[] buffer = new byte[1024]; int length; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } } } } catch (IOException e) { e.printStackTrace(); } } }
3. 修改Python脚本(ocr.py)
更新Tesseract路径和图像处理逻辑,通过Chaquopy API获取安卓应用路径:
import pytesseract from PIL import Image as PIL_image from io import BytesIO from com.chaquo.python import Python # 获取安卓应用上下文,获取私有文件目录路径 context = Python.getInstance().getContext() tesseract_path = context.getFilesDir().getAbsolutePath() + "/tesseract" tessdata_dir = context.getFilesDir().getAbsolutePath() + "/tessdata" # 配置Tesseract路径和语言包目录 pytesseract.pytesseract.tesseract_cmd = tesseract_path tessdata_dir_config = f'--tessdata-dir "{tessdata_dir}"' def bitmaptoString(byte_array): # 将Java传递的字节数组转为PIL图像 img = PIL_image.open(BytesIO(byte_array)) res = pytesseract.image_to_string(img, lang='eng', config=tessdata_dir_config) return res def test(): return "chaquopy in the works"
4. 修正Java调用逻辑
不能直接传递bitmap.toString(),需要将Bitmap转为字节数组传递给Python:
// 先调用复制文件的方法(在启动时执行) copyTesseractBinary(); copyTessData(); // 调用Python函数的正确代码 Python python = Python.getInstance(); PyObject pobj = python.getModule("ocr"); // 将Bitmap转为字节数组 ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] byteArray = stream.toByteArray(); // 调用函数并获取结果 PyObject result = pobj.callAttr("bitmaptoString", byteArray); String ouText = result.toString(); Log.e("_______________","TESSER:"); Log.e("OUTPUT_OF_PYFUNC",ouText);
替代方案:直接使用安卓原生Tesseract库
如果Chaquopy的方式过于繁琐,也可以直接集成tesseract-android-tools或com.rmtheis:tess-two等安卓原生Tesseract库,避免Python层的调用问题,实现更贴合安卓生态。
内容的提问来源于stack exchange,提问作者medamin
相关产品推荐
相关产品推荐

