当前位置:   article > 正文

c# opencv二维码识别_c#opencv二维码

c#opencv二维码

1、测试界面

2、核心代码

  1. using OpenCvSharp;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Windows.Forms;
  11. namespace TestProject
  12. {
  13. public partial class Form1 : DevExpress.XtraEditors.XtraForm
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. Thread bath_th = null;
  20. Mat frame = null;
  21. private void simpleButton1_Click(object sender, EventArgs e)
  22. {
  23. try
  24. {
  25. //videocapture后面参数为0表示获取电脑本机摄像头的内容
  26. //如果输入rtsp地址就是获取网络摄像头的拍摄内容,rtsp地址可由摄像头厂家提供
  27. //已大华为例 rtsp://用户名:密码@ip:port
  28. if (frame != null)
  29. {
  30. MessageBox.Show("正在采集");
  31. return;
  32. }
  33. frame =new Mat();
  34. var capture = new VideoCapture(0);
  35. int sleeptime = (int)Math.Round(1000 / capture.Fps);
  36. bath_th = new Thread(
  37. new ThreadStart(delegate
  38. {
  39. while (true)
  40. {
  41. try
  42. {
  43. capture.Read(frame);
  44. if (frame.Empty())
  45. {
  46. break;
  47. }
  48. Bitmap image = OpenCvSharp.Extensions.BitmapConverter.ToBitmap(frame);
  49. this.Invoke((EventHandler)delegate
  50. {
  51. if (pictureEdit1.Image != null)
  52. {
  53. pictureEdit1.Image.Dispose();
  54. }
  55. pictureEdit1.Image = image;
  56. });
  57. Cv2.WaitKey(sleeptime);
  58. }
  59. catch { }
  60. }
  61. }));
  62. bath_th.Start();
  63. }
  64. catch (Exception ex)
  65. {
  66. Console.WriteLine(ex.Message);
  67. }
  68. }
  69. private void simpleButton2_Click(object sender, EventArgs e)
  70. {
  71. this.simpleButton2.Enabled = false;
  72. String qr_code = DetectQRCode(frame);
  73. if (!qr_code.Equals(string.Empty))
  74. {
  75. this.Invoke((EventHandler)delegate {
  76. this.textEdit1.Text = qr_code;
  77. });
  78. //MessageBox.Show(qr_code);
  79. }
  80. Thread.Sleep(1000);
  81. this.simpleButton2.Enabled = true;
  82. }
  83. public string DetectQRCode(Mat src)
  84. {
  85. //Mat src = Cv2.ImRead(@"D:\Project\禾川PLC\code.png");
  86. //图像灰度
  87. Mat gray = new Mat();
  88. Cv2.CvtColor(src, gray, ColorConversionCodes.BGRA2GRAY);
  89. //Cv2.ImShow("gray", gray);
  90. //二值化
  91. Mat threshold = new Mat();
  92. //Cv2.AdaptiveThreshold(gray, threshold, 255.0, AdaptiveThresholdTypes.MeanC, ThresholdTypes.Binary, 13, 2);
  93. Cv2.Threshold(gray, threshold, 0, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);
  94. //Cv2.ImShow("threshold", threshold);
  95. //绘制轮廓
  96. //截取二维码有效区域
  97. //识别二维码
  98. QRCodeDetector qRCodeDetector = new QRCodeDetector();
  99. Point2f[] point2Fs;
  100. //Cv2.ImShow("gray", gray);
  101. Mat mat = new Mat();
  102. string code = qRCodeDetector.DetectAndDecode(src, out point2Fs, mat);
  103. //Point[][] contours;
  104. //HierarchyIndex[] hierarchies;
  105. //Cv2.FindContours(src, out contours, out hierarchies, RetrievalModes.Tree, ContourApproximationModes.ApproxSimple);
  106. //RNG rng = new RNG(12345);
  107. //for (int i = 0; i < point2Fs.Length; i++)
  108. //{
  109. // Cv2.DrawContours(src, point2Fs, i, new Scalar(rng.Uniform(0, 255), rng.Uniform(0, 255), rng.Uniform(0, 255)), 6, LineTypes.Link4);
  110. //}
  111. // Cv2.Rectangle(src, new OpenCvSharp.Point((int)point2Fs[3].X, (int)point2Fs[1].Y), new OpenCvSharp.Point((int)point2Fs[2].X, (int)point2Fs[2].Y), new Scalar(0, 255, 255), 2);
  112. // Cv2.ImShow("src", src);
  113. return code;
  114. }
  115. private void simpleButton3_Click(object sender, EventArgs e)
  116. {
  117. this.textEdit1.Text = "";
  118. }
  119. }
  120. }

 作者V: jbossjf

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/883774
推荐阅读
相关标签
  

闽ICP备14008679号