Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Abbyy.CloudSdk.Demo.Core/Abbyy.CloudSdk.Demo.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<Compile Include="Extensions\ExportFormatExtensions.cs" />
<Compile Include="FieldLevelXmlReader.cs" />
<Compile Include="IScope.cs" />
<Compile Include="Models\FieldRegion.cs" />
<Compile Include="Models\Mode.cs" />
<Compile Include="Extensions\ModeExtensions.cs" />
<Compile Include="Models\Options.cs" />
Expand Down
38 changes: 38 additions & 0 deletions Abbyy.CloudSdk.Demo.Core/Models/FieldRegion.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright © 2019 ABBYY Production LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Abbyy.CloudSdk.Demo.Core.Models
{
public class FieldRegion
{
public FieldRegion(int left, int top, int right, int bottom)
{
_left = left;
_top = top;
_right = right;
_bottom = bottom;
}

public string Format()
{
string formatted = $"{_left},{_top},{_right},{_bottom}";
return formatted;
}

private readonly int _left;
private readonly int _top;
private readonly int _right;
private readonly int _bottom;
}
}
2 changes: 2 additions & 0 deletions Abbyy.CloudSdk.Demo.Core/Models/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public sealed class Options

public string XmlSettingsPath { get; set; }

public FieldRegion Region { get; set; }

public string FileName => Path.GetFileNameWithoutExtension(SourcePath);
}
}
16 changes: 12 additions & 4 deletions Abbyy.CloudSdk.Demo.Core/ProcessingParamsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,28 @@ public static FieldsProcessingParams GetFieldsProcessingParams(Guid taskId)

public static TextFieldProcessingParams GetTextFieldProcessingParams(Options options)
{
return new TextFieldProcessingParams { Language = options.Language };
return new TextFieldProcessingParams
{
Language = options.Language,
Region = options.Region?.Format(),
};
}

public static CheckmarkFieldProcessingParams GetCheckmarkFieldProcessingParams()
public static CheckmarkFieldProcessingParams GetCheckmarkFieldProcessingParams(Options options)
{
return new CheckmarkFieldProcessingParams
{
CheckmarkType = CheckmarkType.Square,
Region = options.Region?.Format(),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Может ему дописать property?
public string RegionFormat { get { return value.Region?.Format(); }}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Скорее это должен быть метод-расширение во вспомогательном классе.

};
}

public static BarcodeFieldProcessingParams GetBarcodeFieldProcessingParams()
public static BarcodeFieldProcessingParams GetBarcodeFieldProcessingParams(Options options)
{
return new BarcodeFieldProcessingParams();
return new BarcodeFieldProcessingParams
{
Region = options.Region?.Format(),
};
}

public static BusinessCardProcessingParams GetBusinessCardProcessingParams(Options options)
Expand Down
4 changes: 2 additions & 2 deletions Abbyy.CloudSdk.Demo.Core/Processor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,10 +229,10 @@ private Task<TaskInfo> StartFieldProcessingAsync(
var textParams = ProcessingParamsBuilder.GetTextFieldProcessingParams(options);
return _ocrClient.ProcessTextFieldAsync(textParams, fileStream, options.FileName);
case Mode.BarcodeField:
var barcodeParams = ProcessingParamsBuilder.GetBarcodeFieldProcessingParams();
var barcodeParams = ProcessingParamsBuilder.GetBarcodeFieldProcessingParams(options);
return _ocrClient.ProcessBarcodeFieldAsync(barcodeParams, fileStream, options.FileName);
case Mode.CheckmarkField:
var checkmarkParams = ProcessingParamsBuilder.GetCheckmarkFieldProcessingParams();
var checkmarkParams = ProcessingParamsBuilder.GetCheckmarkFieldProcessingParams(options);
return _ocrClient.ProcessCheckmarkFieldAsync(checkmarkParams, fileStream, options.FileName);
default:
throw new InvalidOperationException("Wrong operation");
Expand Down
29 changes: 11 additions & 18 deletions Abbyy.CloudSdk.Demo.WindowsApp/ImageControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Abbyy.CloudSdk.Sample.WindowsApp;

namespace Abbyy.CloudSdk.Demo.WindowsApp
{
public partial class ImageControl : UserControl
{
public event EventHandler<RegionSelectedEventArgs> RegionSelected;

private string _sourceFile;
private string _sourceFilePath;
private Point _imageCaptureStart;

public ImageControl()
Expand All @@ -40,7 +39,15 @@ public string Source
set
{
image.Source = new BitmapImage(new Uri(value));
_sourceFile = value;
_sourceFilePath = value;
}
}

public string SourceFilePath
{
get
{
return _sourceFilePath;
}
}

Expand Down Expand Up @@ -77,21 +84,7 @@ private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
var newX = (visualLeft - imageOffset.X) * scaleX;
var newY = (visualTop - imageOffset.Y) * scaleY;

var src = (System.Drawing.Bitmap)System.Drawing.Image.FromFile(_sourceFile);
var target = new System.Drawing.Bitmap((int)newWidth, (int)newHeight);
target.SetResolution(src.HorizontalResolution, src.VerticalResolution);

using (var g = System.Drawing.Graphics.FromImage(target))
{
var rect = new System.Drawing.Rectangle((int)newX, (int)newY, (int)newWidth, (int)newHeight);
g.DrawImage(
src,
new System.Drawing.Rectangle(0, 0, target.Width, target.Height),
rect,
System.Drawing.GraphicsUnit.Pixel);
}

var ev = new RegionSelectedEventArgs(new Rect(newX, newY, newWidth, newHeight), target);
var ev = new RegionSelectedEventArgs(new Rect(newX, newY, newWidth, newHeight));
OnRegionSelected(ev);

selectBox.Visibility = Visibility.Hidden;
Expand Down
31 changes: 14 additions & 17 deletions Abbyy.CloudSdk.Demo.WindowsApp/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
using Abbyy.CloudSdk.V2.Client;
using Abbyy.CloudSdk.V2.Client.Models;
using Application = System.Windows.Application;
using Image = System.Drawing.Image;

namespace Abbyy.CloudSdk.Demo.WindowsApp
{
Expand Down Expand Up @@ -123,16 +122,20 @@ private string[] GetDropFiles(IDataObject data)

private async void FieldSelected(object sender, RegionSelectedEventArgs e)
{
var tempFilePath = Path.GetTempFileName();
e.CroppedImage.Save(tempFilePath, System.Drawing.Imaging.ImageFormat.Tiff);

await AddFileTaskAsync(tempFilePath, e.CroppedImage);
var imagePath = fieldLevelImage.SourceFilePath;
var rectangle = e.SelectedRectangle;
var left = (int)rectangle.Left;
var top = (int)rectangle.Top;
var right = (int)rectangle.Right;
var bottom = (int)rectangle.Bottom;

await AddFileTaskAsync(imagePath, new FieldRegion(left, top, right, bottom));
}

private async Task AddFileTaskAsync(string filePath, Image sourceImage)
private async Task AddFileTaskAsync(string filePath, FieldRegion fieldRegion)
{
var outputDir = GetOutputDir();
var options = BuildOptions(filePath, outputDir);
var options = BuildOptions(filePath, outputDir, fieldRegion);
var exportFormats = options.OutputFormat.Split(',');

if (modeBcr.IsChecked == true)
Expand All @@ -144,7 +147,7 @@ private async Task AddFileTaskAsync(string filePath, Image sourceImage)
}
else if (options.Mode.IsSingleFieldLevel())
{
await StartSingleFieldProcessing(sourceImage, options)
await StartSingleFieldProcessing(options)
.ConfigureAwait(false);
}
else
Expand All @@ -166,7 +169,7 @@ private string GetOutputDir()
return outputDir;
}

private Options BuildOptions(string filePath, string outputDir)
private Options BuildOptions(string filePath, string outputDir, FieldRegion fieldRegion)
{
var mode = Mode.None;

Expand Down Expand Up @@ -195,6 +198,7 @@ private Options BuildOptions(string filePath, string outputDir)
TargetPath = outputDir,
Language = GetLanguages(),
OutputFormat = GetOutputFormat(mode),
Region = fieldRegion,
};

return options;
Expand All @@ -217,14 +221,12 @@ private Task StartGeneralProcessing(IEnumerable<string> exportFormats, Options o
return SafeInvokeProcessorCommands(_processor.ProcessPathAsync(options, task), task);
}

private Task StartSingleFieldProcessing(Image sourceImage, Options options)
private Task StartSingleFieldProcessing(Options options)
{
var task = new UserTask(options.SourcePath)
{
TaskStatus = "Uploading",
SourceIsTempFile = true,
IsFieldLevel = true,
SourceImage = sourceImage,
OutputFilePaths = new Dictionary<string, string>
{
["Xml"] = Path.Combine(options.TargetPath, $"{options.FileName}.xml"),
Expand Down Expand Up @@ -316,11 +318,6 @@ private void ProcessingCompleted(object sender, TaskEventArgs e)
{
var task = (UserTask)e.UserState;

if (task.SourceIsTempFile)
{
File.Delete(task.SourceFilePath);
}

if (e.Error != null)
{
MoveTaskToCompleted(task);
Expand Down
13 changes: 0 additions & 13 deletions Abbyy.CloudSdk.Demo.WindowsApp/Models/UserTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public UserTask(string filePath)
SourceFilePath = filePath;
TaskId = "<unknown>";
TaskStatus = "<initializing>";
SourceIsTempFile = false;
}

public UserTask(TaskInfo task)
Expand All @@ -39,12 +38,8 @@ public UserTask(TaskInfo task)
Description = task.Description;
RegistrationTime = task.RegistrationTime;
StatusChangeTime = task.StatusChangeTime;

SourceIsTempFile = false;
}

public bool SourceIsTempFile { get; set; }

public string SourceFilePath { get; set; }

public string SourceFileName => Path.GetFileName(SourceFilePath);
Expand Down Expand Up @@ -129,12 +124,6 @@ public string RecognizedText
set { _recognizedText = value; NotifyPropertyChanged("RecognizedText"); }
}

public System.Drawing.Image SourceImage
{
get => _sourceImage;
set { _sourceImage = value; NotifyPropertyChanged("SourceImage"); }
}

public string ErrorMessage
{
get => _errorMessage;
Expand All @@ -152,8 +141,6 @@ public string ErrorMessage
private DateTime _statusChangeTime;

private string _recognizedText;
private System.Drawing.Image _sourceImage;

private string _errorMessage;
}
}
8 changes: 2 additions & 6 deletions Abbyy.CloudSdk.Demo.WindowsApp/RegionSelectedEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Drawing;
using System.Windows;

namespace Abbyy.CloudSdk.Demo.WindowsApp
{
public class RegionSelectedEventArgs : System.EventArgs
{
public RegionSelectedEventArgs(Rect r, Bitmap cropped)
public RegionSelectedEventArgs(Rect rect)
{
SelectedRectangle = r;
CroppedImage = cropped;
SelectedRectangle = rect;
}

public Rect SelectedRectangle { get; set; }

public Bitmap CroppedImage { get; set; }
}
}