/// <summary> /// ORC自动分析图片 /// </summary> /// <param name="filePath"></param> private List <UnChekedWordInfo> AutoExcutePicOCR(string filePath, Microsoft.Office.Interop.Excel.Shape shape) { List <UnChekedWordInfo> listResult = new List <UnChekedWordInfo>(); try { try { APIService service = new APIService(); var userStateInfos = service.GetUserStateByToken(); if (!userStateInfos) { try { CommonExchangeInfo commonExchangeInfo = new CommonExchangeInfo(); commonExchangeInfo.Code = "ShowNotifyMessageView"; commonExchangeInfo.Data = "500"; string jsonData = JsonConvert.SerializeObject(commonExchangeInfo); //序列化 Win32Helper.SendMessage("WordAndImgOperationApp", jsonData); } catch { } return(null); } } catch { return(null); } countWhile = 0; isInitCompleted = false; Dispatcher.Invoke(new System.Action(() => { //清除框选 TextOverlay.Children.Clear(); //生成绑定图片 bitmap = Util.GetBitmapImage(filePath); img.Width = bitmap.PixelWidth; img.Height = bitmap.PixelHeight; img.Source = bitmap; })); ImgGeneralInfo resultImgGeneral = null; try { var image = File.ReadAllBytes(filePath); //集成云处理OCR APIService service = new APIService(); var result = service.GetOCRResultByToken(image); //反序列化 resultImgGeneral = JsonConvert.DeserializeObject <ImgGeneralInfo>(result.ToString().Replace("char", "Char")); ////////var options = new Dictionary<string, object>{ //////// {"recognize_granularity", "small"}, //////// {"vertexes_location", "true"} //////// }; ////////string apiName = ""; ////////try ////////{ //////// apiName = ConfigurationManager.AppSettings["CallAPIName"].ToString(); ////////} ////////catch (Exception ex) ////////{ } ////////DESHelper dESHelper = new DESHelper(); ////////OCR clientOCR = new OCR(dESHelper.DecryptString(ConfigurationManager.AppSettings["APIKey"].ToString()), dESHelper.DecryptString(ConfigurationManager.AppSettings["SecretKey"].ToString())); ////////var result = clientOCR.Accurate(apiName, image, options); //////////反序列化 ////////resultImgGeneral = JsonConvert.DeserializeObject<ImgGeneralInfo>(result.ToString().Replace("char", "Char")); } catch (Exception ex) { CheckWordUtil.Log.TextLog.SaveError(ex.Message); } while (!isInitCompleted && countWhile < 10) { System.Threading.Thread.Sleep(100); countWhile++; } if (resultImgGeneral != null && resultImgGeneral.words_result_num > 0) { string desiredFolderName = CheckWordTempPath + " \\" + Guid.NewGuid().ToString() + "\\"; if (!Directory.Exists(desiredFolderName)) { Directory.CreateDirectory(desiredFolderName); } List <WordInfo> listUnValidInfos = new List <WordInfo>(); foreach (var item in resultImgGeneral.words_result) { string lineWord = ""; List <Rect> rects = new List <Rect>(); foreach (var charInfo in item.Chars) { lineWord += charInfo.Char; rects.Add(new Rect() { X = charInfo.location.left * xScale, Y = charInfo.location.top * yScale, Width = charInfo.location.width * xScale, Height = charInfo.location.height * yScale }); } var listUnChekedWordInfo = CheckWordUtil.CheckWordHelper.GetUnChekedWordInfoList(lineWord); foreach (var itemInfo in listUnChekedWordInfo) { listUnValidInfos.Add(new WordInfo() { UnValidText = itemInfo.Name, AllText = lineWord, Rects = rects }); MatchCollection mc = Regex.Matches(lineWord, itemInfo.Name, RegexOptions.IgnoreCase); if (mc.Count > 0) { foreach (Match m in mc) { var infoResult = listResult.FirstOrDefault(x => x.Name == itemInfo.Name); if (infoResult == null) { itemInfo.UnChekedWordInLineDetailInfos.Add(new UnChekedInLineDetailWordInfo() { TypeTextFrom = "Img", UnCheckWordExcelRangeShape = shape, InLineText = lineWord, ImgResultPath = desiredFolderName + System.IO.Path.GetFileName(filePath) }); itemInfo.ErrorTotalCount++; listResult.Add(itemInfo); } else { infoResult.UnChekedWordInLineDetailInfos.Add(new UnChekedInLineDetailWordInfo() { TypeTextFrom = "Img", UnCheckWordExcelRangeShape = shape, InLineText = lineWord, ImgResultPath = desiredFolderName + System.IO.Path.GetFileName(filePath) }); infoResult.ErrorTotalCount++; } } } } } var list = CheckWordHelper.GetUnValidRects(listUnValidInfos); foreach (var item in list) { try { Dispatcher.Invoke(new System.Action(() => { WordOverlay wordBoxOverlay = new WordOverlay(item); var overlay = new System.Windows.Controls.Border() { Style = (System.Windows.Style) this.Resources["HighlightedWordBoxHorizontalLine"] }; overlay.SetBinding(System.Windows.Controls.Border.MarginProperty, wordBoxOverlay.CreateWordPositionBinding()); overlay.SetBinding(System.Windows.Controls.Border.WidthProperty, wordBoxOverlay.CreateWordWidthBinding()); overlay.SetBinding(System.Windows.Controls.Border.HeightProperty, wordBoxOverlay.CreateWordHeightBinding()); TextOverlay.Children.Add(overlay); })); } catch (Exception ex) { } } if (listUnValidInfos.Count > 0) { System.Threading.Thread.Sleep(50); SavePic(desiredFolderName + System.IO.Path.GetFileName(filePath)); } } } catch (Exception ex) { } return(listResult); }
private void img_SizeChanged(object sender, SizeChangedEventArgs e) { xScale = img.ActualWidth / bitmap.PixelWidth; yScale = img.ActualHeight / bitmap.PixelHeight; try { if (myFolder.CheckResultInfo == "1") { Dispatcher.Invoke(new Action(() => { TextOverlay.Children.Clear(); })); if (myFolder.ResultImgGeneral != null && myFolder.ResultImgGeneral.words_result_num > 0) { List <WordInfo> listUnValidInfos = new List <WordInfo>(); foreach (var item in myFolder.ResultImgGeneral.words_result) { string lineWord = ""; List <Rect> rects = new List <Rect>(); foreach (var charInfo in item.Chars) { lineWord += charInfo.Char; rects.Add(new Rect() { X = charInfo.location.left * xScale, Y = charInfo.location.top * yScale, Width = charInfo.location.width * xScale, Height = charInfo.location.height * yScale }); } var listUnChekedWordInfo = CheckWordUtil.CheckWordHelper.GetUnChekedWordInfoList(lineWord); foreach (var itemInfo in listUnChekedWordInfo) { listUnValidInfos.Add(new WordInfo() { UnValidText = itemInfo.Name, AllText = lineWord, Rects = rects }); } } var list = CheckWordUtil.CheckWordHelper.GetUnValidRects(listUnValidInfos); foreach (var item in list) { try { Dispatcher.Invoke(new Action(() => { WordOverlay wordBoxOverlay = new WordOverlay(item); var overlay = new Border() { Style = (System.Windows.Style) this.Resources["HighlightedWordBoxHorizontalLine"] }; overlay.SetBinding(Border.MarginProperty, wordBoxOverlay.CreateWordPositionBinding()); overlay.SetBinding(Border.WidthProperty, wordBoxOverlay.CreateWordWidthBinding()); overlay.SetBinding(Border.HeightProperty, wordBoxOverlay.CreateWordHeightBinding()); TextOverlay.Children.Add(overlay); })); } catch (Exception ex) { } } } } } catch (Exception ex) { } }