吾羽 發表於 2025-7-22 14:48:00

国产化条码类库Spire.Barcode教程:如何使用 Python 读取条形码(从图像文件或字节流)

<p><img src="https://image.evget.com/attachment/keditor/image/20250722/140704_8.png"></p>
<p>现代商业系统高度依赖条形码扫描功能,从零售收银通道到仓库库存追踪皆是如此。如今,强大的编程库使得将这一核心功能集成到定制化软件解决方案中变得异常简单。其中,基于 Python 的实现方案因其多功能性和易用性而格外受欢迎。</p>
<p>本文将介绍如何使用<strong><u>&nbsp;</u></strong><strong><u>Spire.Barcode for Python</u></strong><strong><u>&nbsp;</u></strong>库&nbsp;<strong>在 Python 中读取条形码</strong>,该库提供了简单易用的条形码扫描API。文章涵盖库的安装配置、从图像文件及字节流中读取条形码、设置自定义识别选项等全方位内容。</p>
<p style="text-align: center"><strong>&nbsp;<span style="color: rgba(230, 126, 35, 1)"> &nbsp;Spire.Barcode for .NET正版免费试用,请联系<span style="color: rgba(0, 0, 0, 1)">慧都科技</span></span></strong></p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><em><strong>加入Spire技术交流QQ群(125237868),与更多开发者一起提升文档开发技能。</strong></em></span></p>
<h2>Python 条形码识别库</h2>
<p><strong><u>Spire.Barcode for Python&nbsp;</u></strong>是一款专为 Python 应用程序设计的强大条形码生成与读取库,凭借其丰富的功能和易用的接口脱颖而出。该库支持多种条形码格式,包括:</p>
<ul>
<li><strong>一维条形码</strong>:如 Code 128、Code 39、EAN-13 和 UPC-A。</li>
<li><strong>二维条形码</strong>:包括 QR 码、DataMatrix 和 PDF417。</li>
</ul>
<p><strong><u>Spire.Barcode</u></strong>&nbsp;的核心特性:</p>
<ul>
<li><strong>多格式支持</strong>:可从 PNG、JPG、BMP、GIF 和 TIFF 等多种图像格式中读取条形码。</li>
<li><strong>批量扫描</strong>:支持单张图像内多个条形码的批量识别。</li>
<li><strong>高精度识别</strong>:采用先进算法,确保稳定可靠的条形码检测。</li>
<li><strong>灵活定制</strong>:允许用户指定条形码类型并启用校验和验证,以提升识别效率。</li>
</ul>
<p>该库不仅支持从图像和流中读取条形码,还提供丰富的自定义选项,满足多样化的应用需求。</p>
<h2>将 Spire.Barcode 集成到Python 应用程序</h2>
<p>要开始使用&nbsp;<strong><u>Spire.Barcode</u></strong>,首先需要安装该库,您可以通过 pip 完成此操作。打开终端并运行以下命令:</p>
<p style="text-align: center"><strong><span style="color: rgba(230, 126, 35, 1)">Spire.Barcode for .NET正版免费试用,请联系<span style="color: rgba(0, 0, 0, 1)">慧都科技</span></span></strong></p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><em><strong>加入Spire技术交流QQ群(125237868),与更多开发者一起提升文档开发技能。</strong></em></span></p>
<pre class="prettyprint lang-js highlighter-hljs"><code>pip install spire.barcode</code></pre>
<p>安装库后,您需要一个许可证密钥来解锁全部功能。您可以从我们的官网获取试用许可证。获得许可证密钥后,在 Python 脚本中设置库:</p>
<pre class="prettyprint lang-js highlighter-hljs"><code>from spire.barcode import *

# 应用许可证密钥以解锁全部功能
License.SetLicenseKey("your license key")</code></pre>
<p>现在库已准备就绪,您就可以开始使用 Python 读取条形码了。</p>
<h2>Python 从图像文件中读取条形码</h2>
<p>使用<strong>&nbsp;Spire.Barcode&nbsp;</strong>从图像文件中读取单个条形码非常简单,具体操作如下:</p>
<pre class="prettyprint lang-js highlighter-hljs"><code>from spire.barcode import *

# 应用许可证密钥以解锁全部功能
License.SetLicenseKey("your license key")

# 从图像文件读取单个条形码
result = BarcodeScanner.ScanOneFile("C:/Users/Administrator/Desktop/qr_code.png")

# 打印结果
print(result)</code></pre>
<p><strong>代码说明</strong></p>
<ul>
<li><strong>License.SetLicenseKey</strong>&nbsp;:使用许可证密钥解除功能限制。</li>
<li><strong>BarcodeScanner.ScanOneFile</strong>&nbsp;:从指定图像文件中读取单个条形码。</li>
<li><strong>输出结果</strong>&nbsp;:控制台将打印识别出的条形码数据。</li>
</ul>
<p><strong>效果图:</strong></p>
<p><img src="https://www.e-iceblue.cn/images/tutorials-images/read_single_barcode_code_cn_20250718.png"></p>
<h2>Python 从图像文件中读取多个条形码</h2>
<p>若需从单个图像文件中读取多个条形码,<strong>Spire.Barcode</strong>同样能轻松实现。以下是具体示例:</p>
<pre class="prettyprint lang-js highlighter-hljs"><code>from spire.barcode import *

# 应用许可证密钥以解锁全部功能
License.SetLicenseKey("your license key")

# 从文件读取多个条形码
results = BarcodeScanner.ScanFile("C:/Users/Administrator/Desktop/barcodes.jpg")

# 打印结果
print(results)</code></pre>
<p><strong>代码说明</strong></p>
<ul>
<li><strong>BarcodeScanner.ScanFile</strong>:扫描整张图像以识别多个条形码</li>
<li><strong>返回结果</strong>:以列表形式存储,每个元素包含一个检测到的条形码数据</li>
</ul>
<p><strong>效果图:</strong></p>
<p><img src="https://www.e-iceblue.cn/images/tutorials-images/python_read_barcodes_20250718.jpg"></p>
<h2>Python 从图像字节流中读取条形码</h2>
<p>除了直接从文件中读取条形码,<strong>Spire.Barcode for Python</strong>&nbsp;还支持从内存中的图像字节解码条形码,这种方案特别适用于处理动态加载的图像(如来自API接口、数据库或用户上传的场景)。</p>
<p>以下是实现方法:</p>
<pre class="prettyprint lang-js highlighter-hljs"><code>from spire.barcode import *

# 应用许可证密钥以解锁全部功能
License.SetLicenseKey("your license key")

# 读取图像文件到内存
image_path = "C:/Users/Administrator/Desktop/barcodes.jpg"
with open(image_path, "rb") as file:
    image_bytes = file.read()

# 将字节流封装为 Spire.Barcode 的 Stream 对象中
stream = Stream(image_bytes)

# 从流中读取一个条形码
# result = BarcodeScanner.ScanOneStream(stream)

# 从流中读取多个条形码
results = BarcodeScanner.ScanStream(stream)

# 打印结果
print(results)</code></pre>
<p><strong>代码说明</strong></p>
<ul>
<li><strong>image_bytes</strong>:从图像文件(如PNG/JPG)或API/数据库等来源获取的原始二进制数据。</li>
<li><strong>Stream类</strong>:将图像字节流转换为Spire.Barcode兼容的内存流对象。</li>
<li><strong>ScanStream方法</strong>:支持返回检测到的所有条形码列表。</li>
</ul>
<h2>条形码识别参数配置指南</h2>
<p><strong>Spire.Barcode</strong>&nbsp;的 BarcodeScanner 类提供多种方法用于自定义识别参数,可显著提升检测精度和效率。核心配置方法包括:</p>
<ul>
<li>ScanOneFileBarCodeTypeIncludeCheckSum(fileName: str, barcodeType: BarCodeType, IncludeCheckSum: bool)</li>
<li>ScanFileBarCodeTypeIncludeCheckSum(fileName: str, barcodeType: BarCodeType, IncludeCheckSum: bool)</li>
<li>ScanOneStreamBarCodeTypeIncludeCheckSum(stream: Stream, barcodeType: BarCodeType, IncludeCheckSum: bool)</li>
<li>ScanStreamBarCodeTypeIncludeCheckSum(stream: Stream, barcodeType: BarCodeType, IncludeCheckSum: bool)</li>
</ul>
<p><strong>应用示例(指定条码类型 + 校验和验证)</strong></p>
<pre class="prettyprint lang-js highlighter-hljs"><code>from spire.barcode import *

# 应用许可证密钥以解锁全部功能
License.SetLicenseKey("your license key")

# 指定条形码类型(例如,EAN13)
barcode_type = BarCodeType.EAN13

# 带校验和验证的条码扫描
result = BarcodeScanner.ScanOneFileBarCodeTypeIncludeCheckSum("C:/Users/Administrator/Desktop/EAN_13.png", barcode_type, True)

# 打印结果
print(result)</code></pre>
<p><strong>代码说明</strong></p>
<ul>
<li><strong>条码类型(BarcodeType)</strong>:精确指定待识别的条码格式,可有效减少误识别。</li>
<li><strong>校验和验证(IncludeCheckSum)</strong>:确定在扫描过程中是否验证校验和。将其设置为 True 可以帮助捕捉数据中的错误;设置为 False 则可跳过验证,提升识别速度。</li>
</ul>
<h2>总结</h2>
<p>本文详细介绍了如何使用<strong>&nbsp;Spire.Barcode&nbsp;</strong>库在 Python 中实现条形码识别功能,内容包括:</p>
<ul>
<li>环境配置与库安装</li>
<li>单条码/批量条码的图像文件识别</li>
<li>内存字节流的条码解析方案</li>
<li>通过参数定制提升识别精度的技巧</li>
</ul>
<p>掌握这些方法后,您可轻松为 Python 应用程序集成专业的条码扫描能力。</p>
<h2>常见问题解答</h2>
<h3>Q1: 我可以使用 Spire.Barcode 读取哪些类型的条形码?</h3>
<p><strong>Spire.Barcode&nbsp;</strong>支持多种条形码格式,包括 QR 码、UPC、EAN、Code 128、Code 39 等。</p>
<h3>Q2: 我需要许可证才能使用 Spire.Barcode 吗?</h3>
<p>是的,您需要许可证密钥以解锁库的全部功能。</p>
<h3>Q3: 我可以使用 Spire.Barcode 从网络摄像头读取条形码吗?</h3>
<p>虽然&nbsp;<strong>Spire.Barcode</strong>&nbsp;不直接支持网络摄像头输入,但您可以从网络摄像头捕获图像,然后使用该库从这些图像中读取条形码。</p>
<h3>Q4: 如何提高条形码扫描的准确性?</h3>
<p>您可以通过指定条形码类型和在扫描过程中启用校验和验证来提高准确性。此外,确保图像足够清晰。</p>
<h3>Q5: 我可以使用 Spire.Barcode for Python 生成条形码吗?</h3>
<p>是的,<strong>Spire.Barcode&nbsp;</strong>支持条形码识别和生成。</p>
<p style="text-align: center"><strong><span style="color: rgba(230, 126, 35, 1)">Spire.Barcode for .NET正版免费试用,请联系<span style="color: rgba(0, 0, 0, 1)">慧都科技</span></span></strong></p>
<p style="text-align: center"><span style="color: rgba(230, 126, 35, 1)"><em><strong>加入Spire技术交流QQ群(125237868),与更多开发者一起提升文档开发技能。</strong></em></span></p><br><br>
来源:https://www.cnblogs.com/software-Development/p/18998759
頁: [1]
查看完整版本: 国产化条码类库Spire.Barcode教程:如何使用 Python 读取条形码(从图像文件或字节流)