private void OCRthread(Image imag, DateTime currentTime) { Image image = imag; if (image == null) { return; } if (image.Height < 15) { int newHeight = 15; double magnification = (double)newHeight / (double)image.Height; int newWidth = (int)(image.Width * magnification); Bitmap bitmap = new Bitmap(image, newWidth, newHeight); image = bitmap; } if (image.Width < 15) { int newWidth = 15; double magnification = (double)newWidth / (double)image.Width; int newHeight = (int)(image.Height * magnification); Bitmap bitmap = new Bitmap(image, newWidth, newHeight); image = bitmap; } if (image.Height > _config.Max_image_Height || image.Height < _config.Min_image_Height || image.Width > _config.Max_image_Width || image.Width < _config.Min_image_Width ) { label3.Text = @"超设置尺寸,本次不识别. W:" + image.Width.ToString() + @" H" + image.Height.ToString(); return; } else { label3.Text = @""; } label1.Text = currentTime.ToString("HH:mm:ss") + @" 从剪切板提取:"; while (ClipboardData.Rows.Count > 20) { ClipboardData.Rows[0].Delete(); } ClipboardData.Rows.Add("", currentTime.ToString("HH:mm:ss:fff"), "图片", "", ImageProcess.ImageToBytes(image)); Baidu_ocr baidu_Ocr = new Baidu_ocr(image, radioButton_Precision_True.Checked, currentTime); baidu_Ocr.API_KEY = _config.Baidu_ocr_API_KEY; baidu_Ocr.APP_ID = _config.Baidu_ocr_APP_ID; baidu_Ocr.SECRET_KEY = _config.Baidu_ocr_SECRET_KEY; baidu_Ocr.UpdateUIDelegate += UpdataUIStatus;//绑定更新任务状态的委托 //baidu_Ocr.OCR(pictureBox1.Image, radioButton2.Checked); Thread OCR_thread = new Thread(baidu_Ocr.ThreadOCR); OCR_thread.IsBackground = true; OCR_thread.Start(); }
private void pictureBox1_DoubleClick(object sender, EventArgs e) { OpenFileDialog openFile = new OpenFileDialog(); openFile.DefaultExt = "*.jpg;*.jpeg;"; openFile.Filter = "Image files (JPeg, Gif, Bmp, etc.)|*.jpg;*.jpeg;*.gif;*.bmp;*.tif; *.tiff; *.png|" + " JPeg files (*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF files (*.gif)|*.gif |BMP files (*.b" + "mp)|*.bmp|Tiff files (*.tif;*.tiff)|*.tif;*.tiff|Png files (*.png)| *.png |All f" + "iles (*.*)|*.*"; if (openFile.ShowDialog() == DialogResult.OK) { try { Image image = Image.FromFile(openFile.FileName); if (image.Height < 15) { int newHeight = 15; double magnification = (double)newHeight / (double)image.Height; int newWidth = (int)(image.Width * magnification); Bitmap bitmap = new Bitmap(image, newWidth, newHeight); image = bitmap; } if (image.Width < 15) { int newWidth = 15; double magnification = (double)newWidth / (double)image.Width; int newHeight = (int)(image.Height * magnification); Bitmap bitmap = new Bitmap(image, newWidth, newHeight); image = bitmap; } pictureBox1.Image = image; DateTime currentTime = DateTime.Now; label1.Text = currentTime.ToString("HH:mm:ss") + @" 手工打开图片:"; if (pictureBox1.Image.Height > _config.Max_image_Height || pictureBox1.Image.Height < _config.Min_image_Height || pictureBox1.Image.Width > _config.Max_image_Width || pictureBox1.Image.Width < _config.Min_image_Width ) { label3.Text = @"超设置尺寸,本次不识别. W:" + pictureBox1.Image.Width.ToString() + @" H" + pictureBox1.Image.Height.ToString(); return; } else { label3.Text = @""; } Baidu_ocr baidu_Ocr = new Baidu_ocr(pictureBox1.Image, radioButton_Precision_True.Checked); baidu_Ocr.API_KEY = _config.Baidu_ocr_API_KEY; baidu_Ocr.APP_ID = _config.Baidu_ocr_APP_ID; baidu_Ocr.SECRET_KEY = _config.Baidu_ocr_SECRET_KEY; baidu_Ocr.UpdateUIDelegate += UpdataUIStatus;//绑定更新任务状态的委托 //baidu_Ocr.OCR(pictureBox1.Image, radioButton2.Checked); Thread OCR_thread = new Thread(baidu_Ocr.ThreadOCR) { IsBackground = true }; OCR_thread.Start(); } catch (Exception e1) { //MessageBox.Show(e1.ToString()); Logging.LogUsefulException(e1); } } }
private void ClipboardImageOCR_DragDrop(object sender, DragEventArgs e) { string[] files = (string[])e.Data.GetData(DataFormats.FileDrop); if (files.Length == 1) { string Extension = Path.GetExtension(files[0]).ToLower(); /* * "Image files (JPeg, Gif, Bmp, etc.)|*.jpg;*.jpeg;*.gif;*.bmp;*.tif; *.tiff; *.png|" * " JPeg files (*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF files (*.gif)|*.gif |BMP files (*.b" * "mp)|*.bmp|Tiff files (*.tif;*.tiff)|*.tif;*.tiff|Png files (*.png)| *.png |All f" * "iles (*.*)|*.*"; * )*/ string[] temArr = { ".jpg", ".jpeg", ".bmp", ".png" }; List <string> Image_files_Extension = new List <string>(temArr); if (Image_files_Extension.Contains(Extension)) { try { Image image = Image.FromFile(files[0]); if (image.Height < 15) { int newHeight = 15; double magnification = (double)newHeight / (double)image.Height; int newWidth = (int)(image.Width * magnification); Bitmap bitmap = new Bitmap(image, newWidth, newHeight); image = bitmap; } if (image.Width < 15) { int newWidth = 15; double magnification = (double)newWidth / (double)image.Width; int newHeight = (int)(image.Height * magnification); Bitmap bitmap = new Bitmap(image, newWidth, newHeight); image = bitmap; } pictureBox1.Image = image; DateTime currentTime = DateTime.Now; label1.Text = currentTime.ToString("HH:mm:ss") + @" 拖拽图片:"; if (pictureBox1.Image.Height > _config.Max_image_Height || pictureBox1.Image.Height < _config.Min_image_Height || pictureBox1.Image.Width > _config.Max_image_Width || pictureBox1.Image.Width < _config.Min_image_Width ) { label3.Text = @"超设置尺寸,本次不识别. W:" + pictureBox1.Image.Width.ToString() + @" H" + pictureBox1.Image.Height.ToString(); return; } else { label3.Text = @""; } Baidu_ocr baidu_Ocr = new Baidu_ocr(pictureBox1.Image, radioButton_Precision_True.Checked); baidu_Ocr.API_KEY = _config.Baidu_ocr_API_KEY; baidu_Ocr.APP_ID = _config.Baidu_ocr_APP_ID; baidu_Ocr.SECRET_KEY = _config.Baidu_ocr_SECRET_KEY; baidu_Ocr.UpdateUIDelegate += UpdataUIStatus; //绑定更新任务状态的委托 //baidu_Ocr.OCR(pictureBox1.Image, radioButton2.Checked); Thread OCR_thread = new Thread(baidu_Ocr.ThreadOCR) { IsBackground = true }; OCR_thread.Start(); } catch (Exception e1) { //MessageBox.Show(e1.ToString()); Logging.LogUsefulException(e1); } } } }
private void button2_Click(object sender, EventArgs e) { IDataObject iData = new DataObject(); iData = Clipboard.GetDataObject(); //显示剪贴板中的图片信息 if (iData.GetDataPresent(DataFormats.Bitmap)) { Image image = Clipboard.GetImage(); if (image.Height < 15) { int newHeight = 15; double magnification = (double)newHeight / (double)image.Height; int newWidth = (int)(image.Width * magnification); Bitmap bitmap = new Bitmap(image, newWidth, newHeight); image = bitmap; } if (image.Width < 15) { int newWidth = 15; double magnification = (double)newWidth / (double)image.Width; int newHeight = (int)(image.Height * magnification); Bitmap bitmap = new Bitmap(image, newWidth, newHeight); image = bitmap; } pictureBox1.Image = image; pictureBox1.Update(); } if (pictureBox1.Image == null) { MessageBox.Show("没有需要OCR识别的图片"); return; } try { DateTime currentTime = DateTime.Now; if (pictureBox1.Image.Height > _config.Max_image_Height || pictureBox1.Image.Height < _config.Min_image_Height || pictureBox1.Image.Width > _config.Max_image_Width || pictureBox1.Image.Width < _config.Min_image_Width ) { label3.Text = @"超设置尺寸,本次不识别. W:" + pictureBox1.Image.Width.ToString() + @" H" + pictureBox1.Image.Height.ToString(); return; } else { label3.Text = @""; } label1.Text = currentTime.ToString("HH:mm:ss") + "再次识别的图片:"; Baidu_ocr baidu_Ocr = new Baidu_ocr(pictureBox1.Image, radioButton_Precision_True.Checked); baidu_Ocr.API_KEY = _config.Baidu_ocr_API_KEY; baidu_Ocr.APP_ID = _config.Baidu_ocr_APP_ID; baidu_Ocr.SECRET_KEY = _config.Baidu_ocr_SECRET_KEY; baidu_Ocr.UpdateUIDelegate += UpdataUIStatus;//绑定更新任务状态的委托 Thread OCR_thread = new Thread(baidu_Ocr.ThreadOCR) { IsBackground = true }; OCR_thread.Start(); } catch (Exception e1) { //MessageBox.Show(e1.ToString()); Logging.LogUsefulException(e1); } }
/// <summary> /// 处理剪切板数据 /// </summary> private void DisplayClipboardData() { try { if (checkBox1.Checked == false) { return; } //显示剪贴板中的图片信息 if (Clipboard.ContainsImage()) // if (iData.GetDataPresent(DataFormats.Bitmap)) { Image image = Clipboard.GetImage(); if (image.Height < 15) { int newHeight = 15; double magnification = (double)newHeight / (double)image.Height; int newWidth = (int)(image.Width * magnification); Bitmap bitmap = new Bitmap(image, newWidth, newHeight); image = bitmap; } if (image.Width < 15) { int newWidth = 15; double magnification = (double)newWidth / (double)image.Width; int newHeight = (int)(image.Height * magnification); Bitmap bitmap = new Bitmap(image, newWidth, newHeight); image = bitmap; } pictureBox1.Image = image; pictureBox1.Update(); DateTime currentTime = DateTime.Now; if (pictureBox1.Image.Height > _config.Max_image_Height || pictureBox1.Image.Height < _config.Min_image_Height || pictureBox1.Image.Width > _config.Max_image_Width || pictureBox1.Image.Width < _config.Min_image_Width ) { label3.Text = @"超设置尺寸,本次不识别. W:" + pictureBox1.Image.Width.ToString() + @" H" + pictureBox1.Image.Height.ToString(); return; } else { label3.Text = @""; } label1.Text = currentTime.ToString("HH:mm:ss") + @" 从剪切板提取:"; Baidu_ocr baidu_Ocr = new Baidu_ocr(pictureBox1.Image, radioButton_Precision_True.Checked); baidu_Ocr.API_KEY = _config.Baidu_ocr_API_KEY; baidu_Ocr.APP_ID = _config.Baidu_ocr_APP_ID; baidu_Ocr.SECRET_KEY = _config.Baidu_ocr_SECRET_KEY; baidu_Ocr.UpdateUIDelegate += UpdataUIStatus;//绑定更新任务状态的委托 //baidu_Ocr.OCR(pictureBox1.Image, radioButton2.Checked); Thread OCR_thread = new Thread(baidu_Ocr.ThreadOCR) { IsBackground = true }; OCR_thread.Start(); } } catch (Exception e) { //MessageBox.Show(e.ToString()); Logging.LogUsefulException(e); } }