使用pyzbar扫描特定二维码出现exit code -1073740940错误的解决咨询
解决pyzbar扫描特定二维码出现0xC0000374错误的方法
0xC0000374是Windows系统的堆损坏错误,大概率是pyzbar依赖的底层zbar库与当前Python、Pillow版本不兼容,或目标二维码图片存在特殊格式问题导致,可尝试以下方案:
方案1:降级Pillow到兼容版本
pyzbar 0.1.9发布时间较早,与Pillow 10.x系列的API变更存在兼容性问题,降级到9.x版本即可解决:
pip uninstall pillow -y pip install pillow==9.5.0
方案2:重新安装适配Windows的pyzbar包
卸载现有pyzbar后,安装针对Python 3.8编译的Windows专属wheel包(避免pip自动安装的通用包存在适配问题):
- 卸载现有包:
pip uninstall pyzbar -y - 下载对应Python 3.8的pyzbar wheel包(如
pyzbar-0.1.9-cp38-cp38-win_amd64.whl,根据系统位数选择) - 安装下载的包:
pip install pyzbar-0.1.9-cp38-cp38-win_amd64.whl
方案3:预处理图片后再识别
对目标二维码图片做灰度转换、对比度增强,消除图片格式或清晰度问题导致的堆错误,修改代码如下:
from pyzbar.pyzbar import decode from PIL import Image, ImageEnhance def decode_qr_code(image_path): image = Image.open(image_path) # 转换为灰度图像 image = image.convert('L') # 增强对比度(系数可根据实际调整) enhancer = ImageEnhance.Contrast(image) image = enhancer.enhance(2) decoded_objects = decode(image) if decoded_objects: for obj in decoded_objects: print('Type:', obj.type) print('Data:', obj.data.decode('utf-8')) else: print('No QR code found in the image.') decode_qr_code('moduCode.jpg')
方案4:更换其他二维码识别库
如果上述方案无效,可尝试使用兼容性更好的pyzxing库替代:
- 安装pyzxing:
pip install pyzxing - 替换后的代码:
from pyzxing import BarCodeReader def decode_qr_code(image_path): reader = BarCodeReader() results = reader.decode(image_path) if results: for result in results: print('Type:', result.format) print('Data:', result.parsed) else: print('No QR code found in the image.') decode_qr_code('moduCode.jpg')
内容的提问来源于stack exchange,提问作者kaiqi song
相关产品推荐
相关产品推荐

