<h1 style="font-size: 32px; font-weight: bold; border-bottom: 2px solid rgb(204, 204, 204); padding: 0px 4px 0px 0px; text-align: left; margin: 0px 0px 10px;">二维码、条形码识别</h1><p style="margin-top: 0px; margin-bottom: 10px; text-wrap: wrap; padding: 0px; list-style: none; border: 0px; overflow-wrap: break-word; word-break: break-all; line-height: 1.5em; font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "WenQuanYi Micro Hei", Arial, sans-serif; color: rgb(51, 51, 51); box-sizing: border-box;">当前系列所有demo下载地址:</p><p style="margin-top: 0px; margin-bottom: 10px; text-wrap: wrap; padding: 0px; list-style: none; border: 0px; overflow-wrap: break-word; word-break: break-all; line-height: 1.5em; font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "WenQuanYi Micro Hei", Arial, sans-serif; color: rgb(51, 51, 51); box-sizing: border-box;"><a href="https://github.com/GaoRenBao/OpenCv4-Demo" target="_blank" style="margin: 0px; padding: 0px; list-style: none; border: 0px; color: rgb(0, 102, 0); transition-duration: 0.2s; transition-property: opacity; outline: none; opacity: 0.8;">https://github.com/GaoRenBao/OpenCv4-Demo</a></p><p style="margin-top: 0px; margin-bottom: 10px; text-wrap: wrap; padding: 0px; list-style: none; border: 0px; overflow-wrap: break-word; word-break: break-all; line-height: 1.5em; font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "WenQuanYi Micro Hei", Arial, sans-serif; color: rgb(51, 51, 51); box-sizing: border-box;"><a href="https://gitee.com/fuckgrb/OpenCv4-Demo" target="_blank" style="margin: 0px; padding: 0px; list-style: none; border: 0px; color: rgb(0, 102, 0); transition-duration: 0.2s; transition-property: opacity; outline: none;">https://gitee.com/fuckgrb/OpenCv4-Demo</a></p><p style="margin-top: 0px; margin-bottom: 10px; text-wrap: wrap; padding: 0px; list-style: none; border: 0px; overflow-wrap: break-word; word-break: break-all; line-height: 1.5em; font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "WenQuanYi Micro Hei", Arial, sans-serif; box-sizing: border-box; color: rgb(51, 51, 51);">不同编程语言对应的OpenCv版本以及<span style="">开发环境</span>信息如下:<span style="font-family: Calibri; font-size: 14px;"> </span></p><table border="1" style="text-wrap: wrap; border-right: none; border-bottom: none; border-image: initial; border-left: 1px solid rgb(102, 102, 102); border-top: 1px solid rgb(102, 102, 102);"><tbody><tr class="firstRow"><td width="81" valign="top" style="border-color: windowtext rgb(102, 102, 102) rgb(102, 102, 102) windowtext; border-bottom-width: 1px; border-bottom-style: solid; border-right-width: 1px; border-right-style: solid; padding: 5px;"><p style="text-align: center;"><strong><span style="font-family: 宋体; font-size: 14px;">语言</span></strong></p></td><td width="223" valign="top" style="border-color: windowtext rgb(102, 102, 102) rgb(102, 102, 102) windowtext; border-bottom-width: 1px; border-bottom-style: solid; border-right-width: 1px; border-right-style: solid; padding: 5px;"><p style="text-align: center;"><strong><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">OpenCv</span>版本</span></strong></p></td><td width="242" valign="top" style="border-color: windowtext rgb(102, 102, 102) rgb(102, 102, 102) windowtext; border-bottom-width: 1px; border-bottom-style: solid; border-right-width: 1px; border-right-style: solid; padding: 5px;"><p style="text-align: center;"><strong><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">IDE</span></span></strong></p></td></tr><tr><td width="81" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">C#</span></span></p></td><td width="223" valign="top" style="border-top: none; border-left-color: windowtext; word-break: break-all; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: Calibri; letter-spacing: 0px;">OpenCvSharp4.4.8.0.20230708</span></p></td><td width="242" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">Visual Studio 2022</span></span></p></td></tr><tr><td width="81" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">C++</span></span></p></td><td width="223" valign="top" style="border-top: none; border-left-color: windowtext; word-break: break-all; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; letter-spacing: 0px;"><span style="font-family: Calibri;">O</span></span><span style="font-family: Calibri; letter-spacing: 0px;">pen</span><span style="font-family: 宋体; letter-spacing: 0px;"><span style="font-family: Calibri;">C</span></span><span style="font-family: Calibri; letter-spacing: 0px;">v-4.5.5-vc14_vc15</span></p></td><td width="242" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">Visual Studio 2022</span></span></p></td></tr><tr><td width="81" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">Python</span></span></p></td><td width="223" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">OpenCv-Python (4.6.0.66)</span></span></p></td><td width="242" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">PyCharm Community Edition 2022.1.3</span></span></p></td></tr></tbody></table><p style="text-wrap: wrap;"><br/></p><p style="text-wrap: wrap;">测试原图如下:</p><p style="text-wrap: wrap;"><img src="/upload/image/6383668127954587673793451.jpg" title="macbookPro.jpg" alt="macbookPro.jpg"/></p><p style="text-wrap: wrap;"><br/></p><p style="text-wrap: wrap;">识别结果如下:</p><p style="text-wrap: wrap;"><img src="/upload/image/6383668132415534163958705.jpg" title="macbook_qr_rect.jpg" alt="macbook_qr_rect.jpg"/></p><p style="text-wrap: wrap;"><strong style="font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "WenQuanYi Micro Hei", Arial, sans-serif; margin: 0px; padding: 0px; list-style: none; border: 0px; color: rgb(51, 51, 51); "><span style="font-style: italic; color: rgb(51, 153, 204); line-height: 18px;"><br/></span></strong></p><p style="text-wrap: wrap;"><strong style="font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "WenQuanYi Micro Hei", Arial, sans-serif; margin: 0px; padding: 0px; list-style: none; border: 0px; color: rgb(51, 51, 51); "><span style="font-style: italic; color: rgb(51, 153, 204); line-height: 18px;">C#版本代码如下:</span></strong><br/></p><p>C#版本主要使用的是ZXing和ZBar进行识别。</p><p>ZXing同时支持二维码和条形码的识别,ZBar只支持条形码识别。这里要注意了,通过NuGet安装的ZBar包运行会提示找不到dll文件。只能手动将dll文件扔到执行文件目录下才行。</p><p><img src="/upload/image/6383684988973677873596724.jpg" title="QQ截图20231129101804.jpg" alt="QQ截图20231129101804.jpg"/> <img src="/upload/image/6383684999684313252206617.jpg" title="QQ截图20231129101954.jpg" alt="QQ截图20231129101954.jpg"/></p><p>完整代码如下:</p><pre class="brush:c#;toolbar:false">using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Collections.Generic;
using System.Drawing;
using ZXing;
namespace demo
{
internal class Program
{
/// <summary>
/// ZBar方法
/// 通过NuGet安装的ZBar包运行会提示找不到dll文件。
/// 只能手动将dll文件扔到执行文件目录下。
/// ZBar只支持条形码识别。
/// </summary>
/// <param name="srcImage"></param>
/// <returns></returns>
static string ZBarQrCode(Mat srcImage)
{
Bitmap pImg = BitmapConverter.ToBitmap(srcImage);
using (ZBar.ImageScanner scanner = new ZBar.ImageScanner())
{
scanner.SetConfiguration(ZBar.SymbolType.None, ZBar.Config.Enable, 0);
scanner.SetConfiguration(ZBar.SymbolType.CODE39, ZBar.Config.Enable, 1);
scanner.SetConfiguration(ZBar.SymbolType.CODE128, ZBar.Config.Enable, 1);
scanner.SetConfiguration(ZBar.SymbolType.QRCODE, ZBar.Config.Enable, 1);
List<ZBar.Symbol> symbols = new List<ZBar.Symbol>();
symbols = scanner.Scan((System.Drawing.Image)pImg);
if (symbols != null && symbols.Count > 0)
{
string result = string.Empty;
symbols.ForEach(s => result += "条码内容:" + s.Data + " 条码质量:" + s.Quality + Environment.NewLine);
return result;
}
}
return null;
}
/// <summary>
/// Zxing方法:ZXing.Net 0.16.9
/// 支持条形码和二维码的识别
/// </summary>
/// <param name="srcImage"></param>
/// <returns></returns>
static string ZxingQrCode(Mat srcImage)
{
Bitmap pImg = BitmapConverter.ToBitmap(srcImage);
Result result = new BarcodeReader().Decode(pImg);
return result?.Text;
}
static void Main(string[] args)
{
Mat img1 = Cv2.ImRead("../../../images/macbookPro.jpg");
Mat img2 = Cv2.ImRead("../../../images/macbookPro2.jpg");
//Console.WriteLine($"ZBarQrCode1: {ZBarQrCode(img1)}");
Console.WriteLine($"ZBarQrCode2: {ZBarQrCode(img2)}");
Console.WriteLine($"ZxingQrCode1: {ZxingQrCode(img1)}");
Console.WriteLine($"ZxingQrCode2: {ZxingQrCode(img2)}");
Console.Read();
}
}
}</pre><p style="text-wrap: wrap;"><br/></p><p style="text-wrap: wrap;"><strong style="font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "WenQuanYi Micro Hei", Arial, sans-serif; margin: 0px; padding: 0px; list-style: none; border: 0px; color: rgb(51, 51, 51); "><strong style="margin: 0px; padding: 0px; list-style: none; border: 0px;"><span style="font-style: italic; color: rgb(51, 153, 204); line-height: 18px;">C++版本代码如下:</span></strong></strong></p><p>C++ windows下的环境有点麻烦。</p><p>参考博客:<a href="https://blog.csdn.net/weixin_42975610/article/details/110165856?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522170122395716800182146129%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=170122395716800182146129&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~baidu_landing_v2~default-1-110165856-null-null.nonecase&utm_term=C%2B%2B%20ZXing&spm=1018.2226.3001.4450" target="_blank" title="C++利用zxing识别二维码">C++利用zxing识别二维码</a> </p><pre class="brush:cpp;toolbar:false">【暂缺】</pre><p style="text-wrap: wrap;"><br/></p><p style="text-wrap: wrap;"><strong style="font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "WenQuanYi Micro Hei", Arial, sans-serif; margin: 0px; padding: 0px; list-style: none; border: 0px; color: rgb(51, 51, 51); "><strong style="margin: 0px; padding: 0px; list-style: none; border: 0px;"><span style="font-style: italic; color: rgb(51, 153, 204); line-height: 18px;">Python版本代码如下:</span></strong></strong></p><p>需要安装“pyzbar”包</p><pre class="brush:bash;toolbar:false">pip install pyzbar -i https://mirror.baidu.com/pypi/simple</pre><p><br/></p><p>测试代码如下:</p><pre class="brush:python;toolbar:false">from pyzbar.pyzbar import decode
# from PIL import Image
import cv2
def main():
# fp = '../images/macbookPro.jpg'
fp = '../images/macbookPro2.jpg'
# image = Image.open(fp)
# image.show()
image = cv2.imread(fp)
barcodes = decode(image)
decoded = barcodes[0]
print(decoded)
#
url: bytes = decoded.data
url = url.decode()
print(url)
# rect
rect = decoded.rect
print(rect) # Rect(left=19, top=19, width=292, height=292)
# loop over the detected barcodes
for barcode in barcodes:
# extract the bounding box location of the barcode and draw the
# bounding box surrounding the barcode on the image
(x, y, w, h) = barcode.rect
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
# the barcode data is a bytes object so if we want to draw it on
# our output image we need to convert it to a string first
barcodeData = barcode.data.decode("utf-8")
barcodeType = barcode.type
# draw the barcode data and barcode type on the image
text = "{} ({})".format(barcodeData, barcodeType)
cv2.putText(image, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0, 0, 255), 2)
# print the barcode type and data to the terminal
print("[INFO] Found {} barcode: {}".format(barcodeType, barcodeData))
# show the output image
cv2.imshow("Image", image)
cv2.imwrite('macbook_qr_rect.jpg', image)
cv2.waitKey(0) # 按任意键退出
if __name__ == '__main__':
main()</pre><p><br/></p>
二维码、条形码识别 当前系列所有demo下载地址:
https://github.com/GaoRenBao/OpenCv4-Demo
https://gitee.com/fuckgrb/OpenCv4-Demo
不同编程语言对应的OpenCv版本以及开发环境 信息如下:
语言
OpenCv 版本
IDE
C#
OpenCvSharp4.4.8.0.20230708
Visual Studio 2022
C++
O pen C v-4.5.5-vc14_vc15
Visual Studio 2022
Python
OpenCv-Python (4.6.0.66)
PyCharm Community Edition 2022.1.3
测试原图如下:
识别结果如下:
C#版本代码如下:
C#版本主要使用的是ZXing和ZBar进行识别。
ZXing同时支持二维码和条形码的识别,ZBar只支持条形码识别。这里要注意了,通过NuGet安装的ZBar包运行会提示找不到dll文件。只能手动将dll文件扔到执行文件目录下才行。
完整代码如下:
using OpenCvSharp;
using OpenCvSharp.Extensions;
using System;
using System.Collections.Generic;
using System.Drawing;
using ZXing;
namespace demo
{
internal class Program
{
/// <summary>
/// ZBar方法
/// 通过NuGet安装的ZBar包运行会提示找不到dll文件。
/// 只能手动将dll文件扔到执行文件目录下。
/// ZBar只支持条形码识别。
/// </summary>
/// <param name="srcImage"></param>
/// <returns></returns>
static string ZBarQrCode(Mat srcImage)
{
Bitmap pImg = BitmapConverter.ToBitmap(srcImage);
using (ZBar.ImageScanner scanner = new ZBar.ImageScanner())
{
scanner.SetConfiguration(ZBar.SymbolType.None, ZBar.Config.Enable, 0);
scanner.SetConfiguration(ZBar.SymbolType.CODE39, ZBar.Config.Enable, 1);
scanner.SetConfiguration(ZBar.SymbolType.CODE128, ZBar.Config.Enable, 1);
scanner.SetConfiguration(ZBar.SymbolType.QRCODE, ZBar.Config.Enable, 1);
List<ZBar.Symbol> symbols = new List<ZBar.Symbol>();
symbols = scanner.Scan((System.Drawing.Image)pImg);
if (symbols != null && symbols.Count > 0)
{
string result = string.Empty;
symbols.ForEach(s => result += "条码内容:" + s.Data + " 条码质量:" + s.Quality + Environment.NewLine);
return result;
}
}
return null;
}
/// <summary>
/// Zxing方法:ZXing.Net 0.16.9
/// 支持条形码和二维码的识别
/// </summary>
/// <param name="srcImage"></param>
/// <returns></returns>
static string ZxingQrCode(Mat srcImage)
{
Bitmap pImg = BitmapConverter.ToBitmap(srcImage);
Result result = new BarcodeReader().Decode(pImg);
return result?.Text;
}
static void Main(string[] args)
{
Mat img1 = Cv2.ImRead("../../../images/macbookPro.jpg");
Mat img2 = Cv2.ImRead("../../../images/macbookPro2.jpg");
//Console.WriteLine($"ZBarQrCode1: {ZBarQrCode(img1)}");
Console.WriteLine($"ZBarQrCode2: {ZBarQrCode(img2)}");
Console.WriteLine($"ZxingQrCode1: {ZxingQrCode(img1)}");
Console.WriteLine($"ZxingQrCode2: {ZxingQrCode(img2)}");
Console.Read();
}
}
}
C++版本代码如下:
C++ windows下的环境有点麻烦。
参考博客:C++利用zxing识别二维码
【暂缺】
Python版本代码如下:
需要安装“pyzbar”包
pip install pyzbar -i https://mirror.baidu.com/pypi/simple
测试代码如下:
from pyzbar.pyzbar import decode
# from PIL import Image
import cv2
def main():
# fp = '../images/macbookPro.jpg'
fp = '../images/macbookPro2.jpg'
# image = Image.open(fp)
# image.show()
image = cv2.imread(fp)
barcodes = decode(image)
decoded = barcodes[0]
print(decoded)
#
url: bytes = decoded.data
url = url.decode()
print(url)
# rect
rect = decoded.rect
print(rect) # Rect(left=19, top=19, width=292, height=292)
# loop over the detected barcodes
for barcode in barcodes:
# extract the bounding box location of the barcode and draw the
# bounding box surrounding the barcode on the image
(x, y, w, h) = barcode.rect
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2)
# the barcode data is a bytes object so if we want to draw it on
# our output image we need to convert it to a string first
barcodeData = barcode.data.decode("utf-8")
barcodeType = barcode.type
# draw the barcode data and barcode type on the image
text = "{} ({})".format(barcodeData, barcodeType)
cv2.putText(image, text, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX,
0.5, (0, 0, 255), 2)
# print the barcode type and data to the terminal
print("[INFO] Found {} barcode: {}".format(barcodeType, barcodeData))
# show the output image
cv2.imshow("Image", image)
cv2.imwrite('macbook_qr_rect.jpg', image)
cv2.waitKey(0) # 按任意键退出
if __name__ == '__main__':
main()