|
|
using CommunityToolkit.Mvvm.DependencyInjection;
|
|
|
using iText.IO.Image;
|
|
|
using Microsoft.Win32;
|
|
|
using Newtonsoft.Json;
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
using OpenCvSharp;
|
|
|
using OpenCvSharp.WpfExtensions;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.Diagnostics;
|
|
|
using System.Drawing;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Reflection.Emit;
|
|
|
using System.Reflection.Metadata;
|
|
|
using System.Security.Cryptography;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
using System.Windows;
|
|
|
using System.Windows.Controls;
|
|
|
using System.Windows.Controls.Primitives;
|
|
|
using System.Windows.Data;
|
|
|
using System.Windows.Documents;
|
|
|
|
|
|
using System.Windows.Input;
|
|
|
using System.Windows.Media;
|
|
|
using System.Windows.Media.Imaging;
|
|
|
using System.Windows.Media.Media3D;
|
|
|
using System.Windows.Shapes;
|
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
|
using Brushes = System.Windows.Media.Brush;
|
|
|
using Path = System.IO.Path;
|
|
|
using Point = System.Windows.Point;
|
|
|
using Rectangle = System.Windows.Shapes.Rectangle;
|
|
|
using Window = System.Windows.Window;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace WpfEditor
|
|
|
{
|
|
|
|
|
|
public class ImageFileInfo
|
|
|
{
|
|
|
public string Name { get; set; }
|
|
|
public string FullPath { get; set; }
|
|
|
|
|
|
public ImageFileInfo(string fullPath)
|
|
|
{
|
|
|
FullPath = fullPath;
|
|
|
Name = Path.GetFileName(fullPath);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
public class DirectoryTreeNode
|
|
|
{
|
|
|
|
|
|
public int level { get; set; }
|
|
|
public TextLine line { get; set; }
|
|
|
|
|
|
|
|
|
public string Name { get; set; }
|
|
|
public string FullPath { get; set; }
|
|
|
public string Icon { get; set; }
|
|
|
public bool IsDirectory { get; set; }
|
|
|
public List<DirectoryTreeNode> Children { get; set; }
|
|
|
|
|
|
public DirectoryTreeNode(string Name, int level, TextLine textline)
|
|
|
{
|
|
|
FullPath = Name;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.Name = Name;
|
|
|
this.level = level;
|
|
|
this.line = textline;
|
|
|
Icon = "📄";
|
|
|
IsDirectory = false;
|
|
|
Children = new List<DirectoryTreeNode>();
|
|
|
}
|
|
|
|
|
|
public override string ToString()
|
|
|
{
|
|
|
return Name;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public enum EditorMode
|
|
|
{
|
|
|
ImageOCR,
|
|
|
ReviseEdit,
|
|
|
MenuSplit
|
|
|
}
|
|
|
|
|
|
public enum OcrMode
|
|
|
{
|
|
|
WeChatOcr,
|
|
|
ComicOcr,
|
|
|
AliOCR,
|
|
|
PPOCR
|
|
|
}
|
|
|
|
|
|
public class CursorPositionChangedEventArgs : EventArgs
|
|
|
{
|
|
|
public int Line { get; set; }
|
|
|
public int Column { get; set; }
|
|
|
|
|
|
public CursorPositionChangedEventArgs(int line, int column)
|
|
|
{
|
|
|
Line = line;
|
|
|
Column = column;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public partial class MainWindow : Window
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ViewModel ViewModel { get; set; }
|
|
|
private Mat? imgMat;
|
|
|
private System.Windows.Point origin;
|
|
|
private System.Windows.Point start;
|
|
|
private bool isDragging = false;
|
|
|
private bool isDrawingRectangle = false;
|
|
|
private System.Windows.Point rectangleStartPoint;
|
|
|
|
|
|
|
|
|
public string _book = "";
|
|
|
public List<string> opened_image_pths = new List<string>();
|
|
|
|
|
|
private string _currentFilePath = null;
|
|
|
private bool _textChanged = false;
|
|
|
private double _imageZoomFactor = 1.0;
|
|
|
private EditorMode _mode = EditorMode.ImageOCR;
|
|
|
public OcrMode _ocrMode = OcrMode.WeChatOcr;
|
|
|
|
|
|
|
|
|
private bool _isSelecting = false;
|
|
|
private Point _selectionStartPoint;
|
|
|
private Rectangle _selectionRectangle;
|
|
|
private Canvas _selectionCanvas;
|
|
|
|
|
|
private List<Rectangle> _selectionRectangles = new List<Rectangle>();
|
|
|
|
|
|
|
|
|
private List<JObject> _pendingRectangles = new List<JObject>();
|
|
|
|
|
|
|
|
|
private bool _isResizing = false;
|
|
|
private Rectangle _resizingRectangle = null;
|
|
|
private string _resizeDirection = "";
|
|
|
private Point _resizeStartPoint;
|
|
|
private double _originalLeft, _originalTop, _originalWidth, _originalHeight;
|
|
|
|
|
|
private bool _isImageListVisible = true;
|
|
|
|
|
|
|
|
|
private double _previousImageWidth = 0;
|
|
|
private double _previousImageHeight = 0;
|
|
|
|
|
|
public Dictionary<string, string> _dic_m5_imgname = new Dictionary<string, string>();
|
|
|
|
|
|
public MainWindow(EditorMode mode = EditorMode.ImageOCR)
|
|
|
{
|
|
|
InitializeComponent();
|
|
|
Editor._mainWindow = this;
|
|
|
|
|
|
ViewModel = new ViewModel();
|
|
|
DataContext = ViewModel;
|
|
|
|
|
|
Editor.TextChanged += Editor_TextChanged;
|
|
|
Editor.CursorPositionChanged += Editor_CursorPositionChanged;
|
|
|
|
|
|
|
|
|
image.SizeChanged += Image_SizeChanged;
|
|
|
|
|
|
|
|
|
InitializeSelectionCanvas();
|
|
|
|
|
|
this._mode = mode;
|
|
|
Editor._mode = mode;
|
|
|
|
|
|
|
|
|
if (this._mode == EditorMode.ImageOCR)
|
|
|
{
|
|
|
this.Title = "图片识别";
|
|
|
}
|
|
|
else if (this._mode == EditorMode.ReviseEdit)
|
|
|
{
|
|
|
this.Title = "整理校对";
|
|
|
}
|
|
|
else if (this._mode == EditorMode.MenuSplit)
|
|
|
{
|
|
|
this.Title = "目录拆解";
|
|
|
this.BorderDirectoryTreeView.Visibility = Visibility.Visible;
|
|
|
this.DirectoryTreeView.Visibility = Visibility.Visible;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
FontManager.SetProgrammingFont(Editor, 14);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
private void OnOpenImage_Click(string FileName)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
BitmapImage bitmap = new BitmapImage();
|
|
|
bitmap.BeginInit();
|
|
|
bitmap.UriSource = new Uri(FileName);
|
|
|
bitmap.EndInit();
|
|
|
|
|
|
ViewModel.Image = bitmap;
|
|
|
ViewModel.ClearAllRectangles();
|
|
|
|
|
|
|
|
|
_previousImageWidth = 0;
|
|
|
_previousImageHeight = 0;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
MessageBox.Show($"无法加载图片: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void Grid_MouseWheel(object sender, MouseWheelEventArgs e)
|
|
|
{
|
|
|
double zoom = e.Delta > 0 ? 0.2 : -0.2;
|
|
|
double minScale = 0.5;
|
|
|
double maxScale = 10.0;
|
|
|
double newScale = scaleTransform.ScaleX + zoom;
|
|
|
|
|
|
if (newScale < minScale) newScale = minScale;
|
|
|
else if (newScale > maxScale) newScale = maxScale;
|
|
|
|
|
|
if (Math.Abs(newScale - scaleTransform.ScaleX) < 0.001) return;
|
|
|
|
|
|
var mousePos = e.GetPosition(image);
|
|
|
var oldScale = scaleTransform.ScaleX;
|
|
|
|
|
|
scaleTransform.ScaleX = newScale;
|
|
|
scaleTransform.ScaleY = newScale;
|
|
|
|
|
|
var deltaScale = newScale - oldScale;
|
|
|
double newX = translateTransform.X - mousePos.X * deltaScale;
|
|
|
double newY = translateTransform.Y - mousePos.Y * deltaScale;
|
|
|
|
|
|
|
|
|
if (image.ActualWidth > 0 && image.ActualHeight > 0)
|
|
|
{
|
|
|
var grid = sender as Grid;
|
|
|
if (grid != null)
|
|
|
{
|
|
|
double scaledWidth = image.ActualWidth * newScale;
|
|
|
double scaledHeight = image.ActualHeight * newScale;
|
|
|
double containerWidth = grid.ActualWidth;
|
|
|
double containerHeight = grid.ActualHeight;
|
|
|
|
|
|
|
|
|
double minX = -scaledWidth + 50;
|
|
|
double maxX = containerWidth - 50;
|
|
|
newX = Math.Max(minX, Math.Min(maxX, newX));
|
|
|
|
|
|
|
|
|
double minY = -scaledHeight + 50;
|
|
|
double maxY = containerHeight - 50;
|
|
|
newY = Math.Max(minY, Math.Min(maxY, newY));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
translateTransform.X = newX;
|
|
|
translateTransform.Y = newY;
|
|
|
|
|
|
if (Math.Abs(newScale - 1.0) < 0.001)
|
|
|
{
|
|
|
translateTransform.X = 0;
|
|
|
translateTransform.Y = 0;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void Grid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
|
|
{
|
|
|
if (!isDrawingRectangle)
|
|
|
{
|
|
|
var grid = sender as Grid;
|
|
|
if (grid != null)
|
|
|
{
|
|
|
grid.CaptureMouse();
|
|
|
isDragging = true;
|
|
|
start = e.GetPosition(grid);
|
|
|
origin = new System.Windows.Point(translateTransform.X, translateTransform.Y);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void Grid_MouseMove(object sender, MouseEventArgs e)
|
|
|
{
|
|
|
if (isDragging && e.LeftButton == MouseButtonState.Pressed &&
|
|
|
(Keyboard.IsKeyUp(Key.LeftCtrl) && Keyboard.IsKeyUp(Key.RightCtrl)))
|
|
|
{
|
|
|
var grid = sender as Grid;
|
|
|
if (grid != null)
|
|
|
{
|
|
|
Vector v = start - e.GetPosition(grid);
|
|
|
double newX = origin.X - v.X;
|
|
|
double newY = origin.Y - v.Y;
|
|
|
|
|
|
|
|
|
if (image.ActualWidth > 0 && image.ActualHeight > 0)
|
|
|
{
|
|
|
double scaledWidth = image.ActualWidth * scaleTransform.ScaleX;
|
|
|
double scaledHeight = image.ActualHeight * scaleTransform.ScaleY;
|
|
|
double containerWidth = grid.ActualWidth;
|
|
|
double containerHeight = grid.ActualHeight;
|
|
|
|
|
|
|
|
|
double minX = -scaledWidth + 50;
|
|
|
double maxX = containerWidth - 50;
|
|
|
newX = Math.Max(minX, Math.Min(maxX, newX));
|
|
|
|
|
|
|
|
|
double minY = -scaledHeight + 50;
|
|
|
double maxY = containerHeight - 50;
|
|
|
newY = Math.Max(minY, Math.Min(maxY, newY));
|
|
|
}
|
|
|
|
|
|
translateTransform.X = newX;
|
|
|
translateTransform.Y = newY;
|
|
|
}
|
|
|
}
|
|
|
else if (isDrawingRectangle && e.RightButton == MouseButtonState.Pressed)
|
|
|
{
|
|
|
var currentPoint = e.GetPosition(image);
|
|
|
UpdateRectangle(rectangleStartPoint, currentPoint);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void Grid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
|
|
|
{
|
|
|
var grid = sender as Grid;
|
|
|
if (grid != null)
|
|
|
{
|
|
|
grid.ReleaseMouseCapture();
|
|
|
isDragging = false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private RectangleItem GetClickedRectangle(Point point)
|
|
|
{
|
|
|
|
|
|
if (_mode != EditorMode.ReviseEdit)
|
|
|
return null;
|
|
|
|
|
|
|
|
|
foreach (var rectangle in ViewModel.Rectangles)
|
|
|
{
|
|
|
if (rectangle.IsVisible &&
|
|
|
point.X >= rectangle.Left &&
|
|
|
point.X <= rectangle.Left + rectangle.Width &&
|
|
|
point.Y >= rectangle.Top &&
|
|
|
point.Y <= rectangle.Top + rectangle.Height)
|
|
|
{
|
|
|
return rectangle;
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ShowDeleteContextMenu(RectangleItem rectangle, Point position)
|
|
|
{
|
|
|
var contextMenu = new ContextMenu();
|
|
|
var deleteMenuItem = new MenuItem()
|
|
|
{
|
|
|
Header = "删除选框",
|
|
|
Tag = rectangle
|
|
|
};
|
|
|
deleteMenuItem.Click += (s, e) =>
|
|
|
{
|
|
|
var rect = (RectangleItem)((MenuItem)s).Tag;
|
|
|
|
|
|
|
|
|
List<int> idxs = new List<int>();
|
|
|
foreach (var (line, index) in Editor._lines.Select((line, index) => (line, index) ))
|
|
|
{
|
|
|
if (line.selected_rectange == null || !line.selected_rectange.HasValues)
|
|
|
{
|
|
|
continue;
|
|
|
}
|
|
|
int Id = line.selected_rectange["Id"].Value<int>();
|
|
|
if (rect.Id == Id)
|
|
|
{
|
|
|
idxs.Add(index);
|
|
|
}
|
|
|
}
|
|
|
if (idxs.Count > 0)
|
|
|
{
|
|
|
|
|
|
foreach (var (line, index) in Editor._lines.Select((line, index) => (line, index)))
|
|
|
{
|
|
|
if (line.selected_rectange == null || !line.selected_rectange.HasValues)
|
|
|
{
|
|
|
continue;
|
|
|
}
|
|
|
int Id = Editor._lines[index].selected_rectange["Id"].Value<int>();
|
|
|
if (Id > rect.Id)
|
|
|
{
|
|
|
Editor._lines[index].selected_rectange["Id"] = Id - 1;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
foreach(RectangleItem item in ViewModel.rectangles)
|
|
|
{
|
|
|
if (item.Id > rect.Id)
|
|
|
{
|
|
|
item.Id -= 1;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for (int i = idxs.Count - 1; i >= 0; i--)
|
|
|
{
|
|
|
Editor._lines.RemoveAt( idxs[i] );
|
|
|
}
|
|
|
|
|
|
|
|
|
ViewModel.RemoveRectangle(rect);
|
|
|
ViewModel.rectangleCounter -= 1;
|
|
|
|
|
|
}
|
|
|
|
|
|
Editor.ClearSelection();
|
|
|
_textChanged = true;
|
|
|
Editor.InvalidateVisual();
|
|
|
|
|
|
Editor.OnTextChanged();
|
|
|
|
|
|
};
|
|
|
|
|
|
contextMenu.Items.Add(deleteMenuItem);
|
|
|
contextMenu.IsOpen = true;
|
|
|
contextMenu.PlacementTarget = image;
|
|
|
contextMenu.Placement = PlacementMode.MousePoint;
|
|
|
}
|
|
|
|
|
|
private void Grid_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
|
|
|
{
|
|
|
if (ViewModel.Image != null)
|
|
|
{
|
|
|
rectangleStartPoint = e.GetPosition(image);
|
|
|
|
|
|
|
|
|
if (_mode == EditorMode.ReviseEdit)
|
|
|
{
|
|
|
var clickedRectangle = GetClickedRectangle(rectangleStartPoint);
|
|
|
if (clickedRectangle != null)
|
|
|
{
|
|
|
|
|
|
ShowDeleteContextMenu(clickedRectangle, rectangleStartPoint);
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
isDrawingRectangle = true;
|
|
|
ViewModel.IsDrawingRectangle = true;
|
|
|
ViewModel.RectangleVisible = true;
|
|
|
|
|
|
var grid = sender as Grid;
|
|
|
grid?.CaptureMouse();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void Grid_MouseRightButtonUp(object sender, MouseButtonEventArgs e)
|
|
|
{
|
|
|
if (isDrawingRectangle)
|
|
|
{
|
|
|
var grid = sender as Grid;
|
|
|
grid?.ReleaseMouseCapture();
|
|
|
|
|
|
isDrawingRectangle = false;
|
|
|
ViewModel.IsDrawingRectangle = false;
|
|
|
|
|
|
|
|
|
if (ViewModel.RectangleWidth > 5 && ViewModel.RectangleHeight > 5)
|
|
|
{
|
|
|
|
|
|
int Id = ViewModel.AddRectangle(
|
|
|
ViewModel.RectangleLeft,
|
|
|
ViewModel.RectangleTop,
|
|
|
ViewModel.RectangleWidth,
|
|
|
ViewModel.RectangleHeight
|
|
|
);
|
|
|
|
|
|
var x_y_ = Editor.ConvertDisplayToOriginalCoordinates(ViewModel.RectangleLeft, ViewModel.RectangleTop);
|
|
|
var w_h_ = Editor.ConvertDisplayToOriginalSize(ViewModel.RectangleWidth, ViewModel.RectangleHeight);
|
|
|
var pth_img = Editor._pthimg;
|
|
|
|
|
|
Mat orig = new Mat(pth_img, ImreadModes.Color);
|
|
|
var rect = new OpenCvSharp.Rect
|
|
|
{
|
|
|
X = (int)(x_y_.X + 0.5),
|
|
|
Y = (int)(x_y_.Y + 0.5),
|
|
|
Width = (int)(w_h_.Width+0.5) > orig.Width ? orig.Width: (int)(w_h_.Width+0.5),
|
|
|
Height = (int)(w_h_.Height+0.5) > orig.Height ? orig.Height : (int)(w_h_.Height + 0.5),
|
|
|
};
|
|
|
orig.Rectangle(rect, Scalar.Red);
|
|
|
|
|
|
Cv2.ImShow("orig", orig);
|
|
|
Cv2.WaitKey();
|
|
|
orig.Dispose();
|
|
|
|
|
|
|
|
|
JObject jsn = (JObject)Editor._jsn_OpenImg.DeepClone();
|
|
|
jsn = Global.find_rectange_text(jsn, rect.X, rect.Y, rect.Width, rect.Height, Id);
|
|
|
Editor.InsertText(jsn, Editor._md5_OpenImg);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
ViewModel.ClearRectangle();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void On_Rectangle_adjust(RectangleItem rectangle)
|
|
|
{
|
|
|
|
|
|
List<int> idxs = new List<int>();
|
|
|
foreach (var (line, index) in Editor._lines.Select((line, index) => (line, index)))
|
|
|
{
|
|
|
if (line.selected_rectange == null || !line.selected_rectange.HasValues)
|
|
|
{
|
|
|
continue;
|
|
|
}
|
|
|
int Id = line.selected_rectange["Id"].Value<int>();
|
|
|
if (rectangle.Id == Id)
|
|
|
{
|
|
|
idxs.Add(index);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
var x_y_ = Editor.ConvertDisplayToOriginalCoordinates(rectangle.Left, rectangle.Top);
|
|
|
var w_h_ = Editor.ConvertDisplayToOriginalSize(rectangle.Width, rectangle.Height);
|
|
|
var pth_img = Editor._pthimg;
|
|
|
|
|
|
Mat orig = new Mat(pth_img, ImreadModes.Color);
|
|
|
var rect = new OpenCvSharp.Rect
|
|
|
{
|
|
|
X = (int)(x_y_.X + 0.5),
|
|
|
Y = (int)(x_y_.Y + 0.5),
|
|
|
Width = (int)(w_h_.Width + 0.5) > orig.Width ? orig.Width : (int)(w_h_.Width + 0.5),
|
|
|
Height = (int)(w_h_.Height + 0.5) > orig.Height ? orig.Height : (int)(w_h_.Height + 0.5),
|
|
|
};
|
|
|
|
|
|
JObject jsn = (JObject)Editor._jsn_OpenImg.DeepClone();
|
|
|
jsn = Global.find_rectange_text(jsn, rect.X, rect.Y, rect.Width, rect.Height, rectangle.Id);
|
|
|
if (jsn == null || !jsn.HasValues)
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
var lines = Editor.get_text_lines(jsn, Editor._md5_OpenImg);
|
|
|
|
|
|
|
|
|
if (idxs.Count > 0 && lines.Count > 0)
|
|
|
{
|
|
|
int start = idxs[0];
|
|
|
|
|
|
Editor._lines.RemoveRange(start, idxs.Count);
|
|
|
Editor._lines.InsertRange(start, lines);
|
|
|
|
|
|
Editor.ClearSelection();
|
|
|
_textChanged = true;
|
|
|
Editor.InvalidateVisual();
|
|
|
|
|
|
Editor.OnTextChanged();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void UpdateRectangle(System.Windows.Point startPoint, System.Windows.Point currentPoint)
|
|
|
{
|
|
|
double left = Math.Min(startPoint.X, currentPoint.X);
|
|
|
double top = Math.Min(startPoint.Y, currentPoint.Y);
|
|
|
double width = Math.Abs(currentPoint.X - startPoint.X);
|
|
|
double height = Math.Abs(currentPoint.Y - startPoint.Y);
|
|
|
|
|
|
ViewModel.RectangleLeft = left;
|
|
|
ViewModel.RectangleTop = top;
|
|
|
ViewModel.RectangleWidth = width;
|
|
|
ViewModel.RectangleHeight = height;
|
|
|
}
|
|
|
|
|
|
private void CropAndShowImage()
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
if (ViewModel.Image == null) return;
|
|
|
|
|
|
|
|
|
var topLeft = GetScaledPosition(new System.Windows.Point(ViewModel.RectangleLeft, ViewModel.RectangleTop), image);
|
|
|
var bottomRight = GetScaledPosition(new System.Windows.Point(
|
|
|
ViewModel.RectangleLeft + ViewModel.RectangleWidth,
|
|
|
ViewModel.RectangleTop + ViewModel.RectangleHeight), image);
|
|
|
|
|
|
|
|
|
int x = Math.Max(0, (int)topLeft.X);
|
|
|
int y = Math.Max(0, (int)topLeft.Y);
|
|
|
int width = Math.Min((int)(bottomRight.X - topLeft.X), ViewModel.Image.PixelWidth - x);
|
|
|
int height = Math.Min((int)(bottomRight.Y - topLeft.Y), ViewModel.Image.PixelHeight - y);
|
|
|
|
|
|
if (width <= 0 || height <= 0) return;
|
|
|
|
|
|
|
|
|
imgMat = ViewModel.Image.ToMat();
|
|
|
var cropRect = new OpenCvSharp.Rect(x, y, width, height);
|
|
|
var croppedMat = imgMat[cropRect];
|
|
|
|
|
|
|
|
|
var croppedBitmap = croppedMat.ToBitmapSource();
|
|
|
|
|
|
|
|
|
ShowCroppedImage(croppedBitmap, new System.Windows.Point(x, y), new System.Windows.Point(x + width, y + height));
|
|
|
|
|
|
croppedMat.Dispose();
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
MessageBox.Show($"截图失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
public void ShowCharacterSelection(double left, double top, double width, double height)
|
|
|
{
|
|
|
|
|
|
ViewModel.RectangleLeft = left;
|
|
|
ViewModel.RectangleTop = top;
|
|
|
ViewModel.RectangleWidth = width;
|
|
|
ViewModel.RectangleHeight = height;
|
|
|
ViewModel.RectangleVisible = true;
|
|
|
}
|
|
|
|
|
|
private void ShowCroppedImage(BitmapSource croppedImage, System.Windows.Point topLeft, System.Windows.Point bottomRight)
|
|
|
{
|
|
|
var cropWindow = new Window
|
|
|
{
|
|
|
Title = "截图结果",
|
|
|
Width = Math.Max(300, croppedImage.PixelWidth + 50),
|
|
|
Height = Math.Max(200, croppedImage.PixelHeight + 100),
|
|
|
WindowStartupLocation = WindowStartupLocation.CenterOwner,
|
|
|
Owner = this,
|
|
|
Background = new SolidColorBrush(Colors.Black)
|
|
|
};
|
|
|
|
|
|
var stackPanel = new StackPanel
|
|
|
{
|
|
|
Margin = new Thickness(10, 10, 10, 10)
|
|
|
};
|
|
|
|
|
|
var infoText = new TextBlock
|
|
|
{
|
|
|
Text = $"原图坐标: ({topLeft.X:F0}, {topLeft.Y:F0}) - ({bottomRight.X:F0}, {bottomRight.Y:F0})\n" +
|
|
|
$"截图尺寸: {croppedImage.PixelWidth} x {croppedImage.PixelHeight}",
|
|
|
Foreground = new SolidColorBrush(Colors.White),
|
|
|
FontSize = 12,
|
|
|
Margin = new Thickness(0, 0, 0, 10),
|
|
|
TextAlignment = TextAlignment.Center
|
|
|
};
|
|
|
|
|
|
var imageControl = new System.Windows.Controls.Image
|
|
|
{
|
|
|
Source = croppedImage,
|
|
|
Stretch = Stretch.Uniform,
|
|
|
MaxWidth = 800,
|
|
|
MaxHeight = 600
|
|
|
};
|
|
|
|
|
|
var buttonPanel = new StackPanel
|
|
|
{
|
|
|
Orientation = Orientation.Horizontal,
|
|
|
HorizontalAlignment = HorizontalAlignment.Center,
|
|
|
Margin = new Thickness(0, 10, 0, 0)
|
|
|
};
|
|
|
|
|
|
var saveButton = new Button
|
|
|
{
|
|
|
Content = "保存图片",
|
|
|
Padding = new Thickness(10, 5, 10, 5),
|
|
|
Margin = new Thickness(5, 5, 5, 5),
|
|
|
Background = new SolidColorBrush(Colors.DodgerBlue),
|
|
|
Foreground = new SolidColorBrush(Colors.White)
|
|
|
};
|
|
|
|
|
|
saveButton.Click += (s, e) => SaveCroppedImage(croppedImage);
|
|
|
|
|
|
var closeButton = new Button
|
|
|
{
|
|
|
Content = "关闭",
|
|
|
Padding = new Thickness(10, 5, 10, 5),
|
|
|
Margin = new Thickness(5, 5, 5, 5),
|
|
|
Background = new SolidColorBrush(Colors.Gray),
|
|
|
Foreground = new SolidColorBrush(Colors.White)
|
|
|
};
|
|
|
|
|
|
closeButton.Click += (s, e) => cropWindow.Close();
|
|
|
|
|
|
buttonPanel.Children.Add(saveButton);
|
|
|
buttonPanel.Children.Add(closeButton);
|
|
|
|
|
|
stackPanel.Children.Add(infoText);
|
|
|
stackPanel.Children.Add(imageControl);
|
|
|
stackPanel.Children.Add(buttonPanel);
|
|
|
|
|
|
cropWindow.Content = stackPanel;
|
|
|
cropWindow.ShowDialog();
|
|
|
}
|
|
|
|
|
|
private void SaveCroppedImage(BitmapSource image)
|
|
|
{
|
|
|
var saveDialog = new SaveFileDialog
|
|
|
{
|
|
|
Title = "保存截图",
|
|
|
Filter = "PNG图片|*.png|JPEG图片|*.jpg|所有文件|*.*",
|
|
|
DefaultExt = "png"
|
|
|
};
|
|
|
|
|
|
if (saveDialog.ShowDialog() == true)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
using (var fileStream = new FileStream(saveDialog.FileName, FileMode.Create))
|
|
|
{
|
|
|
BitmapEncoder encoder = saveDialog.FileName.ToLower().EndsWith(".jpg") ||
|
|
|
saveDialog.FileName.ToLower().EndsWith(".jpeg") ?
|
|
|
new JpegBitmapEncoder() : new PngBitmapEncoder();
|
|
|
encoder.Frames.Add(BitmapFrame.Create(image));
|
|
|
encoder.Save(fileStream);
|
|
|
}
|
|
|
MessageBox.Show("图片保存成功!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
MessageBox.Show($"保存失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void Image_MouseMove(object sender, MouseEventArgs e)
|
|
|
{
|
|
|
if (ViewModel.Image == null) return;
|
|
|
|
|
|
try
|
|
|
{
|
|
|
imgMat = ViewModel.Image.ToMat();
|
|
|
|
|
|
System.Windows.Point p = e.GetPosition(image);
|
|
|
if (p.X >= 0 && p.X < image.ActualWidth &&
|
|
|
p.Y >= 0 && p.Y < image.ActualHeight)
|
|
|
{
|
|
|
int x = (int)(p.X * (imgMat.Width / image.ActualWidth));
|
|
|
int y = (int)(p.Y * (imgMat.Height / image.ActualHeight));
|
|
|
|
|
|
x = Math.Max(0, Math.Min(x, imgMat.Width - 1));
|
|
|
y = Math.Max(0, Math.Min(y, imgMat.Height - 1));
|
|
|
|
|
|
Vec3b pixel = imgMat.At<Vec3b>(y, x);
|
|
|
|
|
|
Mat bgr = new Mat(1, 1, MatType.CV_8UC3, new Scalar(pixel.Item0, pixel.Item1, pixel.Item2));
|
|
|
Mat hsv = new Mat();
|
|
|
Cv2.CvtColor(bgr, hsv, ColorConversionCodes.BGR2HSV);
|
|
|
|
|
|
var hsvValues = hsv.Get<Vec3b>(0, 0);
|
|
|
|
|
|
|
|
|
|
|
|
bgr.Dispose();
|
|
|
hsv.Dispose();
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private System.Windows.Point GetScaledPosition(System.Windows.Point position, FrameworkElement element)
|
|
|
{
|
|
|
if (ViewModel.Image == null) return position;
|
|
|
|
|
|
var actualWidth = element.ActualWidth;
|
|
|
var actualHeight = element.ActualHeight;
|
|
|
var imageWidth = ViewModel.Image.PixelWidth;
|
|
|
var imageHeight = ViewModel.Image.PixelHeight;
|
|
|
|
|
|
var xRatio = imageWidth / actualWidth;
|
|
|
var yRatio = imageHeight / actualHeight;
|
|
|
|
|
|
var scaledX = position.X * xRatio;
|
|
|
var scaledY = position.Y * yRatio;
|
|
|
|
|
|
return new System.Windows.Point(scaledX, scaledY);
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ImageViewer_PreviewMouseWheel(object sender, MouseWheelEventArgs e)
|
|
|
{
|
|
|
|
|
|
if (Keyboard.Modifiers == ModifierKeys.Control)
|
|
|
{
|
|
|
|
|
|
if (e.Delta > 0)
|
|
|
{
|
|
|
|
|
|
_imageZoomFactor *= 1.1;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
|
|
|
_imageZoomFactor /= 1.1;
|
|
|
}
|
|
|
|
|
|
|
|
|
_imageZoomFactor = Math.Max(0.1, Math.Min(_imageZoomFactor, 10.0));
|
|
|
|
|
|
|
|
|
ApplyImageZoom();
|
|
|
|
|
|
|
|
|
e.Handled = true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ApplyImageZoom()
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
private void Editor_TextChanged(object sender, EventArgs e)
|
|
|
{
|
|
|
_textChanged = true;
|
|
|
UpdateTitle();
|
|
|
}
|
|
|
|
|
|
private void Editor_CursorPositionChanged(object sender, CursorPositionChangedEventArgs e)
|
|
|
{
|
|
|
CursorPositionText.Text = $"行: {e.Line + 1}, 列: {e.Column + 1}";
|
|
|
}
|
|
|
|
|
|
private void UpdateTitle()
|
|
|
{
|
|
|
string fileName = _currentFilePath != null ? Path.GetFileName(_currentFilePath) : "无标题";
|
|
|
Title = $"{fileName}{(_textChanged ? "*" : "")} - WPF Text Editor";
|
|
|
}
|
|
|
|
|
|
private void OnNewFile_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
if (_textChanged)
|
|
|
{
|
|
|
var result = MessageBox.Show("当前文件已修改,是否保存?", "保存确认", MessageBoxButton.YesNoCancel);
|
|
|
if (result == MessageBoxResult.Cancel)
|
|
|
return;
|
|
|
if (result == MessageBoxResult.Yes)
|
|
|
SaveFile();
|
|
|
}
|
|
|
|
|
|
Editor.Text = string.Empty;
|
|
|
_currentFilePath = null;
|
|
|
_textChanged = false;
|
|
|
UpdateTitle();
|
|
|
StatusText.Text = "新建文件";
|
|
|
}
|
|
|
|
|
|
|
|
|
private async void ImageListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
|
|
{
|
|
|
if (ImageListBox.SelectedItem is ImageFileInfo selectedImage)
|
|
|
{
|
|
|
string pthimg = selectedImage.FullPath;
|
|
|
|
|
|
var (jsn, m5, pth_img, pth_json) = await Global.img_md5_jsn(pthimg, _ocrMode);
|
|
|
|
|
|
|
|
|
|
|
|
string b64_str = File.ReadAllText(pth_img, Encoding.UTF8);
|
|
|
byte[] img_bytes = Base64.Base64Decode(b64_str);
|
|
|
Mat im_orig = Cv2.ImDecode(img_bytes, ImreadModes.Color);
|
|
|
BitmapSource source = TextEditorControl.MatToBitmapSource(im_orig);
|
|
|
ViewModel.Image = source;
|
|
|
ViewModel.ClearAllRectangles();
|
|
|
|
|
|
|
|
|
|
|
|
_previousImageWidth = 0;
|
|
|
_previousImageHeight = 0;
|
|
|
|
|
|
|
|
|
Editor._pthimg = pthimg;
|
|
|
Editor._jsn_OpenImg = jsn;
|
|
|
Editor._md5_OpenImg = m5;
|
|
|
|
|
|
_currentFilePath = pthimg;
|
|
|
if (this._mode == EditorMode.ImageOCR)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
|
|
|
Editor.SetText(jsn, m5);
|
|
|
|
|
|
|
|
|
_textChanged = false;
|
|
|
UpdateTitle();
|
|
|
StatusText.Text = $"已打开: {_currentFilePath}";
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
MessageBox.Show($"打开文件时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
|
}
|
|
|
}
|
|
|
else if (this._mode == EditorMode.ReviseEdit)
|
|
|
{
|
|
|
|
|
|
|
|
|
try
|
|
|
{
|
|
|
|
|
|
Editor._lines.Clear();
|
|
|
|
|
|
Editor._lines = Global.loadJsonReviseEdit_FromOriginalPath(pth_json);
|
|
|
|
|
|
|
|
|
ViewModel.ClearAllRectangles();
|
|
|
|
|
|
var dic_ = new Dictionary<string, bool>();
|
|
|
foreach (var item in Editor._lines)
|
|
|
{
|
|
|
if (item.Characters.Count == 0)
|
|
|
{
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
JObject selected_rectange = item.selected_rectange;
|
|
|
if (selected_rectange == null || !selected_rectange.HasValues)
|
|
|
{
|
|
|
continue;
|
|
|
}
|
|
|
int Id = selected_rectange["Id"].Value<int>();
|
|
|
int lft = selected_rectange["lft"].Value<int>();
|
|
|
int tp = selected_rectange["tp"].Value<int>();
|
|
|
int wdth = selected_rectange["wdth"].Value<int>();
|
|
|
int hght = selected_rectange["hght"].Value<int>();
|
|
|
|
|
|
if (!dic_.ContainsKey(Id.ToString()))
|
|
|
{
|
|
|
_pendingRectangles.Add(selected_rectange);
|
|
|
dic_[Id.ToString()] = true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
Editor._undoBuffer.Clear();
|
|
|
Editor._undoIndex = -1;
|
|
|
Editor._cursorLine = 0;
|
|
|
Editor._cursorColumn = 0;
|
|
|
Editor.ClearSelection();
|
|
|
Editor._textChanged = true;
|
|
|
Editor.InvalidateVisual();
|
|
|
|
|
|
|
|
|
Editor.OnTextChanged();
|
|
|
|
|
|
UpdateTitle();
|
|
|
StatusText.Text = $"已打开: {_currentFilePath}";
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
MessageBox.Show($"打开文件时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
else if (this._mode == EditorMode.MenuSplit)
|
|
|
{
|
|
|
if (m5 != "")
|
|
|
{
|
|
|
await split_menu_openImage(m5);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void SelectBookButton_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
|
|
|
var bookManagementWindow = new Window
|
|
|
{
|
|
|
Title = "书籍管理",
|
|
|
Width = 500,
|
|
|
Height = 600,
|
|
|
WindowStartupLocation = WindowStartupLocation.CenterOwner,
|
|
|
Owner = this,
|
|
|
ResizeMode = ResizeMode.CanResize,
|
|
|
MinWidth = 400,
|
|
|
MinHeight = 500
|
|
|
};
|
|
|
|
|
|
|
|
|
var mainPanel = new Grid
|
|
|
{
|
|
|
Margin = new Thickness(20)
|
|
|
};
|
|
|
|
|
|
|
|
|
mainPanel.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
|
|
|
mainPanel.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
|
|
|
mainPanel.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
|
|
|
mainPanel.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
|
|
|
mainPanel.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
|
|
|
|
|
|
|
|
|
var titleText = new TextBlock
|
|
|
{
|
|
|
Text = "书籍管理",
|
|
|
FontSize = 16,
|
|
|
FontWeight = FontWeights.Bold,
|
|
|
HorizontalAlignment = HorizontalAlignment.Center,
|
|
|
Margin = new Thickness(0, 0, 0, 20)
|
|
|
};
|
|
|
Grid.SetRow(titleText, 0);
|
|
|
mainPanel.Children.Add(titleText);
|
|
|
|
|
|
|
|
|
var buttonPanel = new StackPanel
|
|
|
{
|
|
|
Orientation = Orientation.Horizontal,
|
|
|
HorizontalAlignment = HorizontalAlignment.Center,
|
|
|
Margin = new Thickness(0, 0, 0, 20)
|
|
|
};
|
|
|
|
|
|
|
|
|
var addBookButton = new Button
|
|
|
{
|
|
|
Content = "添加书籍",
|
|
|
Width = 100,
|
|
|
Height = 30,
|
|
|
Margin = new Thickness(5),
|
|
|
|
|
|
|
|
|
BorderThickness = new Thickness(0),
|
|
|
FontSize = 12
|
|
|
};
|
|
|
|
|
|
|
|
|
var deleteBookButton = new Button
|
|
|
{
|
|
|
Content = "删除书籍",
|
|
|
Width = 100,
|
|
|
Height = 30,
|
|
|
Margin = new Thickness(5),
|
|
|
|
|
|
|
|
|
BorderThickness = new Thickness(0),
|
|
|
FontSize = 12
|
|
|
};
|
|
|
|
|
|
buttonPanel.Children.Add(addBookButton);
|
|
|
buttonPanel.Children.Add(deleteBookButton);
|
|
|
Grid.SetRow(buttonPanel, 1);
|
|
|
mainPanel.Children.Add(buttonPanel);
|
|
|
|
|
|
|
|
|
var searchPanel = new StackPanel
|
|
|
{
|
|
|
Orientation = Orientation.Horizontal,
|
|
|
Margin = new Thickness(0, 0, 0, 10)
|
|
|
};
|
|
|
|
|
|
var searchLabel = new TextBlock
|
|
|
{
|
|
|
Text = "搜索书籍:",
|
|
|
VerticalAlignment = VerticalAlignment.Center,
|
|
|
Margin = new Thickness(0, 0, 10, 0),
|
|
|
FontSize = 12
|
|
|
};
|
|
|
|
|
|
var searchTextBox = new TextBox
|
|
|
{
|
|
|
Width = 200,
|
|
|
Height = 25,
|
|
|
VerticalAlignment = VerticalAlignment.Center,
|
|
|
FontSize = 12
|
|
|
};
|
|
|
|
|
|
var searchButton = new Button
|
|
|
{
|
|
|
Content = "搜索",
|
|
|
Width = 60,
|
|
|
Height = 25,
|
|
|
Margin = new Thickness(10, 0, 0, 0),
|
|
|
|
|
|
|
|
|
BorderThickness = new Thickness(0),
|
|
|
FontSize = 12
|
|
|
};
|
|
|
|
|
|
searchPanel.Children.Add(searchLabel);
|
|
|
searchPanel.Children.Add(searchTextBox);
|
|
|
searchPanel.Children.Add(searchButton);
|
|
|
Grid.SetRow(searchPanel, 2);
|
|
|
mainPanel.Children.Add(searchPanel);
|
|
|
|
|
|
|
|
|
var bookListBox = new ListBox
|
|
|
{
|
|
|
Margin = new Thickness(0, 0, 0, 20),
|
|
|
BorderBrush = new SolidColorBrush(System.Windows.Media.Color.FromRgb(0xCC, 0xCC, 0xCC)),
|
|
|
BorderThickness = new Thickness(1)
|
|
|
};
|
|
|
|
|
|
|
|
|
var allBooks = new List<string>
|
|
|
{
|
|
|
"《红楼梦》- 曹雪芹",
|
|
|
"《西游记》- 吴承恩",
|
|
|
"《水浒传》- 施耐庵",
|
|
|
"《三国演义》- 罗贯中"
|
|
|
};
|
|
|
|
|
|
|
|
|
foreach (var book in allBooks)
|
|
|
{
|
|
|
bookListBox.Items.Add(book);
|
|
|
}
|
|
|
|
|
|
Grid.SetRow(bookListBox, 3);
|
|
|
mainPanel.Children.Add(bookListBox);
|
|
|
|
|
|
|
|
|
searchButton.Click += (s, args) =>
|
|
|
{
|
|
|
var searchText = searchTextBox.Text.Trim().ToLower();
|
|
|
bookListBox.Items.Clear();
|
|
|
|
|
|
if (string.IsNullOrEmpty(searchText))
|
|
|
{
|
|
|
|
|
|
foreach (var book in allBooks)
|
|
|
{
|
|
|
bookListBox.Items.Add(book);
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
|
|
|
var filteredBooks = allBooks.Where(book =>
|
|
|
book.ToLower().Contains(searchText)).ToList();
|
|
|
|
|
|
foreach (var book in filteredBooks)
|
|
|
{
|
|
|
bookListBox.Items.Add(book);
|
|
|
}
|
|
|
|
|
|
if (filteredBooks.Count == 0)
|
|
|
{
|
|
|
bookListBox.Items.Add("未找到匹配的书籍");
|
|
|
}
|
|
|
}
|
|
|
};
|
|
|
|
|
|
|
|
|
searchTextBox.KeyDown += (s, args) =>
|
|
|
{
|
|
|
if (args.Key == Key.Enter)
|
|
|
{
|
|
|
searchButton.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
|
|
|
}
|
|
|
};
|
|
|
|
|
|
|
|
|
var bottomPanel = new StackPanel
|
|
|
{
|
|
|
Orientation = Orientation.Horizontal,
|
|
|
HorizontalAlignment = HorizontalAlignment.Center
|
|
|
};
|
|
|
|
|
|
|
|
|
var selectBookButton = new Button
|
|
|
{
|
|
|
Content = "选择此书籍",
|
|
|
Width = 100,
|
|
|
Height = 35,
|
|
|
Margin = new Thickness(5),
|
|
|
Background = new SolidColorBrush(System.Windows.Media.Color.FromRgb(0xFF, 0x98, 0x00)),
|
|
|
BorderThickness = new Thickness(0),
|
|
|
FontSize = 12
|
|
|
};
|
|
|
|
|
|
|
|
|
var closeButton = new Button
|
|
|
{
|
|
|
Content = "关闭",
|
|
|
Width = 100,
|
|
|
Height = 35,
|
|
|
Margin = new Thickness(5),
|
|
|
Background = new SolidColorBrush(System.Windows.Media.Color.FromRgb(0x95, 0x95, 0x95)),
|
|
|
BorderThickness = new Thickness(0),
|
|
|
FontSize = 12
|
|
|
};
|
|
|
|
|
|
bottomPanel.Children.Add(selectBookButton);
|
|
|
bottomPanel.Children.Add(closeButton);
|
|
|
Grid.SetRow(bottomPanel, 4);
|
|
|
mainPanel.Children.Add(bottomPanel);
|
|
|
|
|
|
|
|
|
addBookButton.Click += (s, args) =>
|
|
|
{
|
|
|
var inputDialog = new Window
|
|
|
{
|
|
|
Title = "添加书籍",
|
|
|
Width = 300,
|
|
|
Height = 180,
|
|
|
WindowStartupLocation = WindowStartupLocation.CenterOwner,
|
|
|
Owner = bookManagementWindow,
|
|
|
ResizeMode = ResizeMode.NoResize
|
|
|
};
|
|
|
|
|
|
var inputPanel = new StackPanel { Margin = new Thickness(20) };
|
|
|
var inputLabel = new TextBlock { Text = "请输入书籍名称:", Margin = new Thickness(0, 0, 0, 10) };
|
|
|
var inputTextBox = new TextBox { Height = 25, Margin = new Thickness(0, 0, 0, 15) };
|
|
|
var inputButtonPanel = new StackPanel {
|
|
|
Orientation = Orientation.Horizontal,
|
|
|
HorizontalAlignment = HorizontalAlignment.Center,
|
|
|
Margin = new Thickness(0, 10, 0, 0)
|
|
|
};
|
|
|
|
|
|
var confirmButton = new Button {
|
|
|
Content = "确定",
|
|
|
Width = 60,
|
|
|
Height = 25,
|
|
|
Margin = new Thickness(5),
|
|
|
Background = new SolidColorBrush(System.Windows.Media.Color.FromRgb(0x4C, 0xAF, 0x50)),
|
|
|
|
|
|
BorderThickness = new Thickness(0)
|
|
|
};
|
|
|
var cancelButton = new Button {
|
|
|
Content = "取消",
|
|
|
Width = 60,
|
|
|
Height = 25,
|
|
|
Margin = new Thickness(5),
|
|
|
Background = new SolidColorBrush(System.Windows.Media.Color.FromRgb(0x95, 0x95, 0x95)),
|
|
|
|
|
|
BorderThickness = new Thickness(0)
|
|
|
};
|
|
|
|
|
|
confirmButton.Click += (ss, ee) =>
|
|
|
{
|
|
|
var newBook = inputTextBox.Text.Trim();
|
|
|
if (!string.IsNullOrEmpty(newBook))
|
|
|
{
|
|
|
allBooks.Add(newBook);
|
|
|
bookListBox.Items.Add(newBook);
|
|
|
inputDialog.Close();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
MessageBox.Show("请输入书籍名称", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
|
|
|
}
|
|
|
};
|
|
|
cancelButton.Click += (ss, ee) => inputDialog.Close();
|
|
|
|
|
|
inputButtonPanel.Children.Add(confirmButton);
|
|
|
inputButtonPanel.Children.Add(cancelButton);
|
|
|
inputPanel.Children.Add(inputLabel);
|
|
|
inputPanel.Children.Add(inputTextBox);
|
|
|
inputPanel.Children.Add(inputButtonPanel);
|
|
|
inputDialog.Content = inputPanel;
|
|
|
inputDialog.ShowDialog();
|
|
|
};
|
|
|
|
|
|
deleteBookButton.Click += (s, args) =>
|
|
|
{
|
|
|
if (bookListBox.SelectedItem != null)
|
|
|
{
|
|
|
var selectedBook = bookListBox.SelectedItem.ToString();
|
|
|
if (MessageBox.Show($"确认删除书籍 { selectedBook} ?", "确认删除",
|
|
|
MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
|
|
|
{
|
|
|
allBooks.Remove(selectedBook);
|
|
|
bookListBox.Items.Remove(selectedBook);
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
MessageBox.Show("请先选择要删除的书籍", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
selectBookButton.Click += (s, args) =>
|
|
|
{
|
|
|
if (bookListBox.SelectedItem != null)
|
|
|
{
|
|
|
var selectedBook = bookListBox.SelectedItem.ToString();
|
|
|
MessageBox.Show($"已选择书籍:{selectedBook}", "选择成功", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
bookManagementWindow.Close();
|
|
|
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
MessageBox.Show("请先选择一本书籍", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
}
|
|
|
};
|
|
|
|
|
|
closeButton.Click += (s, args) => bookManagementWindow.Close();
|
|
|
|
|
|
bookManagementWindow.Content = mainPanel;
|
|
|
bookManagementWindow.ShowDialog();
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ToggleImageListButton_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
_isImageListVisible = !_isImageListVisible;
|
|
|
|
|
|
if (_isImageListVisible)
|
|
|
{
|
|
|
|
|
|
ImageListBox.Visibility = Visibility.Visible;
|
|
|
ImageListTitle.Visibility = Visibility.Visible;
|
|
|
ToggleImageListButton.Content = "▼";
|
|
|
ToggleImageListButton.ToolTip = "隐藏图片列表";
|
|
|
|
|
|
|
|
|
MainContentGrid.ColumnDefinitions[0].Width = new GridLength(200, GridUnitType.Pixel);
|
|
|
MainContentGrid.ColumnDefinitions[0].MinWidth = 150;
|
|
|
|
|
|
|
|
|
ImageListBorder.BorderThickness = new Thickness(0, 0, 1, 0);
|
|
|
ImageListBorder.Padding = new Thickness(0);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
|
|
|
ImageListBox.Visibility = Visibility.Collapsed;
|
|
|
ImageListTitle.Visibility = Visibility.Collapsed;
|
|
|
ToggleImageListButton.Content = "▶";
|
|
|
ToggleImageListButton.ToolTip = "显示图片列表";
|
|
|
|
|
|
|
|
|
MainContentGrid.ColumnDefinitions[0].Width = new GridLength(30, GridUnitType.Pixel);
|
|
|
MainContentGrid.ColumnDefinitions[0].MinWidth = 30;
|
|
|
|
|
|
|
|
|
ImageListBorder.BorderThickness = new Thickness(0, 0, 1, 0);
|
|
|
ImageListBorder.Padding = new Thickness(5, 10, 5, 0);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public async void handle_comicocr_open_img(string img_pth)
|
|
|
{
|
|
|
var (jsn, m5, pth_img, pth_json) = await Global.img_md5_jsn(img_pth, _ocrMode);
|
|
|
|
|
|
ImageListBox.Items.Add(new ImageFileInfo(img_pth));
|
|
|
|
|
|
|
|
|
string b64_str = File.ReadAllText(pth_img, Encoding.UTF8);
|
|
|
byte[] img_bytes = Base64.Base64Decode(b64_str);
|
|
|
Mat im_orig = Cv2.ImDecode(img_bytes, ImreadModes.Color);
|
|
|
BitmapSource source = TextEditorControl.MatToBitmapSource(im_orig);
|
|
|
ViewModel.Image = source;
|
|
|
ViewModel.ClearAllRectangles();
|
|
|
|
|
|
|
|
|
|
|
|
_previousImageWidth = 0;
|
|
|
_previousImageHeight = 0;
|
|
|
|
|
|
|
|
|
Editor._pthimg = img_pth;
|
|
|
Editor._jsn_OpenImg = jsn;
|
|
|
Editor._md5_OpenImg = m5;
|
|
|
|
|
|
_currentFilePath = img_pth;
|
|
|
|
|
|
try
|
|
|
{
|
|
|
|
|
|
Editor.SetText(jsn, m5);
|
|
|
|
|
|
_textChanged = false;
|
|
|
UpdateTitle();
|
|
|
StatusText.Text = $"已打开: {_currentFilePath}";
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
MessageBox.Show($"打开文件时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private async void OnOpenFile_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
if (_textChanged)
|
|
|
{
|
|
|
var result = MessageBox.Show("当前文件已修改,是否保存?", "保存确认", MessageBoxButton.YesNoCancel);
|
|
|
if (result == MessageBoxResult.Cancel)
|
|
|
return;
|
|
|
if (result == MessageBoxResult.Yes)
|
|
|
_textChanged = false;
|
|
|
SaveFile();
|
|
|
}
|
|
|
|
|
|
OpenFileDialog openFileDialog = new OpenFileDialog();
|
|
|
|
|
|
openFileDialog.Filter = "图片文件 (*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp|PDF文件 (*.pdf)|(*.pdf)|所有文件 (*.*)|*.*";
|
|
|
openFileDialog.Multiselect = true;
|
|
|
|
|
|
if (openFileDialog.ShowDialog() == true)
|
|
|
{
|
|
|
if ( openFileDialog.FileName.Contains(".txt") )
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
|
|
|
Editor.SetText(File.ReadAllText(openFileDialog.FileName));
|
|
|
_currentFilePath = openFileDialog.FileName;
|
|
|
_textChanged = false;
|
|
|
UpdateTitle();
|
|
|
StatusText.Text = $"已打开: {_currentFilePath}";
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
MessageBox.Show($"打开文件时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
|
}
|
|
|
}
|
|
|
else if (openFileDialog.FileName.Contains(".pdf"))
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
string pth_pdf = openFileDialog.FileName;
|
|
|
|
|
|
|
|
|
string pdfDirectory = Path.GetDirectoryName(pth_pdf);
|
|
|
|
|
|
|
|
|
var jpgFiles = Directory.GetFiles(pdfDirectory, "*.jpg", SearchOption.TopDirectoryOnly)
|
|
|
.Concat(Directory.GetFiles(pdfDirectory, "*.jpeg", SearchOption.TopDirectoryOnly))
|
|
|
.OrderBy(f => f)
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
ImageListBox.Items.Clear();
|
|
|
|
|
|
|
|
|
foreach (string jpgFile in jpgFiles)
|
|
|
{
|
|
|
ImageListBox.Items.Add(new ImageFileInfo(jpgFile));
|
|
|
}
|
|
|
|
|
|
|
|
|
StatusText.Text = $"已加载PDF文件及同目录下的 {jpgFiles.Count} 个JPG图片";
|
|
|
|
|
|
|
|
|
_currentFilePath = pth_pdf;
|
|
|
UpdateTitle();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
var (jsn, m5, pth_img, pth_json) = await Global.img_md5_jsn(openFileDialog.FileName, _ocrMode);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach (string imagePath in openFileDialog.FileNames)
|
|
|
{
|
|
|
bool exist = false;
|
|
|
foreach(ImageFileInfo item in ImageListBox.Items )
|
|
|
{
|
|
|
if (item.FullPath == imagePath)
|
|
|
{
|
|
|
exist = true;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
if (!exist)
|
|
|
{
|
|
|
ImageListBox.Items.Add(new ImageFileInfo(imagePath));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
string b64_str = File.ReadAllText(pth_img, Encoding.UTF8);
|
|
|
byte[] img_bytes = Base64.Base64Decode(b64_str);
|
|
|
Mat im_orig = Cv2.ImDecode(img_bytes, ImreadModes.Color);
|
|
|
BitmapSource source = TextEditorControl.MatToBitmapSource(im_orig);
|
|
|
ViewModel.Image = source;
|
|
|
ViewModel.ClearAllRectangles();
|
|
|
|
|
|
|
|
|
|
|
|
_previousImageWidth = 0;
|
|
|
_previousImageHeight = 0;
|
|
|
|
|
|
|
|
|
Editor._pthimg = openFileDialog.FileName;
|
|
|
Editor._jsn_OpenImg = jsn;
|
|
|
Editor._md5_OpenImg = m5;
|
|
|
|
|
|
_currentFilePath = openFileDialog.FileName;
|
|
|
if (this._mode == EditorMode.ImageOCR)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
|
|
|
Editor.SetText(jsn, m5);
|
|
|
|
|
|
|
|
|
_textChanged = false;
|
|
|
UpdateTitle();
|
|
|
StatusText.Text = $"已打开: {_currentFilePath}";
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
MessageBox.Show($"打开文件时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
|
}
|
|
|
}
|
|
|
else if (this._mode == EditorMode.ReviseEdit)
|
|
|
{
|
|
|
|
|
|
|
|
|
try
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Editor._lines.Clear();
|
|
|
|
|
|
Editor._lines = Global.loadJsonReviseEdit_FromOriginalPath(pth_json);
|
|
|
|
|
|
|
|
|
ViewModel.ClearAllRectangles();
|
|
|
|
|
|
|
|
|
var dic_ = new Dictionary<string, bool>();
|
|
|
foreach (var item in Editor._lines)
|
|
|
{
|
|
|
if (item.Characters.Count == 0)
|
|
|
{
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
JObject selected_rectange = item.selected_rectange;
|
|
|
if (selected_rectange == null || !selected_rectange.HasValues)
|
|
|
{
|
|
|
continue;
|
|
|
}
|
|
|
int Id = selected_rectange["Id"].Value<int>();
|
|
|
int lft = selected_rectange["lft"].Value<int>();
|
|
|
int tp = selected_rectange["tp"].Value<int>();
|
|
|
int wdth = selected_rectange["wdth"].Value<int>();
|
|
|
int hght = selected_rectange["hght"].Value<int>();
|
|
|
|
|
|
if (!dic_.ContainsKey(Id.ToString()))
|
|
|
{
|
|
|
_pendingRectangles.Add(selected_rectange);
|
|
|
dic_[Id.ToString()] = true;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
Editor._undoBuffer.Clear();
|
|
|
Editor._undoIndex = -1;
|
|
|
Editor._cursorLine = 0;
|
|
|
Editor._cursorColumn = 0;
|
|
|
Editor.ClearSelection();
|
|
|
Editor._textChanged = true;
|
|
|
Editor.InvalidateVisual();
|
|
|
|
|
|
|
|
|
Editor.OnTextChanged();
|
|
|
|
|
|
UpdateTitle();
|
|
|
StatusText.Text = $"已打开: {_currentFilePath}";
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
MessageBox.Show($"打开文件时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
|
}
|
|
|
|
|
|
} else if (this._mode == EditorMode.MenuSplit) {
|
|
|
|
|
|
Editor._lines.Clear();
|
|
|
|
|
|
Editor._lines = Global.loadJsonReviseEdit_FromOriginalPath(pth_json);
|
|
|
|
|
|
string dir = Path.GetDirectoryName(_currentFilePath);
|
|
|
|
|
|
var jpgs = Directory.EnumerateFiles(dir, "*.jpg", SearchOption.TopDirectoryOnly)
|
|
|
.Where(file => System.Text.RegularExpressions.Regex.IsMatch(Path.GetFileNameWithoutExtension(file), @"^\d+$"))
|
|
|
.OrderBy(f => f)
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
List<TextLine> ls = new List<TextLine>();
|
|
|
foreach (var pth in jpgs)
|
|
|
{
|
|
|
var (jsn_, m5_, pth_img_, pth_json_) = await Global.img_md5_jsn(pth, _ocrMode, false);
|
|
|
|
|
|
if (Path.Exists(pth_json_))
|
|
|
{
|
|
|
if (!_dic_m5_imgname.ContainsKey(m5_))
|
|
|
{
|
|
|
_dic_m5_imgname[m5_] = pth;
|
|
|
}
|
|
|
|
|
|
List<TextLine> lines = Global.loadJsonReviseEdit_FromOriginalPath(pth_json_);
|
|
|
if (lines.Count > 0)
|
|
|
{
|
|
|
ls.AddRange(lines);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
InitializeDirectoryTreeData(ls);
|
|
|
|
|
|
Editor._undoBuffer.Clear();
|
|
|
Editor._undoIndex = -1;
|
|
|
Editor._cursorLine = 0;
|
|
|
Editor._cursorColumn = 0;
|
|
|
Editor.ClearSelection();
|
|
|
Editor._textChanged = true;
|
|
|
Editor.InvalidateVisual();
|
|
|
|
|
|
|
|
|
Editor.OnTextChanged();
|
|
|
|
|
|
UpdateTitle();
|
|
|
StatusText.Text = $"已打开: {_currentFilePath}";
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void export_mdx(List<DirectoryTreeNode> ls, string parent_guid, List<Dictionary<string, string>> ls_info)
|
|
|
{
|
|
|
if (ls == null || ls.Count <= 0)
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
foreach (DirectoryTreeNode tnode in ls)
|
|
|
{
|
|
|
string Name = tnode.Name;
|
|
|
int level = tnode.level;
|
|
|
List<DirectoryTreeNode> Children = tnode.Children;
|
|
|
TextLine line = tnode.line;
|
|
|
|
|
|
Dictionary<string, string> info = new Dictionary<string, string>();
|
|
|
|
|
|
foreach (TextCharacter textcharacter in line.Characters)
|
|
|
{
|
|
|
string ImageMD5 = textcharacter.ImageMD5;
|
|
|
if ( !_dic_m5_imgname.ContainsKey(ImageMD5) )
|
|
|
{
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
string pth = _dic_m5_imgname[ImageMD5];
|
|
|
string baseName = Path.GetFileNameWithoutExtension(pth);
|
|
|
string ext = Path.GetExtension(pth);
|
|
|
string dir = Path.GetDirectoryName(pth);
|
|
|
|
|
|
info["Guid"] = Guid.NewGuid().ToString();
|
|
|
info["parent_guid"] = parent_guid;
|
|
|
info["Name"] = Name;
|
|
|
info["level"] = level.ToString();
|
|
|
info["ImagebaseName"] = baseName;
|
|
|
info["ImageName"] = $"{baseName}{ext}";
|
|
|
info["ImageFullPath"] = pth;
|
|
|
|
|
|
|
|
|
ls_info.Add(info);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (info.Count <= 0)
|
|
|
{
|
|
|
throw new Exception("Error: something wrong.");
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export_mdx(Children, info["Guid"], ls_info);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
private void OnExportMDX_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (Editor._mode != EditorMode.MenuSplit)
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
List<Dictionary<string, string>> ls_info = new List<Dictionary<string, string>>();
|
|
|
|
|
|
var rootNodes = (List<DirectoryTreeNode>)DirectoryTreeView.ItemsSource;
|
|
|
|
|
|
export_mdx(rootNodes, "", ls_info);
|
|
|
|
|
|
string prefix_Name = "TIMESDT1K";
|
|
|
|
|
|
string dest = "E:\\";
|
|
|
string mdx_dir_name = "Times单挑1000";
|
|
|
string mdx_dir = Path.Join(dest, mdx_dir_name);
|
|
|
string mdx_pth = Path.Join(mdx_dir, $"{mdx_dir_name}.mdx.txt");
|
|
|
string mdd_dir = Path.Join(dest, mdx_dir_name, "mdd");
|
|
|
|
|
|
if (Directory.Exists(mdd_dir))
|
|
|
{
|
|
|
Directory.Delete(mdd_dir, true);
|
|
|
}
|
|
|
|
|
|
if (!Directory.Exists(mdd_dir))
|
|
|
{
|
|
|
DirectoryInfo di = Directory.CreateDirectory(mdd_dir);
|
|
|
if (!Directory.Exists(mdd_dir))
|
|
|
{
|
|
|
throw new Exception($"创健目录失败:{mdd_dir}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
string last_ImageBaseName_new = "";
|
|
|
string last_ImageName_new = "";
|
|
|
|
|
|
List<string> mdx_items_pic = new List<string>();
|
|
|
List<string> mdx_items_word = new List<string>();
|
|
|
Dictionary<string, bool> dic_key = new Dictionary<string, bool>();
|
|
|
|
|
|
Dictionary<string, Dictionary<string, string>> dic_guid_info = new Dictionary<string, Dictionary<string, string>>();
|
|
|
|
|
|
for (int i = 0; i < ls_info.Count; i++)
|
|
|
{
|
|
|
var info = ls_info[i];
|
|
|
|
|
|
string Guid = info["Guid"];
|
|
|
string parent_guid = info["parent_guid"];
|
|
|
|
|
|
string Name = info["Name"];
|
|
|
string level = info["level"];
|
|
|
string ImagebaseName = info["ImagebaseName"];
|
|
|
int Page = int.Parse(info["ImagebaseName"]);
|
|
|
string ImageName = info["ImageName"];
|
|
|
string ImageFullPath = info["ImageFullPath"];
|
|
|
|
|
|
string next_ImageBaseName_new = "";
|
|
|
|
|
|
|
|
|
string ImageBaseName_new = $"{prefix_Name}{ImagebaseName}";
|
|
|
string ImageName_new = $"{prefix_Name}{ImageName}";
|
|
|
|
|
|
next_ImageBaseName_new = ImageBaseName_new;
|
|
|
|
|
|
for (int j = i + 1; j < ls_info.Count; j++)
|
|
|
{
|
|
|
int p = int.Parse(ls_info[j]["ImagebaseName"]);
|
|
|
if (p > Page)
|
|
|
{
|
|
|
next_ImageBaseName_new = $"{prefix_Name}{ls_info[j]["ImagebaseName"]}";
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
if (last_ImageBaseName_new == "")
|
|
|
{
|
|
|
last_ImageBaseName_new = ImageBaseName_new;
|
|
|
}
|
|
|
|
|
|
if (last_ImageName_new == "")
|
|
|
{
|
|
|
last_ImageName_new = ImageName_new;
|
|
|
}
|
|
|
|
|
|
info["ImageBaseName_new"] = ImageBaseName_new;
|
|
|
info["ImageName_new"] = ImageName_new;
|
|
|
info["last_ImageBaseName_new"] = last_ImageBaseName_new;
|
|
|
info["next_ImageBaseName_new"] = next_ImageBaseName_new;
|
|
|
|
|
|
|
|
|
if ( !dic_guid_info.ContainsKey(Guid) )
|
|
|
{
|
|
|
dic_guid_info[Guid] = info;
|
|
|
}
|
|
|
|
|
|
|
|
|
List<Dictionary<string, string>> parent_all = new List<Dictionary<string, string>>();
|
|
|
while(parent_guid != "")
|
|
|
{
|
|
|
if (!dic_guid_info.ContainsKey(parent_guid))
|
|
|
{
|
|
|
throw new Exception("Error: something wrong.");
|
|
|
}
|
|
|
|
|
|
var parent_info = dic_guid_info[parent_guid];
|
|
|
parent_guid = parent_info["parent_guid"];
|
|
|
|
|
|
parent_all.Add(parent_info);
|
|
|
|
|
|
}
|
|
|
|
|
|
List<string> nav_ls = new List<string>();
|
|
|
|
|
|
string nav_string = "";
|
|
|
|
|
|
nav_ls.Insert(0, $"<a href=\"entry://{ImageBaseName_new}\">{Name}</a>");
|
|
|
|
|
|
foreach (var item in parent_all)
|
|
|
{
|
|
|
string parent_Name = item["Name"];
|
|
|
string parent_ImageBaseName_new = item["ImageBaseName_new"];
|
|
|
string parent_nav_string = $"<a href=\"entry://{parent_ImageBaseName_new}\">{parent_Name}</a>";
|
|
|
nav_ls.Insert(0, parent_nav_string);
|
|
|
}
|
|
|
|
|
|
nav_string = $"<center>{string.Join(" > ", nav_ls)}</center><br>";
|
|
|
|
|
|
if (!dic_key.ContainsKey(ImageBaseName_new))
|
|
|
{
|
|
|
dic_key[ImageBaseName_new] = true;
|
|
|
|
|
|
string mdx_item = $"{ImageBaseName_new}\n{nav_string}<img src=\"{ImageName_new}\" width=\"100%\"><br><center> <a href=\"entry://{last_ImageBaseName_new}\">上一页</a> <a href=\"entry://{next_ImageBaseName_new}\">下一页</a> <br><br></center>\n</>";
|
|
|
mdx_items_pic.Add(mdx_item);
|
|
|
|
|
|
string dst_img = Path.Join(mdd_dir, ImageName_new);
|
|
|
|
|
|
File.Copy(ImageFullPath, dst_img, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (level == "999")
|
|
|
{
|
|
|
string mdx_item = $"{Name}\n{nav_string}<img src=\"{ImageName_new}\" width=\"100%\"><br><center> <a href=\"entry://{last_ImageBaseName_new}\">上一页</a> <a href=\"entry://{next_ImageBaseName_new}\">下一页</a> <br><br></center>\n</>";
|
|
|
mdx_items_word.Add(mdx_item);
|
|
|
}
|
|
|
|
|
|
|
|
|
last_ImageBaseName_new = ImageBaseName_new;
|
|
|
last_ImageName_new = ImageName_new;
|
|
|
}
|
|
|
|
|
|
mdx_items_pic.AddRange(mdx_items_word);
|
|
|
|
|
|
string mdx_string = string.Join("\n", mdx_items_pic);
|
|
|
|
|
|
File.WriteAllText(mdx_pth, mdx_string, new UTF8Encoding(false));
|
|
|
|
|
|
}
|
|
|
private void InitializeDirectoryTreeData(List<TextLine> lines)
|
|
|
{
|
|
|
if (lines.Count <= 0) return;
|
|
|
|
|
|
List<Tuple<int, string, TextLine>> ls = new List<Tuple<int, string, TextLine>>();
|
|
|
|
|
|
List<DirectoryTreeNode> tree_ls = new List<DirectoryTreeNode>();
|
|
|
|
|
|
foreach (var line in lines)
|
|
|
{
|
|
|
List<string> leading = new List<string>();
|
|
|
bool appear = false;
|
|
|
bool appear2 = false;
|
|
|
|
|
|
foreach (TextCharacter textcharacter in line.Characters)
|
|
|
{
|
|
|
System.Text.Rune character = textcharacter.Character;
|
|
|
|
|
|
if (character.Value == '#')
|
|
|
{
|
|
|
if (!appear)
|
|
|
{
|
|
|
appear = true;
|
|
|
}
|
|
|
|
|
|
leading.Add("#");
|
|
|
|
|
|
} else if (character.Value == '*')
|
|
|
{
|
|
|
if (!appear2)
|
|
|
{
|
|
|
appear2 = true;
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
if (appear)
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
if (character.Value != ' ')
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (appear)
|
|
|
{
|
|
|
int level = leading.Count;
|
|
|
string lineText = string.Concat(line.Characters.Select(tc => tc.Character.ToString()));
|
|
|
Tuple<int, string, TextLine> node = new Tuple<int, string, TextLine>(level, lineText, line);
|
|
|
ls.Add(node);
|
|
|
|
|
|
string Name = lineText.Replace("#", "").Trim();
|
|
|
|
|
|
DirectoryTreeNode tnode = new DirectoryTreeNode(Name, level, line);
|
|
|
tree_ls.Add(tnode);
|
|
|
} else if (appear2)
|
|
|
{
|
|
|
int level = 999;
|
|
|
string lineText = string.Concat(line.Characters.Select(tc => tc.Character.ToString()));
|
|
|
Tuple<int, string, TextLine> node = new Tuple<int, string, TextLine>(level, lineText, line);
|
|
|
ls.Add(node);
|
|
|
|
|
|
string[] Names = lineText.Replace("*", "").Trim().Split(' ');
|
|
|
string Name = "";
|
|
|
if (Names.Length > 0)
|
|
|
{
|
|
|
Name = Names[0];
|
|
|
} else
|
|
|
{
|
|
|
Name = lineText.Replace("*", "").Trim();
|
|
|
}
|
|
|
|
|
|
DirectoryTreeNode tnode = new DirectoryTreeNode(Name, level, line);
|
|
|
tree_ls.Add(tnode);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
for (int i = 0; i < tree_ls.Count; i++)
|
|
|
{
|
|
|
|
|
|
DirectoryTreeNode cur = tree_ls[i];
|
|
|
|
|
|
for (int j = i - 1; j >= 0; j--)
|
|
|
{
|
|
|
DirectoryTreeNode pre = tree_ls[j];
|
|
|
|
|
|
if (pre.level < cur.level)
|
|
|
{
|
|
|
pre.Children.Add(cur);
|
|
|
pre.IsDirectory = true;
|
|
|
pre.Icon = "📁";
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
var rootNodes = new List<DirectoryTreeNode>();
|
|
|
|
|
|
foreach(var node in tree_ls)
|
|
|
{
|
|
|
if (node.level == 1)
|
|
|
{
|
|
|
rootNodes.Add(node);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
DirectoryTreeView.ItemsSource = rootNodes;
|
|
|
}
|
|
|
|
|
|
|
|
|
private void OnSaveFile_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
SaveFile();
|
|
|
}
|
|
|
|
|
|
private void OnSaveAsFile_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
SaveFileAs();
|
|
|
}
|
|
|
|
|
|
private void OnExit_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
Close();
|
|
|
}
|
|
|
|
|
|
private void ToggleLineNumbers_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
var menuItem = sender as MenuItem;
|
|
|
Editor.LineNumbersVisible = menuItem.IsChecked;
|
|
|
}
|
|
|
|
|
|
private void ToggleWhitespaces_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
var menuItem = sender as MenuItem;
|
|
|
Editor.ShowWhitespaces = menuItem.IsChecked;
|
|
|
}
|
|
|
|
|
|
private void ToggleHighlightCurrentLine_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
var menuItem = sender as MenuItem;
|
|
|
Editor.HighlightCurrentLine = menuItem.IsChecked;
|
|
|
}
|
|
|
|
|
|
private void OnRecognitionMethod_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
if (sender is MenuItem clickedItem && clickedItem.Parent is MenuItem parentMenu)
|
|
|
{
|
|
|
|
|
|
foreach (MenuItem item in parentMenu.Items)
|
|
|
{
|
|
|
if (item.IsCheckable)
|
|
|
{
|
|
|
item.IsChecked = false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
clickedItem.IsChecked = true;
|
|
|
|
|
|
|
|
|
string method = clickedItem.Tag?.ToString();
|
|
|
|
|
|
|
|
|
HandleRecognitionMethodChange(method);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void HandleRecognitionMethodChange(string method)
|
|
|
{
|
|
|
switch (method)
|
|
|
{
|
|
|
case "wechat":
|
|
|
|
|
|
StatusText.Text = "当前识别方法:微信OCR";
|
|
|
_ocrMode = OcrMode.WeChatOcr;
|
|
|
break;
|
|
|
case "comicocr":
|
|
|
|
|
|
StatusText.Text = "当前识别方法:ComicOcr";
|
|
|
_ocrMode = OcrMode.ComicOcr;
|
|
|
break;
|
|
|
case "ppocr":
|
|
|
|
|
|
StatusText.Text = "当前识别方法:PaddleOCR";
|
|
|
_ocrMode = OcrMode.PPOCR;
|
|
|
break;
|
|
|
case "aliocr":
|
|
|
|
|
|
StatusText.Text = "当前识别方法:阿里OCR";
|
|
|
_ocrMode = OcrMode.AliOCR;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private bool SaveFile()
|
|
|
{
|
|
|
if (_currentFilePath == null)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
if (_currentFilePath != null)
|
|
|
{
|
|
|
return Editor.saveJson(_currentFilePath);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
private bool SaveFileAs()
|
|
|
{
|
|
|
SaveFileDialog saveFileDialog = new SaveFileDialog();
|
|
|
saveFileDialog.Filter = "文本文件 (*.txt)|*.txt|C++ 文件 (*.cpp;*.h)|*.cpp;*.h|所有文件 (*.*)|*.*";
|
|
|
if (saveFileDialog.ShowDialog() == true)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
File.WriteAllText(saveFileDialog.FileName, Editor.Text);
|
|
|
_currentFilePath = saveFileDialog.FileName;
|
|
|
_textChanged = false;
|
|
|
UpdateTitle();
|
|
|
StatusText.Text = $"已保存: {_currentFilePath}";
|
|
|
return true;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
MessageBox.Show($"保存文件时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
|
}
|
|
|
}
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
|
|
|
{
|
|
|
if (_textChanged)
|
|
|
{
|
|
|
if (_mode == EditorMode.ImageOCR || _mode == EditorMode.ReviseEdit)
|
|
|
{
|
|
|
var result = MessageBox.Show("当前文件已修改,是否保存?", "保存确认", MessageBoxButton.YesNoCancel);
|
|
|
if (result == MessageBoxResult.Cancel)
|
|
|
{
|
|
|
e.Cancel = true;
|
|
|
return;
|
|
|
}
|
|
|
if (result == MessageBoxResult.Yes)
|
|
|
SaveFile();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
base.OnClosing(e);
|
|
|
}
|
|
|
|
|
|
|
|
|
public void ShowImageInPanel(BitmapSource image)
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
private void CopyButton_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
private void PasteButton_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
private void InsertSymbolButton_Click(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
|
|
|
var symbolWindow = new Window
|
|
|
{
|
|
|
Title = "插入符号",
|
|
|
Width = 400,
|
|
|
Height = 300,
|
|
|
WindowStartupLocation = WindowStartupLocation.CenterOwner,
|
|
|
Owner = this,
|
|
|
ResizeMode = ResizeMode.NoResize
|
|
|
};
|
|
|
|
|
|
var grid = new Grid();
|
|
|
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
|
|
|
grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
|
|
|
|
|
|
|
|
|
var symbols = new string[]
|
|
|
{
|
|
|
"□", "▢", "■", "◻", "◼", "○", "●", "△",
|
|
|
"▲", "▽", "▼", "◇", "◆", "★", "☆",
|
|
|
"→", "←", "↑", "↓", "↔", "⇒", "⇐", "⇔",
|
|
|
"α", "β", "γ", "δ", "ε", "θ", "λ", "μ", "π", "σ", "φ", "ω",
|
|
|
"Α", "Β", "Γ", "Δ", "Θ", "Λ", "Π", "Σ", "Φ", "Ω",
|
|
|
"∞", "∑", "∏", "∫", "∂", "∇", "√", "∛", "∜",
|
|
|
"≤", "≥", "≠", "≈", "≡", "∈", "∉", "⊂", "⊃", "∪", "∩",
|
|
|
"°", "±", "×", "÷", "¼", "½", "¾", "²", "³", "⁴"
|
|
|
};
|
|
|
|
|
|
var listBox = new ListBox
|
|
|
{
|
|
|
ItemsSource = symbols,
|
|
|
FontSize = 16,
|
|
|
Margin = new Thickness(10)
|
|
|
};
|
|
|
|
|
|
var buttonPanel = new StackPanel
|
|
|
{
|
|
|
Orientation = Orientation.Horizontal,
|
|
|
HorizontalAlignment = HorizontalAlignment.Right,
|
|
|
Margin = new Thickness(10)
|
|
|
};
|
|
|
|
|
|
var insertButton = new Button
|
|
|
{
|
|
|
Content = "插入",
|
|
|
Width = 60,
|
|
|
Height = 25,
|
|
|
Margin = new Thickness(5, 0, 0, 0)
|
|
|
};
|
|
|
|
|
|
var cancelButton = new Button
|
|
|
{
|
|
|
Content = "取消",
|
|
|
Width = 60,
|
|
|
Height = 25
|
|
|
};
|
|
|
|
|
|
insertButton.Click += (s, args) =>
|
|
|
{
|
|
|
if (listBox.SelectedItem != null)
|
|
|
{
|
|
|
string selectedSymbol = listBox.SelectedItem.ToString();
|
|
|
|
|
|
StatusText.Text = $"已插入符号: {selectedSymbol}";
|
|
|
_textChanged = true;
|
|
|
UpdateTitle();
|
|
|
symbolWindow.Close();
|
|
|
}
|
|
|
};
|
|
|
|
|
|
cancelButton.Click += (s, args) => symbolWindow.Close();
|
|
|
|
|
|
listBox.MouseDoubleClick += (s, args) =>
|
|
|
{
|
|
|
if (listBox.SelectedItem != null)
|
|
|
{
|
|
|
string selectedSymbol = listBox.SelectedItem.ToString();
|
|
|
|
|
|
|
|
|
Editor.InsertTextCharacters(selectedSymbol);
|
|
|
|
|
|
|
|
|
_textChanged = true;
|
|
|
|
|
|
|
|
|
Editor.OnTextChanged();
|
|
|
|
|
|
|
|
|
|
|
|
Editor.OnCursorPositionChanged(Editor._cursorLine, Editor._cursorColumn);
|
|
|
|
|
|
|
|
|
InvalidateVisual();
|
|
|
|
|
|
e.Handled = true;
|
|
|
|
|
|
StatusText.Text = $"已插入符号: {selectedSymbol}";
|
|
|
_textChanged = true;
|
|
|
UpdateTitle();
|
|
|
symbolWindow.Close();
|
|
|
}
|
|
|
};
|
|
|
|
|
|
buttonPanel.Children.Add(cancelButton);
|
|
|
buttonPanel.Children.Add(insertButton);
|
|
|
|
|
|
Grid.SetRow(listBox, 0);
|
|
|
Grid.SetRow(buttonPanel, 1);
|
|
|
grid.Children.Add(listBox);
|
|
|
grid.Children.Add(buttonPanel);
|
|
|
|
|
|
symbolWindow.Content = grid;
|
|
|
symbolWindow.ShowDialog();
|
|
|
}
|
|
|
|
|
|
|
|
|
private void InitializeSelectionCanvas()
|
|
|
{
|
|
|
_selectionCanvas = new Canvas()
|
|
|
{
|
|
|
Background = System.Windows.Media.Brushes.Transparent,
|
|
|
IsHitTestVisible = false
|
|
|
};
|
|
|
|
|
|
|
|
|
if (ImageContainer.Child is ScrollViewer scrollViewer)
|
|
|
{
|
|
|
var grid = new Grid();
|
|
|
|
|
|
|
|
|
var originalContent = scrollViewer.Content as UIElement;
|
|
|
if (originalContent != null)
|
|
|
{
|
|
|
scrollViewer.Content = null;
|
|
|
grid.Children.Add(originalContent);
|
|
|
}
|
|
|
|
|
|
grid.Children.Add(_selectionCanvas);
|
|
|
scrollViewer.Content = grid;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ImageViewer_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ImageViewer_MouseMove(object sender, MouseEventArgs e)
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
private void UpdateCursor(Point point)
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ImageViewer_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ShowCoordinateDialog(double left, double top, double width, double height)
|
|
|
{
|
|
|
string message = $"选择区域坐标:\n" +
|
|
|
$"左上角: ({left:F0}, {top:F0})\n" +
|
|
|
$"右下角: ({left + width:F0}, {top + height:F0})\n" +
|
|
|
$"宽度: {width:F0}\n" +
|
|
|
$"高度: {height:F0}";
|
|
|
|
|
|
MessageBox.Show(message, "矩形坐标信息", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void ImageViewer_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
private bool IsPointOnRectangleBorder(Point point, Rectangle rectangle)
|
|
|
{
|
|
|
double left = Canvas.GetLeft(rectangle);
|
|
|
double top = Canvas.GetTop(rectangle);
|
|
|
double right = left + rectangle.Width;
|
|
|
double bottom = top + rectangle.Height;
|
|
|
double tolerance = 5;
|
|
|
|
|
|
|
|
|
bool onLeftBorder = Math.Abs(point.X - left) <= tolerance && point.Y >= top - tolerance && point.Y <= bottom + tolerance;
|
|
|
bool onRightBorder = Math.Abs(point.X - right) <= tolerance && point.Y >= top - tolerance && point.Y <= bottom + tolerance;
|
|
|
bool onTopBorder = Math.Abs(point.Y - top) <= tolerance && point.X >= left - tolerance && point.X <= right + tolerance;
|
|
|
bool onBottomBorder = Math.Abs(point.Y - bottom) <= tolerance && point.X >= left - tolerance && point.X <= right + tolerance;
|
|
|
|
|
|
return onLeftBorder || onRightBorder || onTopBorder || onBottomBorder;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void ExtractTextFromRectangle(Rectangle rectangle)
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void ResetImagePosition()
|
|
|
{
|
|
|
translateTransform.X = 0;
|
|
|
translateTransform.Y = 0;
|
|
|
scaleTransform.ScaleX = 1.0;
|
|
|
scaleTransform.ScaleY = 1.0;
|
|
|
}
|
|
|
|
|
|
|
|
|
private void Grid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
|
|
|
{
|
|
|
if (e.ClickCount == 2)
|
|
|
{
|
|
|
ResetImagePosition();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void Image_SizeChanged(object sender, SizeChangedEventArgs e)
|
|
|
{
|
|
|
if (ViewModel.Image == null) return;
|
|
|
|
|
|
var newWidth = e.NewSize.Width;
|
|
|
var newHeight = e.NewSize.Height;
|
|
|
|
|
|
|
|
|
if (_pendingRectangles.Count > 0 && newWidth > 0 && newHeight > 0)
|
|
|
{
|
|
|
foreach(var selected_rectange in _pendingRectangles)
|
|
|
{
|
|
|
int Id = selected_rectange["Id"].Value<int>();
|
|
|
int lft = selected_rectange["lft"].Value<int>();
|
|
|
int tp = selected_rectange["tp"].Value<int>();
|
|
|
int wdth = selected_rectange["wdth"].Value<int>();
|
|
|
int hght = selected_rectange["hght"].Value<int>();
|
|
|
|
|
|
|
|
|
var displayPos = Editor.ConvertOriginalToDisplayCoordinates(lft, tp);
|
|
|
var displaySize = Editor.ConvertOriginalToDisplaySize(wdth, hght);
|
|
|
|
|
|
ViewModel.AddRectangle(Id, displayPos.X, displayPos.Y, displaySize.Width, displaySize.Height);
|
|
|
}
|
|
|
|
|
|
_pendingRectangles.Clear();
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
|
|
|
if (_previousImageWidth == 0 || _previousImageHeight == 0)
|
|
|
{
|
|
|
_previousImageWidth = newWidth;
|
|
|
_previousImageHeight = newHeight;
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
|
|
|
double scaleX = newWidth / _previousImageWidth;
|
|
|
double scaleY = newHeight / _previousImageHeight;
|
|
|
|
|
|
|
|
|
foreach (var rectangle in ViewModel.Rectangles)
|
|
|
{
|
|
|
rectangle.Left *= scaleX;
|
|
|
rectangle.Top *= scaleY;
|
|
|
rectangle.Width *= scaleX;
|
|
|
rectangle.Height *= scaleY;
|
|
|
}
|
|
|
|
|
|
|
|
|
_previousImageWidth = newWidth;
|
|
|
_previousImageHeight = newHeight;
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ResizeThumb_DragDelta(object sender, DragDeltaEventArgs e)
|
|
|
{
|
|
|
var thumb = sender as Thumb;
|
|
|
if (thumb?.DataContext is RectangleItem rectangle)
|
|
|
{
|
|
|
|
|
|
double newWidth = Math.Max(10, rectangle.Width + e.HorizontalChange);
|
|
|
double newHeight = Math.Max(10, rectangle.Height + e.VerticalChange);
|
|
|
|
|
|
rectangle.Width = newWidth;
|
|
|
rectangle.Height = newHeight;
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ResizeRightThumb_DragDelta(object sender, DragDeltaEventArgs e)
|
|
|
{
|
|
|
var thumb = sender as Thumb;
|
|
|
if (thumb?.DataContext is RectangleItem rectangle)
|
|
|
{
|
|
|
|
|
|
double newWidth = Math.Max(10, rectangle.Width + e.HorizontalChange);
|
|
|
rectangle.Width = newWidth;
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ResizeBottomThumb_DragDelta(object sender, DragDeltaEventArgs e)
|
|
|
{
|
|
|
var thumb = sender as Thumb;
|
|
|
if (thumb?.DataContext is RectangleItem rectangle)
|
|
|
{
|
|
|
|
|
|
double newHeight = Math.Max(10, rectangle.Height + e.VerticalChange);
|
|
|
rectangle.Height = newHeight;
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ResizeLeftThumb_DragDelta(object sender, DragDeltaEventArgs e)
|
|
|
{
|
|
|
var thumb = sender as Thumb;
|
|
|
if (thumb?.DataContext is RectangleItem rectangle)
|
|
|
{
|
|
|
|
|
|
double newLeft = rectangle.Left + e.HorizontalChange;
|
|
|
double newWidth = rectangle.Width - e.HorizontalChange;
|
|
|
|
|
|
|
|
|
if (newWidth >= 10)
|
|
|
{
|
|
|
rectangle.Left = newLeft;
|
|
|
rectangle.Width = newWidth;
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ResizeTopThumb_DragDelta(object sender, DragDeltaEventArgs e)
|
|
|
{
|
|
|
var thumb = sender as Thumb;
|
|
|
if (thumb?.DataContext is RectangleItem rectangle)
|
|
|
{
|
|
|
|
|
|
double newTop = rectangle.Top + e.VerticalChange;
|
|
|
double newHeight = rectangle.Height - e.VerticalChange;
|
|
|
|
|
|
|
|
|
if (newHeight >= 10)
|
|
|
{
|
|
|
rectangle.Top = newTop;
|
|
|
rectangle.Height = newHeight;
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ResizeTopLeftThumb_DragDelta(object sender, DragDeltaEventArgs e)
|
|
|
{
|
|
|
var thumb = sender as Thumb;
|
|
|
if (thumb?.DataContext is RectangleItem rectangle)
|
|
|
{
|
|
|
|
|
|
double newLeft = rectangle.Left + e.HorizontalChange;
|
|
|
double newTop = rectangle.Top + e.VerticalChange;
|
|
|
double newWidth = rectangle.Width - e.HorizontalChange;
|
|
|
double newHeight = rectangle.Height - e.VerticalChange;
|
|
|
|
|
|
|
|
|
if (newWidth >= 10 && newHeight >= 10)
|
|
|
{
|
|
|
rectangle.Left = newLeft;
|
|
|
rectangle.Top = newTop;
|
|
|
rectangle.Width = newWidth;
|
|
|
rectangle.Height = newHeight;
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ResizeTopRightThumb_DragDelta(object sender, DragDeltaEventArgs e)
|
|
|
{
|
|
|
var thumb = sender as Thumb;
|
|
|
if (thumb?.DataContext is RectangleItem rectangle)
|
|
|
{
|
|
|
|
|
|
double newTop = rectangle.Top + e.VerticalChange;
|
|
|
double newWidth = rectangle.Width + e.HorizontalChange;
|
|
|
double newHeight = rectangle.Height - e.VerticalChange;
|
|
|
|
|
|
|
|
|
if (newWidth >= 10 && newHeight >= 10)
|
|
|
{
|
|
|
rectangle.Top = newTop;
|
|
|
rectangle.Width = newWidth;
|
|
|
rectangle.Height = newHeight;
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ResizeBottomLeftThumb_DragDelta(object sender, DragDeltaEventArgs e)
|
|
|
{
|
|
|
var thumb = sender as Thumb;
|
|
|
if (thumb?.DataContext is RectangleItem rectangle)
|
|
|
{
|
|
|
|
|
|
double newLeft = rectangle.Left + e.HorizontalChange;
|
|
|
double newWidth = rectangle.Width - e.HorizontalChange;
|
|
|
double newHeight = rectangle.Height + e.VerticalChange;
|
|
|
|
|
|
|
|
|
if (newWidth >= 10 && newHeight >= 10)
|
|
|
{
|
|
|
rectangle.Left = newLeft;
|
|
|
rectangle.Width = newWidth;
|
|
|
rectangle.Height = newHeight;
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void RectangleGrid_MouseEnter(object sender, MouseEventArgs e)
|
|
|
{
|
|
|
if (sender is Grid grid && grid.DataContext is RectangleItem rectangle)
|
|
|
{
|
|
|
rectangle.IsHovered = true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void RectangleGrid_MouseLeave(object sender, MouseEventArgs e)
|
|
|
{
|
|
|
if (sender is Grid grid && grid.DataContext is RectangleItem rectangle)
|
|
|
{
|
|
|
rectangle.IsHovered = false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ResizeThumb_DragCompleted(object sender, DragCompletedEventArgs e)
|
|
|
{
|
|
|
var thumb = sender as Thumb;
|
|
|
if (thumb?.DataContext is RectangleItem rectangle)
|
|
|
{
|
|
|
OnRectangleAdjustCompleted(rectangle, "右下角");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ResizeRightThumb_DragCompleted(object sender, DragCompletedEventArgs e)
|
|
|
{
|
|
|
var thumb = sender as Thumb;
|
|
|
if (thumb?.DataContext is RectangleItem rectangle)
|
|
|
{
|
|
|
OnRectangleAdjustCompleted(rectangle, "右边");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ResizeBottomThumb_DragCompleted(object sender, DragCompletedEventArgs e)
|
|
|
{
|
|
|
var thumb = sender as Thumb;
|
|
|
if (thumb?.DataContext is RectangleItem rectangle)
|
|
|
{
|
|
|
OnRectangleAdjustCompleted(rectangle, "下边");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ResizeLeftThumb_DragCompleted(object sender, DragCompletedEventArgs e)
|
|
|
{
|
|
|
var thumb = sender as Thumb;
|
|
|
if (thumb?.DataContext is RectangleItem rectangle)
|
|
|
{
|
|
|
OnRectangleAdjustCompleted(rectangle, "左边");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ResizeTopThumb_DragCompleted(object sender, DragCompletedEventArgs e)
|
|
|
{
|
|
|
var thumb = sender as Thumb;
|
|
|
if (thumb?.DataContext is RectangleItem rectangle)
|
|
|
{
|
|
|
OnRectangleAdjustCompleted(rectangle, "上边");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ResizeTopLeftThumb_DragCompleted(object sender, DragCompletedEventArgs e)
|
|
|
{
|
|
|
var thumb = sender as Thumb;
|
|
|
if (thumb?.DataContext is RectangleItem rectangle)
|
|
|
{
|
|
|
OnRectangleAdjustCompleted(rectangle, "左上角");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ResizeTopRightThumb_DragCompleted(object sender, DragCompletedEventArgs e)
|
|
|
{
|
|
|
var thumb = sender as Thumb;
|
|
|
if (thumb?.DataContext is RectangleItem rectangle)
|
|
|
{
|
|
|
OnRectangleAdjustCompleted(rectangle, "右上角");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void ResizeBottomLeftThumb_DragCompleted(object sender, DragCompletedEventArgs e)
|
|
|
{
|
|
|
var thumb = sender as Thumb;
|
|
|
if (thumb?.DataContext is RectangleItem rectangle)
|
|
|
{
|
|
|
OnRectangleAdjustCompleted(rectangle, "左下角");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private void OnRectangleAdjustCompleted(RectangleItem rectangle, string adjustType)
|
|
|
{
|
|
|
On_Rectangle_adjust(rectangle);
|
|
|
}
|
|
|
|
|
|
private async Task split_menu_openImage(string ImageMD5)
|
|
|
{
|
|
|
if (ImageMD5 != "")
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!_dic_m5_imgname.ContainsKey(ImageMD5))
|
|
|
{
|
|
|
throw new Exception("Error: somthing wrong.");
|
|
|
}
|
|
|
|
|
|
string image_pth = _dic_m5_imgname[ImageMD5];
|
|
|
|
|
|
this._currentFilePath = image_pth;
|
|
|
|
|
|
var (jsn, m5, pth_img, pth_json) = await Global.img_md5_jsn(image_pth, _ocrMode);
|
|
|
|
|
|
if (Path.Exists(pth_json))
|
|
|
{
|
|
|
Editor._lines.Clear();
|
|
|
|
|
|
Editor._lines = Global.loadJsonReviseEdit_FromOriginalPath(pth_json);
|
|
|
|
|
|
Editor._undoBuffer.Clear();
|
|
|
Editor._undoIndex = -1;
|
|
|
Editor._cursorLine = 0;
|
|
|
Editor._cursorColumn = 0;
|
|
|
Editor.ClearSelection();
|
|
|
Editor._textChanged = true;
|
|
|
Editor.InvalidateVisual();
|
|
|
|
|
|
|
|
|
Editor.OnTextChanged();
|
|
|
|
|
|
UpdateTitle();
|
|
|
StatusText.Text = $"已打开: {_currentFilePath}";
|
|
|
}
|
|
|
|
|
|
|
|
|
string b64_str = File.ReadAllText(pth_img, Encoding.UTF8);
|
|
|
byte[] img_bytes = Base64.Base64Decode(b64_str);
|
|
|
Mat im_orig = Cv2.ImDecode(img_bytes, ImreadModes.Color);
|
|
|
BitmapSource source = TextEditorControl.MatToBitmapSource(im_orig);
|
|
|
ViewModel.Image = source;
|
|
|
ViewModel.ClearAllRectangles();
|
|
|
|
|
|
|
|
|
|
|
|
_previousImageWidth = 0;
|
|
|
_previousImageHeight = 0;
|
|
|
|
|
|
|
|
|
Editor._pthimg = image_pth;
|
|
|
Editor._jsn_OpenImg = jsn;
|
|
|
Editor._md5_OpenImg = ImageMD5;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
private async void DirectoryTreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
|
|
|
{
|
|
|
if (e.NewValue is DirectoryTreeNode selectedNode)
|
|
|
{
|
|
|
string ImageMD5 = "";
|
|
|
|
|
|
foreach (TextCharacter c in selectedNode.line.Characters)
|
|
|
{
|
|
|
if (c.ImageMD5 != "")
|
|
|
{
|
|
|
ImageMD5 = c.ImageMD5;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (ImageMD5 != "")
|
|
|
{
|
|
|
await split_menu_openImage(ImageMD5);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public class BooleanToVisibilityConverter : IValueConverter
|
|
|
{
|
|
|
public static readonly BooleanToVisibilityConverter Instance = new BooleanToVisibilityConverter();
|
|
|
|
|
|
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
|
|
|
{
|
|
|
return (bool)value ? Visibility.Visible : Visibility.Collapsed;
|
|
|
}
|
|
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
|
|
|
{
|
|
|
return (Visibility)value == Visibility.Visible;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
public class VisibilityAndHoverConverter : IMultiValueConverter
|
|
|
{
|
|
|
public static readonly VisibilityAndHoverConverter Instance = new VisibilityAndHoverConverter();
|
|
|
|
|
|
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
|
|
|
{
|
|
|
if (values.Length >= 2 && values[0] is bool isVisible && values[1] is bool isHovered)
|
|
|
{
|
|
|
return (isVisible && isHovered) ? Visibility.Visible : Visibility.Collapsed;
|
|
|
}
|
|
|
return Visibility.Collapsed;
|
|
|
}
|
|
|
|
|
|
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
|
|
|
{
|
|
|
throw new NotImplementedException();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|