开源的Tesseract和ZXing库实现OCR 和条码二维码识别
Rectangle oldRect = new Rectangle(x1 * bs, y1 * bs, (x2 - x1) * bs, (y2 - y1) * bs); //原图中要切除出一块小图,坐标为(x y)长L 高W
Rectangle newRect = new Rectangle(0, 0, (x2 - x1) * bs, (y2 - y1) * bs);//新图在画布的左上角坐标为(0 0)长L 高W
Bitmap newBmp = new Bitmap((x2 - x1) * bs, (y2 - y1) * bs);//放置新图的画布,照搬新图的大小
Graphics Q = Graphics.FromImage(newBmp);
Q.DrawImage(Bmp, newRect, oldRect, GraphicsUnit.Pixel);
pictureBox3.Width = (x2 - x1);
pictureBox3.Height = (y2 - y1);
pictureBox3.Image = newBmp;
if(OCR_cb.Checked == true)
{
TesseractProcessor process = new TesseractProcessor();
process.SetPageSegMode(ePageSegMode.PSM_SINGLE_LINE);
process.Init(System.Environment.CurrentDirectory "\\", "eng", (int)eOcrEngineMode.OEM_DEFAULT);
string result = process.Recognize(newBmp);
if (result == null) MessageBox.Show("读取失败");
else Result_tb.Text = result;
}
else if (Barcode_cb.Checked == true)
{
DecodingOptions decodeOption = new DecodingOptions();
decodeOption.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.All_1D };
//读取条形码
BarcodeReader br = new BarcodeReader();
br.Options = decodeOption;
Result result = br.Decode(newBmp as Bitmap);
if (result == null) MessageBox.Show("读取失败");
else Result_tb.Text = result.Text;
}
else
{
DecodingOptions decodeOption = new DecodingOptions();
decodeOption.PossibleFormats = new List<BarcodeFormat>() { BarcodeFormat.QR_CODE };
//读取二维码
ZXing.BarcodeReader br = new BarcodeReader();
br.Options = decodeOption;
ZXing.Result rs = br.Decode(newBmp as Bitmap);
if (rs == null) MessageBox.Show("读取失败");
else Result_tb.Text = rs.Text;
}
}
}
评论