Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process from imagedata #188

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
5 changes: 4 additions & 1 deletion src/Tesseract/Interop/BaseApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ int BaseApiInit(HandleRef handle,
int BaseApiSetDebugVariable(HandleRef handle, string name, IntPtr valPtr);

// image analysis
[DllImport(Constants.TesseractDllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "TessBaseAPISetImage")]
void BaseApiSetImage(HandleRef handle, IntPtr imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line);

[RuntimeDllImport(Constants.TesseractDllName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "TessBaseAPISetImage2")]
void BaseApiSetImage(HandleRef handle, HandleRef pixHandle);

Expand Down Expand Up @@ -290,4 +293,4 @@ public static string ResultIteratorGetUTF8Text(HandleRef handle, PageIteratorLev
}
}
}
}
}
58 changes: 58 additions & 0 deletions src/Tesseract/TesseractEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,64 @@ public Page Process(Bitmap image, string inputName, Rect region, PageSegMode? pa
new PageDisposalHandle(page, pix);
return page;
}

/// <summary>
/// Processes a specified region in the image using the specified page layout analysis mode.
/// </summary>
/// <remarks>
/// You can only have one result iterator open at any one time.
/// </remarks>
/// <param name="imagedata">The array with the Pixeldata.</param>
/// <param name="width">The width of the image.</param>
/// <param name="height">The height of the image.</param>
/// <param name="bytes_per_pixel">Bytes per pixel.</param>
/// <param name="bytes_per_line">Number of bytes per Line. (Stride)</param>
/// <param name="inputName">Sets the input file's name, only needed for training or loading a uzn file.</param>
/// <param name="region">The image region to process.</param>
/// <param name="pageSegMode">The page layout analyasis method to use.</param>
/// <returns>A result iterator</returns>
public Page Process(byte[] imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line, string inputName, Rect region, PageSegMode? pageSegMode = null)
{
GCHandle pinnedImagedata = GCHandle.Alloc(imagedata, GCHandleType.Pinned);
IntPtr imagedataP = pinnedImagedata.AddrOfPinnedObject();
Page returnPage = Process(imagedataP, width, height, bytes_per_pixel, bytes_per_line, inputName, region, pageSegMode);
pinnedImagedata.Free();

return returnPage;
}

/// <summary>
/// Processes a specified region in the image using the specified page layout analysis mode.
/// </summary>
/// <remarks>
/// You can only have one result iterator open at any one time.
/// </remarks>
/// <param name="imagedata">The array with the Pixeldata.</param>
/// <param name="width">The width of the image.</param>
/// <param name="height">The height of the image.</param>
/// <param name="bytes_per_pixel">Bytes per pixel.</param>
/// <param name="bytes_per_line">Number of bytes per Line. (Stride)</param>
/// <param name="inputName">Sets the input file's name, only needed for training or loading a uzn file.</param>
/// <param name="region">The image region to process.</param>
/// <param name="pageSegMode">The page layout analyasis method to use.</param>
/// <returns>A result iterator</returns>
public Page Process(IntPtr imagedata, int width, int height, int bytes_per_pixel, int bytes_per_line, string inputName, Rect region, PageSegMode? pageSegMode = null)
{
if (processCount > 0) throw new InvalidOperationException("Only one image can be processed at once. Please make sure you dispose of the page once your finished with it.");

processCount++;

Interop.TessApi.BaseAPISetPageSegMode(handle, pageSegMode.HasValue ? pageSegMode.Value : DefaultPageSegMode);
Interop.TessApi.BaseApiSetImage(handle, imagedata, width, height, bytes_per_pixel, bytes_per_line);
Interop.TessApi.BaseApiSetRectangle(handle, region.X1, region.Y1, region.Width, region.Height);
if (!String.IsNullOrEmpty(inputName))
{
Interop.TessApi.BaseApiSetInputName(handle, inputName);
}
var page = new Page(this);
page.Disposed += OnIteratorDisposed;
return page;
}

protected override void Dispose(bool disposing)
{
Expand Down