在NuGet程序包中查询添加以下内容(二维码和图片处理,可酌情安装):
OpenCvSharp4.Extensions
OpenCvSharp4.Windows
ThoughtWorks.QRCode.Standard
ZXing.Net
Spire.OCR
二维码生成相关方法:
/// <summary>
/// 生成二维码:根据传进去的数据 生成二维码
/// </summary>
/// <param name="data">用于生成二维码的数据</param>
/// <returns></returns>
public static Image GenerateQRCode(String data)
{
//创建编码器,设置编码格式。Byte格式的编码,只要能转成Byte的数据,都可以进行编码,比如中文。NUMERIC 只能编码数字。
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;
//大小,值越大生成的二维码图片像素越高
qrCodeEncoder.QRCodeScale = 5;
//版本,设置为0主要是防止编码的字符串太长时发生错误
qrCodeEncoder.QRCodeVersion = 0;
//生成二维码 Bitmap
qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.L;//错误效验、错误更正(有4个等级)
qrCodeEncoder.QRCodeBackgroundColor = Color.White;//背景色
qrCodeEncoder.QRCodeForegroundColor = Color.Black;//前景色
var pbImg = qrCodeEncoder.Encode(data, Encoding.UTF8);
return pbImg;
}
生成Image对象,之后可以转换为不同格式的图片保存
/// <summary>
/// Image图片格式转换(gif/jpg/png/bmp)
/// </summary>
/// <param name="img">Image对象</param>
/// <param name="filePath">保存路径图片名称</param>
/// <returns></returns>
public bool SavePng(Image img, string filePath)
{
try
{
using (var bmp = new Bitmap(img.Width, img.Height))
{
bmp.SetResolution(img.HorizontalResolution, img.VerticalResolution);
using (var g = Graphics.FromImage(bmp))
{
g.Clear(Color.White);
g.DrawImageUnscaled(img, 0, 0);
}
//存储各种格式
//bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Gif);
//bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png);
//bmp.Save(filePath, ImageFormat.Jpeg);
bmp.Save(filePath, ImageFormat.Jpeg);
}
return true;
}
catch(Exception ex)
{
return false;
}
}
二维码识别相关方法:
方法原理:先识别图片中的二维码,获取二维码的坐标,再根据坐标切割
//fileName为图片的路径
public bool CutAreaRQCode(string fileName)
{
Mat src = new Mat(fileName);
OpenCvSharp.Point2f[] points;
QRCodeDetector rqCode = new QRCodeDetector();
rqCode.Detect(src, out points);
Bitmap bm = new Bitmap(fileName);
int width = (int)(points[1].X - points[0].X);
int height = (int)(points[2].Y - points[0].Y);
Bitmap newBm = bm.Clone(new Rectangle((int)points[0].X, (int)points[0].Y, width, height), bm.PixelFormat);
newBm.Save(@"QRcodeImg\rqCode.png");
bool isHaveGreen = ImageToMatExistGreen(newBm);
var color1 = newBm.GetPixel(0, 0);
return isHaveGreen;
}
上述方法中包含了一个判断二维码是否绿码的方法ImageToMatExistGreen,存在一个问题就是黄码也会被识别为绿码,感兴趣的可以深入研究一下:
public bool ImageToMatExistGreen(Image cutImage)
{
Bitmap bm = new Bitmap(cutImage);
Mat srcImg = OpenCvSharp.Extensions.BitmapConverter.ToMat(bm);
Mat hsvImage = new Mat();
Cv2.CvtColor(srcImg, hsvImage, ColorConversionCodes.BGR2HSV);
Mat mask = new Mat();
//Canny算法--边缘检测
//Mat src = new Mat(@"QRcodeImg\text2.jpg", ImreadModes.Grayscale);
//Mat result = new Mat();
////Cv2.Blur(src, src, new OpenCvSharp.Size(3, 3));
//Cv2.Canny(src, result, 40, 120, 3);
//result.SaveImage(@"QRcodeImg\testCanny.png");
Cv2.InRange(hsvImage, new Scalar(35, 43, 46), new Scalar(77, 255, 255), mask);
bool green = false;
for (int r = 0; r < hsvImage.Rows; r++)
{
for (int c = 0; c < hsvImage.Cols; c++)
{
if (mask.At<byte>(r, c) == 255)
{
green = true;
}
}
}
return green;
}
识别图像中的文本(特别适合识别车牌等场景)
//读取图片文字信息
public void PicContainSensitiveText(string fileName)
{
try
{
OcrScanner scanner = new OcrScanner();
var res = scanner.Scan(fileName);
string content = scanner.Text.ToString();
catch (Exception ex)
{
}
}
干净又卫生呀兄弟们