68 lines
2.1 KiB
C#
68 lines
2.1 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// 二维码工具
|
|
/// </summary>
|
|
public static class QRCodeHelper
|
|
{
|
|
/// <summary>
|
|
/// 读取图片文件,识别二维码
|
|
/// </summary>
|
|
/// <param name="filePath">图片文件路劲</param>
|
|
/// <returns>识别结果字符串</returns>
|
|
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);
|
|
}
|
|
}
|
|
} |