溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

C#如何實(shí)現(xiàn)二維碼圖片識(shí)別

發(fā)布時(shí)間:2021-09-03 15:14:14 來源:億速云 閱讀:688 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關(guān)C#如何實(shí)現(xiàn)二維碼圖片識(shí)別的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。

具體內(nèi)容如下

怎么用NuGet和怎么配置log4net就不介紹了,直接上代碼(Visual Studio 2015 下的項(xiàng)目,用的.NET Framework 4.5.2)。

其中QRDecodeConsoleApp.exe.config文件里配置圖片路勁(默認(rèn)為D:\我的文檔\Pictures\二維碼)、圖片類型(默認(rèn)為*.png)。

也支持在命令行里執(zhí)行,exe后接圖片路勁參數(shù)。

需要直接用的朋友,確認(rèn)完QRDecodeDemo\bin\Debug下的配置文件QRDecodeConsoleApp.exe.config后,運(yùn)行QRDecodeConsoleApp.exe即可(運(yùn)行環(huán)境上文已附鏈接)。

后續(xù)更新一個(gè)批量生成二維碼圖片的工具,網(wǎng)上除了在線生成的,下載下來的工具都不怎么好用。

using System;
using System.IO;
using System.Drawing;
using System.Configuration;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using log4net;

namespace QRDecodeConsoleApp
{
 class Program
 {
 /// <summary>
 /// 私有日志對(duì)象
 /// </summary>
 private static readonly ILog logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

 /// <summary>
 /// 識(shí)別指定目錄下的全部二維碼圖片(默認(rèn)是PNG)
 /// </summary>
 /// <param name="args"></param>
 static void Main(string[] args)
 {
  try
  {
  string[] files;
  if (args.Length > 0)
  {
   //args[0]為CMD里exe后的第一個(gè)參數(shù) ImgType默認(rèn)配置的*.png
   files = Directory.GetFiles(args[0], ConfigurationManager.AppSettings["ImgType"]);
  }
  else
  {
   //讀取指定路勁(QRDecodeConsoleApp.exe.config里配置的路勁)
   files = Directory.GetFiles(ConfigurationManager.AppSettings["QRImgPath"],
      ConfigurationManager.AppSettings["ImgType"]);
  }

  //存放結(jié)果的文件
  string filePath = "txtResult" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".config";

  //一個(gè)個(gè)讀取并追加到記錄文件
  for (int i = 0; i < files.Length; i++)
  {
   File.AppendAllText(filePath, CodeDecoder(files[i]) + "\t" + files[i] + "\n");//追加到文件里記錄
   logger.Info("第" + i + "個(gè)識(shí)別成功");
   Console.WriteLine("第" + i + "個(gè)識(shí)別成功");
  }
  Console.WriteLine("識(shí)別完成,按任意鍵退出");
  Console.ReadLine();
  }
  catch (Exception ex)
  {
  Console.WriteLine("識(shí)別出錯(cuò):" + ex.Message);
  logger.Error("識(shí)別出錯(cuò)");
  logger.Error("異常描述:\t" + ex.Message);
  logger.Error("異常方法:\t" + ex.TargetSite);
  logger.Error("異常堆棧:\t" + ex.StackTrace);
  Console.ReadLine();
  }

 }

 /// <summary>
 /// 讀取圖片文件,識(shí)別二維碼
 /// </summary>
 /// <param name="filePath">圖片文件路勁</param>
 /// <returns>識(shí)別結(jié)果字符串</returns>
 public static string CodeDecoder(string filePath)
 {
  string decoderStr;
  try
  {
  if (!System.IO.File.Exists(filePath))//判斷有沒有需要讀取的主文件夾,如果不存在,終止 
   return null;

  Bitmap bitMap = new Bitmap(Image.FromFile(filePath));//實(shí)例化位圖對(duì)象,把文件實(shí)例化為帶有顏色信息的位圖對(duì)象 
  QRCodeDecoder decoder = new QRCodeDecoder();//實(shí)例化QRCodeDecoder 

  //通過.decoder方法把顏色信息轉(zhuǎn)換成字符串信息 
  decoderStr = decoder.decode(new QRCodeBitmapImage(bitMap), System.Text.Encoding.UTF8);
  }
  catch (Exception ex)
  {
  throw ex;
  }

  return decoderStr;//返回字符串信息 
 }


 }
}

感謝各位的閱讀!關(guān)于“C#如何實(shí)現(xiàn)二維碼圖片識(shí)別”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI