|
@@ -1,6 +1,7 @@
|
|
using ComDocumentAIKit;
|
|
using ComDocumentAIKit;
|
|
using ComPDFKit.PDFDocument;
|
|
using ComPDFKit.PDFDocument;
|
|
using ComPDFKit.PDFPage;
|
|
using ComPDFKit.PDFPage;
|
|
|
|
+using ComPDFKit.PDFPage.Edit;
|
|
using ComPDFKitViewer;
|
|
using ComPDFKitViewer;
|
|
using ComPDFKitViewer.PdfViewer;
|
|
using ComPDFKitViewer.PdfViewer;
|
|
using PDF_Office.EventAggregators;
|
|
using PDF_Office.EventAggregators;
|
|
@@ -209,131 +210,134 @@ namespace PDF_Office.ViewModels.Scan
|
|
Directory.CreateDirectory(path);
|
|
Directory.CreateDirectory(path);
|
|
|
|
|
|
CPDFDocument CurrentDoc = PDFViewer.Document;
|
|
CPDFDocument CurrentDoc = PDFViewer.Document;
|
|
|
|
+ Task.Run(() =>
|
|
|
|
+ {
|
|
|
|
+ CErrorCode error = imEngine.SetModel(language);
|
|
|
|
|
|
- CErrorCode error = imEngine.SetModel(language);
|
|
|
|
-
|
|
|
|
- cacahe.Remove(PDFViewer.CurrentIndex);
|
|
|
|
- string pageImagePath = Path.Combine(path, "Area");
|
|
|
|
- Directory.CreateDirectory(pageImagePath);
|
|
|
|
- pageImagePath = Path.Combine(pageImagePath, PDFViewer.CurrentIndex.ToString());
|
|
|
|
|
|
+ cacahe.Remove(PDFViewer.CurrentIndex);
|
|
|
|
+ string pageImagePath = Path.Combine(path, "Area");
|
|
|
|
+ Directory.CreateDirectory(pageImagePath);
|
|
|
|
+ pageImagePath = Path.Combine(pageImagePath, PDFViewer.CurrentIndex.ToString());
|
|
|
|
|
|
- if (IsEnhanced)
|
|
|
|
- {
|
|
|
|
- BitmapImage image = new BitmapImage(new Uri(EnhancedFilePathList[PDFViewer.CurrentIndex]));
|
|
|
|
- BgImage = new WriteableBitmap(image);
|
|
|
|
- double zoomWidth = AreaRect.Width / ImageRect.Width;
|
|
|
|
- double zoomHeight = AreaRect.Height / ImageRect.Height;
|
|
|
|
- double zoomX = (AreaRect.X - ImageRect.X) / ImageRect.Width;
|
|
|
|
- double zoomY = (AreaRect.Y - ImageRect.Y) / ImageRect.Height;
|
|
|
|
- Int32Rect rect = new Int32Rect((int)(BgImage.Width*zoomX), (int)(BgImage.Height*zoomY), (int)(BgImage.Width*zoomWidth), (int)(BgImage.Height*zoomHeight));
|
|
|
|
- byte[] renderData = new byte[(int)(BgImage.Width * BgImage.Height * 4)];
|
|
|
|
-
|
|
|
|
- BgImage.CopyPixels(rect, renderData, BgImage.BackBufferStride, 0);
|
|
|
|
-
|
|
|
|
- WriteableBitmap bitmap = new WriteableBitmap(rect.Width, rect.Height, DpiHelpers.Dpi, DpiHelpers.Dpi, PixelFormats.Bgra32, null);
|
|
|
|
- bitmap.WritePixels(new Int32Rect(0, 0, rect.Width, rect.Height), renderData, BgImage.BackBufferStride, 0);
|
|
|
|
- BitmapEncoder encoder = new PngBitmapEncoder();
|
|
|
|
- encoder.Frames.Add(BitmapFrame.Create(bitmap));
|
|
|
|
-
|
|
|
|
- using (FileStream imageStream = File.Create(pageImagePath))
|
|
|
|
|
|
+ if (IsEnhanced)
|
|
{
|
|
{
|
|
- encoder.Save(imageStream);
|
|
|
|
- imageStream.Flush();
|
|
|
|
- }
|
|
|
|
- error = imEngine.Process(pageImagePath);
|
|
|
|
|
|
+ BitmapImage image = new BitmapImage(new Uri(EnhancedFilePathList[PDFViewer.CurrentIndex]));
|
|
|
|
+ BgImage = new WriteableBitmap(image);
|
|
|
|
+ double zoomWidth = AreaRect.Width / ImageRect.Width;
|
|
|
|
+ double zoomHeight = AreaRect.Height / ImageRect.Height;
|
|
|
|
+ double zoomX = (AreaRect.X - ImageRect.X) / ImageRect.Width;
|
|
|
|
+ double zoomY = (AreaRect.Y - ImageRect.Y) / ImageRect.Height;
|
|
|
|
+ Int32Rect rect = new Int32Rect((int)(BgImage.Width * zoomX), (int)(BgImage.Height * zoomY), (int)(BgImage.Width * zoomWidth), (int)(BgImage.Height * zoomHeight));
|
|
|
|
+ byte[] renderData = new byte[(int)(BgImage.Width * BgImage.Height * 4)];
|
|
|
|
|
|
- if (imEngine.OCRResultList == null)
|
|
|
|
- return;
|
|
|
|
|
|
+ BgImage.CopyPixels(rect, renderData, BgImage.BackBufferStride, 0);
|
|
|
|
|
|
- List<KeyValuePair<Rect, string>> RectList = new List<KeyValuePair<Rect, string>>();
|
|
|
|
- foreach (COCRResult ocrResult in imEngine.OCRResultList)
|
|
|
|
- {
|
|
|
|
- List<Point> rectPoints = new List<Point>();
|
|
|
|
- for (int j = 0; j < ocrResult.position.Length; j += 2)
|
|
|
|
|
|
+ WriteableBitmap bitmap = new WriteableBitmap(rect.Width, rect.Height, DpiHelpers.Dpi, DpiHelpers.Dpi, PixelFormats.Bgra32, null);
|
|
|
|
+ bitmap.WritePixels(new Int32Rect(0, 0, rect.Width, rect.Height), renderData, BgImage.BackBufferStride, 0);
|
|
|
|
+ BitmapEncoder encoder = new PngBitmapEncoder();
|
|
|
|
+ encoder.Frames.Add(BitmapFrame.Create(bitmap));
|
|
|
|
+
|
|
|
|
+ using (FileStream imageStream = File.Create(pageImagePath))
|
|
{
|
|
{
|
|
- rectPoints.Add(new Point(ocrResult.position[j], ocrResult.position[j + 1]));
|
|
|
|
|
|
+ encoder.Save(imageStream);
|
|
|
|
+ imageStream.Flush();
|
|
}
|
|
}
|
|
- int left = (int)rectPoints.AsEnumerable().Min(x => x.X);
|
|
|
|
- int right = (int)rectPoints.AsEnumerable().Max(x => x.X);
|
|
|
|
- int top = (int)rectPoints.AsEnumerable().Min(x => x.Y);
|
|
|
|
- int bottom = (int)rectPoints.AsEnumerable().Max(x => x.Y);
|
|
|
|
|
|
+ error = imEngine.Process(pageImagePath);
|
|
|
|
|
|
- RectList.Add(new KeyValuePair<Rect, string>(new Rect(left + BgImage.Width * zoomX, top + BgImage.Height * zoomY, right - left, bottom - top), ocrResult.text));
|
|
|
|
- }
|
|
|
|
- cacahe.Add(PDFViewer.CurrentIndex, RectList);
|
|
|
|
|
|
+ if (imEngine.OCRResultList == null)
|
|
|
|
+ return;
|
|
|
|
|
|
- if (cacahe.Count > 0)
|
|
|
|
- {
|
|
|
|
- if (cacahe.ContainsKey(PDFViewer.CurrentIndex))
|
|
|
|
|
|
+ List<KeyValuePair<Rect, string>> RectList = new List<KeyValuePair<Rect, string>>();
|
|
|
|
+ foreach (COCRResult ocrResult in imEngine.OCRResultList)
|
|
{
|
|
{
|
|
- TextRectList = cacahe[PDFViewer.CurrentIndex];
|
|
|
|
|
|
+ List<Point> rectPoints = new List<Point>();
|
|
|
|
+ for (int j = 0; j < ocrResult.position.Length; j += 2)
|
|
|
|
+ {
|
|
|
|
+ rectPoints.Add(new Point(ocrResult.position[j], ocrResult.position[j + 1]));
|
|
|
|
+ }
|
|
|
|
+ int left = (int)rectPoints.AsEnumerable().Min(x => x.X);
|
|
|
|
+ int right = (int)rectPoints.AsEnumerable().Max(x => x.X);
|
|
|
|
+ int top = (int)rectPoints.AsEnumerable().Min(x => x.Y);
|
|
|
|
+ int bottom = (int)rectPoints.AsEnumerable().Max(x => x.Y);
|
|
|
|
+
|
|
|
|
+ RectList.Add(new KeyValuePair<Rect, string>(new Rect(left + BgImage.Width * zoomX, top + BgImage.Height * zoomY, right - left, bottom - top), ocrResult.text));
|
|
}
|
|
}
|
|
- else
|
|
|
|
|
|
+ cacahe.Add(PDFViewer.CurrentIndex, RectList);
|
|
|
|
+
|
|
|
|
+ if (cacahe.Count > 0)
|
|
{
|
|
{
|
|
- TextRectList = null;
|
|
|
|
|
|
+ if (cacahe.ContainsKey(PDFViewer.CurrentIndex))
|
|
|
|
+ {
|
|
|
|
+ TextRectList = cacahe[PDFViewer.CurrentIndex];
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ TextRectList = null;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- CPDFPage pdfPage = CurrentDoc.PageAtIndex(PDFViewer.CurrentIndex);
|
|
|
|
- double zoomWidth = AreaRect.Width / ImageRect.Width;
|
|
|
|
- double zoomHeight = AreaRect.Height / ImageRect.Height;
|
|
|
|
- double zoomX = (AreaRect.X- ImageRect.X) / ImageRect.Width;
|
|
|
|
- double zoomY = (AreaRect.Y- ImageRect.Y) / ImageRect.Height;
|
|
|
|
-
|
|
|
|
- float zoom = (float)(DpiHelpers.Dpi / 72D);
|
|
|
|
- int renderWidth = (int)(pdfPage.PageSize.Width* zoomWidth * zoom);
|
|
|
|
- int renderHeight = (int)(pdfPage.PageSize.Height* zoomHeight * zoom);
|
|
|
|
- int renderX = (int)(pdfPage.PageSize.Width* zoomX * zoom);
|
|
|
|
- int renderY = (int)(pdfPage.PageSize.Height* zoomY * zoom);
|
|
|
|
- byte[] renderData = new byte[renderWidth * renderHeight * 4];
|
|
|
|
- pdfPage.RenderPageBitmapWithMatrix(zoom, new Rect(renderX, renderY, renderWidth, renderHeight), 0xFFFFFFFF, renderData, 0);
|
|
|
|
- WriteableBitmap bitmap = new WriteableBitmap(renderWidth, renderHeight, DpiHelpers.Dpi, DpiHelpers.Dpi, PixelFormats.Bgra32, null);
|
|
|
|
- bitmap.WritePixels(new Int32Rect(0, 0, renderWidth, renderHeight), renderData, bitmap.BackBufferStride, 0);
|
|
|
|
- BitmapEncoder encoder = new PngBitmapEncoder();
|
|
|
|
- encoder.Frames.Add(BitmapFrame.Create(bitmap));
|
|
|
|
-
|
|
|
|
- using (FileStream imageStream = File.Create(pageImagePath))
|
|
|
|
|
|
+ else
|
|
{
|
|
{
|
|
- encoder.Save(imageStream);
|
|
|
|
- imageStream.Flush();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- error = imEngine.Process(pageImagePath);
|
|
|
|
|
|
+ CPDFPage pdfPage = CurrentDoc.PageAtIndex(PDFViewer.CurrentIndex);
|
|
|
|
+ double zoomWidth = AreaRect.Width / ImageRect.Width;
|
|
|
|
+ double zoomHeight = AreaRect.Height / ImageRect.Height;
|
|
|
|
+ double zoomX = (AreaRect.X - ImageRect.X) / ImageRect.Width;
|
|
|
|
+ double zoomY = (AreaRect.Y - ImageRect.Y) / ImageRect.Height;
|
|
|
|
|
|
- if (imEngine.OCRResultList == null)
|
|
|
|
- return;
|
|
|
|
|
|
+ float zoom = (float)(DpiHelpers.Dpi / 72D);
|
|
|
|
+ int renderWidth = (int)(pdfPage.PageSize.Width * zoomWidth * zoom);
|
|
|
|
+ int renderHeight = (int)(pdfPage.PageSize.Height * zoomHeight * zoom);
|
|
|
|
+ int renderX = (int)(pdfPage.PageSize.Width * zoomX * zoom);
|
|
|
|
+ int renderY = (int)(pdfPage.PageSize.Height * zoomY * zoom);
|
|
|
|
+ byte[] renderData = new byte[renderWidth * renderHeight * 4];
|
|
|
|
+ pdfPage.RenderPageBitmapWithMatrix(zoom, new Rect(renderX, renderY, renderWidth, renderHeight), 0xFFFFFFFF, renderData, 0);
|
|
|
|
+ WriteableBitmap bitmap = new WriteableBitmap(renderWidth, renderHeight, DpiHelpers.Dpi, DpiHelpers.Dpi, PixelFormats.Bgra32, null);
|
|
|
|
+ bitmap.WritePixels(new Int32Rect(0, 0, renderWidth, renderHeight), renderData, bitmap.BackBufferStride, 0);
|
|
|
|
+ BitmapEncoder encoder = new PngBitmapEncoder();
|
|
|
|
+ encoder.Frames.Add(BitmapFrame.Create(bitmap));
|
|
|
|
|
|
- List<KeyValuePair<Rect, string>> RectList = new List<KeyValuePair<Rect, string>>();
|
|
|
|
- foreach (COCRResult ocrResult in imEngine.OCRResultList)
|
|
|
|
- {
|
|
|
|
- List<Point> rectPoints = new List<Point>();
|
|
|
|
- for (int j = 0; j < ocrResult.position.Length; j += 2)
|
|
|
|
|
|
+ using (FileStream imageStream = File.Create(pageImagePath))
|
|
{
|
|
{
|
|
- rectPoints.Add(new Point(ocrResult.position[j], ocrResult.position[j + 1]));
|
|
|
|
|
|
+ encoder.Save(imageStream);
|
|
|
|
+ imageStream.Flush();
|
|
}
|
|
}
|
|
- int left = (int)rectPoints.AsEnumerable().Min(x => x.X);
|
|
|
|
- int right = (int)rectPoints.AsEnumerable().Max(x => x.X);
|
|
|
|
- int top = (int)rectPoints.AsEnumerable().Min(x => x.Y);
|
|
|
|
- int bottom = (int)rectPoints.AsEnumerable().Max(x => x.Y);
|
|
|
|
|
|
|
|
- RectList.Add(new KeyValuePair<Rect, string>(new Rect(left+ renderX, top+ renderY, right - left, bottom - top), ocrResult.text));
|
|
|
|
- }
|
|
|
|
- cacahe.Add(PDFViewer.CurrentIndex, RectList);
|
|
|
|
|
|
+ error = imEngine.Process(pageImagePath);
|
|
|
|
|
|
- if (cacahe.Count > 0)
|
|
|
|
- {
|
|
|
|
- if (cacahe.ContainsKey(PDFViewer.CurrentIndex))
|
|
|
|
|
|
+ if (imEngine.OCRResultList == null)
|
|
|
|
+ return;
|
|
|
|
+
|
|
|
|
+ List<KeyValuePair<Rect, string>> RectList = new List<KeyValuePair<Rect, string>>();
|
|
|
|
+ foreach (COCRResult ocrResult in imEngine.OCRResultList)
|
|
{
|
|
{
|
|
- TextRectList = cacahe[PDFViewer.CurrentIndex];
|
|
|
|
|
|
+ List<Point> rectPoints = new List<Point>();
|
|
|
|
+ for (int j = 0; j < ocrResult.position.Length; j += 2)
|
|
|
|
+ {
|
|
|
|
+ rectPoints.Add(new Point(ocrResult.position[j], ocrResult.position[j + 1]));
|
|
|
|
+ }
|
|
|
|
+ int left = (int)rectPoints.AsEnumerable().Min(x => x.X);
|
|
|
|
+ int right = (int)rectPoints.AsEnumerable().Max(x => x.X);
|
|
|
|
+ int top = (int)rectPoints.AsEnumerable().Min(x => x.Y);
|
|
|
|
+ int bottom = (int)rectPoints.AsEnumerable().Max(x => x.Y);
|
|
|
|
+
|
|
|
|
+ RectList.Add(new KeyValuePair<Rect, string>(new Rect(left + renderX, top + renderY, right - left, bottom - top), ocrResult.text));
|
|
}
|
|
}
|
|
- else
|
|
|
|
|
|
+ cacahe.Add(PDFViewer.CurrentIndex, RectList);
|
|
|
|
+
|
|
|
|
+ if (cacahe.Count > 0)
|
|
{
|
|
{
|
|
- TextRectList = null;
|
|
|
|
|
|
+ if (cacahe.ContainsKey(PDFViewer.CurrentIndex))
|
|
|
|
+ {
|
|
|
|
+ TextRectList = cacahe[PDFViewer.CurrentIndex];
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ TextRectList = null;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- }
|
|
|
|
|
|
+ imEngine.Release();
|
|
|
|
+ });
|
|
ClearDraw();
|
|
ClearDraw();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -630,5 +634,41 @@ namespace PDF_Office.ViewModels.Scan
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public void Save()
|
|
|
|
+ {
|
|
|
|
+ for (int i = 0; i < PDFViewer.Document.PageCount; i++)
|
|
|
|
+ {
|
|
|
|
+ if (cacahe.ContainsKey(i))
|
|
|
|
+ {
|
|
|
|
+ CPDFPage pdfPage = PDFViewer.Document.PageAtIndex(i);
|
|
|
|
+ CPDFEditPage editPage = pdfPage.GetEditPage();
|
|
|
|
+ editPage.BeginEdit(CPDFEditType.EditText);
|
|
|
|
+ for (int y = textRectList.Count - 1; y >= 0; y--)
|
|
|
|
+ {
|
|
|
|
+ KeyValuePair<Rect, string> textBlock = textRectList[y];
|
|
|
|
+ int fontSize = 1;
|
|
|
|
+ Rect pdfRect = DpiHelpers.GetRawRect(textBlock.Key);
|
|
|
|
+
|
|
|
|
+ for (; fontSize < 100; fontSize++)
|
|
|
|
+ {
|
|
|
|
+ Rect cmpRect = pdfPage.GetTextContentRect(fontSize, "Helvetica", textBlock.Value);
|
|
|
|
+ if (cmpRect.Width > pdfRect.Width || cmpRect.Height > pdfRect.Height)
|
|
|
|
+ {
|
|
|
|
+ fontSize -= 1;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //字体
|
|
|
|
+ CPDFEditTextArea editArea = editPage.CreateNewTextArea(pdfRect, "Helvetica", fontSize, new byte[] { 0, 0, 0 });
|
|
|
|
+ //透明度
|
|
|
|
+ editArea.SetCharsFontTransparency(0);
|
|
|
|
+ bool x = editArea.SetTextAreaAlign(TextAlignType.AlignChar);
|
|
|
|
+ editArea.InsertText(textBlock.Value);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ editPage.EndEdit();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|