using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Web; using ThoughtWorks.QRCode.Codec; using ThoughtWorks.QRCode.Codec.Data; namespace VRS.Util { /// /// 二维码工具 /// public static class QRCodeHelper { /// /// 读取图片文件,识别二维码 /// /// 图片文件路劲 /// 识别结果字符串 public static string CodeDecoder(string filePath) { string decoderStr; bool isGreen = false; try { if (!System.IO.File.Exists(filePath))//判断有没有需要读取的主文件夹,如果不存在,终止 return null; Bitmap bitMap = new Bitmap(Image.FromFile(filePath));//实例化位图对象,把文件实例化为带有颜色信息的位图对象 for (int i = bitMap.Height / 3, h = bitMap.Height; i < h; i++) { for (int j = 0, w = bitMap.Width; j < w; j++) { var color = bitMap.GetPixel(j, i); if (IsGreen(color)) { isGreen = true; break; } } if (isGreen) break; } QRCodeDecoder decoder = new QRCodeDecoder();//实例化QRCodeDecoder //通过.decoder方法把颜色信息转换成字符串信息 decoderStr = decoder.decode(new QRCodeBitmapImage(bitMap), System.Text.Encoding.UTF8); } catch (Exception ex) { return ex.Message; } if (!isGreen) return "warning"; else return decoderStr;//返回字符串信息 } public static bool IsGreen(Color color) { return (color.G > color.R * 5) && (color.G > color.B * 5); } } }