using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Google.Cloud.Vision.V1;
namespace TestProject
{
/// <summary>
/// 메인 폼
/// </summary>
public partial class MainForm : Form
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 이미지 주석자 클라이언트
/// </summary>
private ImageAnnotatorClient client;
/// <summary>
/// 파일 열기 대화 상자
/// </summary>
private OpenFileDialog openFileDialog;
/// <summary>
/// 백그라운드 작업자
/// </summary>
private BackgroundWorker backgroundWorker;
/// <summary>
/// 문자열 빌더
/// </summary>
private StringBuilder stringBuilder = null;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainForm()
/// <summary>
/// 생성자
/// </summary>
public MainForm()
{
InitializeComponent();
#region 이미지 주석자 클라이언트를 설정한다.
this.client = ImageAnnotatorClient.Create();
#endregion
#region 파일 열기 대화 상자를 설정한다.
this.openFileDialog = new OpenFileDialog();
this.openFileDialog.Filter = "이미지 파일(*.png)|*.png";
this.openFileDialog.FilterIndex = 1;
this.openFileDialog.DefaultExt = ".png";
this.openFileDialog.Multiselect = true;
#endregion
#region 백그라운드 작업자를 설정한다.
this.backgroundWorker = new BackgroundWorker();
this.backgroundWorker.WorkerReportsProgress = true;
#endregion
#region 결과 텍스트 박스를 설정한다.
this.resultTextBox.ReadOnly = true;
#endregion
#region 이벤트를 설정한다.
this.imageButton.Click += imageButton_Click;
this.backgroundWorker.DoWork += backgroundWorker_DoWork;
this.backgroundWorker.ProgressChanged += backgroundWorker_ProgressChanged;
this.backgroundWorker.RunWorkerCompleted += backgroundWorker_RunWorkerCompleted;
#endregion
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 이미지 버튼 클릭시 처리하기 - imageButton_Click(sender, e)
/// <summary>
/// 이미지 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void imageButton_Click(object sender, EventArgs e)
{
if(this.openFileDialog.ShowDialog() != DialogResult.OK)
{
return;
}
if(this.openFileDialog.FileNames == null || this.openFileDialog.FileNames.Length == 0)
{
return;
}
this.imageButton.Enabled = false;
List<string> filePathList = this.openFileDialog.FileNames.OrderBy(x => x).ToList();
this.backgroundWorker.RunWorkerAsync(filePathList);
}
#endregion
#region 백그라운드 작업자 작업 처리하기 - backgroundWorker_DoWork(sender, e)
/// <summary>
/// 백그라운드 작업자 작업 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
List<string> filePathList = e.Argument as List<string>;
if(this.stringBuilder != null)
{
this.stringBuilder.Clear();
this.stringBuilder = null;
}
this.stringBuilder = new StringBuilder();
for(int i = 0; i < filePathList.Count; i++)
{
string filePath = filePathList[i];
this.backgroundWorker.ReportProgress(i, filePathList.Count);
this.stringBuilder.AppendLine(filePath);
this.stringBuilder.AppendLine("--------------------------------------------------");
try
{
Image image = Image.FromFile(filePath);
var response = client.DetectText(image);
string text = response.First()?.Description;
this.stringBuilder.AppendLine(text);
this.backgroundWorker.ReportProgress(i + 1, filePathList.Count);
}
catch
{
continue;
}
finally
{
this.stringBuilder.AppendLine("--------------------------------------------------");
this.stringBuilder.AppendLine();
this.stringBuilder.AppendLine();
}
}
}
#endregion
#region 백그라운드 작업자 진행 변경시 처리하기 - backgroundWorker_ProgressChanged(sender, e)
/// <summary>
/// 백그라운드 작업자 진행 변경시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
int index = e.ProgressPercentage;
int count = (int)e.UserState;
this.imageButton.Text = $"{index}/{count}";
this.imageButton.Update();
}
#endregion
#region 백그라운드 작업자 실행 완료시 처리하기 - backgroundWorker_RunWorkerCompleted(sender, e)
/// <summary>
/// 백그라운드 작업자 실행 완료시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.resultTextBox.Text = this.stringBuilder?.ToString();
this.imageButton.Text = "이미지";
this.imageButton.Enabled = true;
}
#endregion
}
}