コード例 #1
0
        private async Task <Result <OcrResult> > OcrPdf(byte[] pdf)
        {
            try
            {
                using (var input = new OcrInput())
                {
                    input.AddPdf(pdf);
                    input.DeNoise();
                    var ocrResult = await ocr.ReadAsync(input);

                    return(Result <OcrResult> .Ok(ocrResult));
                }
            } catch (Exception ex)
            {
                return(Result <OcrResult> .Failure(ex.ToString()));
            }
        }
コード例 #2
0
        private async void HandleOcrProcess(object sender, RoutedEventArgs e) // OCR 작업을 위한 이벤트 핸들러
        {
            // selectedImg에 소스가 없을 시 종료
            if (selectedImgPath == null)
            {
                MessageBox.Show("사진을 선택해주세요!", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            // 응용프로그램 제어 방지
            Mouse.OverrideCursor = Cursors.Wait;
            BlockGrid.Visibility = Visibility.Visible;
            StatusLabel.Content  = "사진-텍스트 변환중";

            var set = Properties.Settings.Default;

            try
            {
                var Ocr = new IronTesseract();
                if (koreanCheck.IsChecked == true)
                {
                    Ocr.Language = OcrLanguage.KoreanBest;
                }
                else if (englishCheck.IsChecked == true)
                {
                    Ocr.Language = OcrLanguage.EnglishBest;
                }
                else
                {
                    // 응용프로그램 오류 방지를 위한 재시작
                    MessageBox.Show("언어선택에서 오류가 발생했습니다. 다시 실행합니다...", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
                    System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);
                    Application.Current.Shutdown();
                }

                // OCR 작업 비동기 대기
                using var Input = new OcrInput(selectedImgPath);
                var Result = await Ocr.ReadAsync(Input);

                // OCR 결과 텍스트를 정규식을 통해 불필요한 문자 제거
                string resultString = Result.Text;
                resultString = Regex.Replace(resultString, @"[`@#$&*_{}[\]|\\;<>/]", "", RegexOptions.Singleline);
                resultString = Regex.Replace(resultString, "\\s+", " ");
                resultString = resultString.Trim();

                // 맞춤법 검사 시행
                StatusLabel.Content = "맞춤법 검사중";
                resultString        = await HandleSpellCheck(resultString);

                // 결과 텍스트 텍스트박스에 출력
                ocrResultTextBox.Text = resultString;

                // 텍스트 파일 저장 옵션 설정이 되어있을시 저장 시행
                if (set.IsTextSave)
                {
                    string filePath = null;
                    string fileName = Path.GetFileName(selectedImgPath);

                    // 사용자가 저장 경로를 지정했는지 확인 후 저장 경로 설정
                    if (set.IsSavePathCustomed)
                    {
                        filePath = set.SavePath + "/txt/";
                    }
                    else
                    {
                        filePath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/ITS/txt/";
                    }

                    // 파일 경로 및 파일 이름 설정
                    string fileFullName = $"{filePath}{fileName}.txt";

                    // 중복된 파일이 존재할시 덮어쓸지 메세지 박스를 통해 알림
                    if (File.Exists(fileFullName))
                    {
                        var dr = MessageBox.Show($"{fileFullName}이 이미 존재합니다. 덮어쓰시겠습니까?", "중복", MessageBoxButton.YesNo, MessageBoxImage.Question);
                        if (dr == MessageBoxResult.Yes)
                        {
                            // 기존 텍스트 파일 제거 저장 비동기 대기
                            File.Delete(fileFullName);
                            await File.WriteAllTextAsync(fileFullName, resultString.Replace(". ", ".\n")); // 가독성을 위한 ". " 이후 줄 변경
                        }
                        else
                        {
                            return;
                        }
                    }
                    else
                    {
                        // 텍스트 파일 저장 비동기 대기
                        await File.WriteAllTextAsync(fileFullName, resultString.Replace(". ", ".\n")); // 가독성을 위한 ". " 이후 줄 변경
                    }
                }
            }
            catch (Exception ex)
            {
                // 예외 처리
                Console.WriteLine(ex);
            }
            finally
            {
                // 제어 방지 해제
                BlockGrid.Visibility = Visibility.Hidden;
                Mouse.OverrideCursor = null;
            }
        }