Compare commits
16 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
525c823397 | ||
|
|
7db2d8813e | ||
|
|
7216f62cef | ||
|
|
a777991b8c | ||
|
|
e2c6c52b32 | ||
|
|
33089c39b6 | ||
|
|
45805178cc | ||
|
|
08a11f025a | ||
|
|
5bb3886bc5 | ||
|
|
ce5f964776 | ||
|
|
f52c7908d7 | ||
|
|
abcbf06493 | ||
|
|
f56bcb3627 | ||
|
|
f789c7904a | ||
|
|
e04709637c | ||
|
|
e39f976607 |
@@ -57,7 +57,7 @@ namespace AppStore
|
|||||||
|
|
||||||
// 初始化并添加应用信息
|
// 初始化并添加应用信息
|
||||||
infoLabel = new Label();
|
infoLabel = new Label();
|
||||||
infoLabel.Text = "kortapp-z\n版本: 1.2.0\n作者: zs-yg\n一个简单、开源的应用商店\nkortapp-z是完全免费\n基于.NET8和C/C++的软件";
|
infoLabel.Text = "kortapp-z\n版本: 1.2.5\n作者: zs-yg\n一个简单、开源的应用商店\nkortapp-z是完全免费\n基于.NET8和C/C++的软件";
|
||||||
infoLabel.Font = new Font("Microsoft YaHei", 12);
|
infoLabel.Font = new Font("Microsoft YaHei", 12);
|
||||||
infoLabel.AutoSize = false;
|
infoLabel.AutoSize = false;
|
||||||
infoLabel.Width = 300;
|
infoLabel.Width = 300;
|
||||||
|
|||||||
106
AppCard.cs
@@ -54,9 +54,23 @@ namespace AppStore
|
|||||||
this.Padding = new Padding(10);
|
this.Padding = new Padding(10);
|
||||||
|
|
||||||
// 异步初始化卡片路径和边框
|
// 异步初始化卡片路径和边框
|
||||||
|
// 预加载边框路径
|
||||||
Task.Run(() => {
|
Task.Run(() => {
|
||||||
InitializeCardPath();
|
InitializeCardPath();
|
||||||
InitializeBorder();
|
InitializeBorder();
|
||||||
|
|
||||||
|
// 确保在主线程注册事件
|
||||||
|
this.Invoke((MethodInvoker)(() => {
|
||||||
|
this.Paint += (sender, e) => {
|
||||||
|
if (BorderCache.IsEmpty)
|
||||||
|
{
|
||||||
|
Task.Run(() => {
|
||||||
|
InitializeBorder();
|
||||||
|
this.Invoke((MethodInvoker)(() => this.Invalidate()));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
// 应用图标 - 添加null检查
|
// 应用图标 - 添加null检查
|
||||||
@@ -187,52 +201,58 @@ namespace AppStore
|
|||||||
// 使用卡片尺寸作为缓存键
|
// 使用卡片尺寸作为缓存键
|
||||||
string cacheKey = $"{Width}_{Height}_10";
|
string cacheKey = $"{Width}_{Height}_10";
|
||||||
|
|
||||||
// 检查缓存中是否已有路径
|
// 双重检查锁模式确保线程安全
|
||||||
if (!BorderCache.TryGetValue(cacheKey, out var borderPath))
|
if (!BorderCache.TryGetValue(cacheKey, out var borderPath))
|
||||||
{
|
{
|
||||||
// 创建临时文件存储路径数据
|
lock (BorderCache)
|
||||||
string tempFile = Path.GetTempFileName();
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
// 配置C++程序启动参数
|
if (!BorderCache.TryGetValue(cacheKey, out borderPath))
|
||||||
ProcessStartInfo startInfo = new ProcessStartInfo
|
|
||||||
{
|
{
|
||||||
FileName = Path.Combine(Application.StartupPath, "resource", "border_renderer.exe"),
|
// 创建临时文件存储路径数据
|
||||||
Arguments = $"{Width} {Height} 10 \"{tempFile}\"", // 传递宽高和圆角半径
|
string tempFile = Path.GetTempFileName();
|
||||||
UseShellExecute = false, // 不显示命令行窗口
|
try
|
||||||
CreateNoWindow = true // 静默运行
|
|
||||||
};
|
|
||||||
|
|
||||||
// 启动C++程序计算路径
|
|
||||||
using (var process = Process.Start(startInfo))
|
|
||||||
{
|
|
||||||
process.WaitForExit();
|
|
||||||
|
|
||||||
// 检查计算结果
|
|
||||||
if (process.ExitCode == 0 && File.Exists(tempFile))
|
|
||||||
{
|
{
|
||||||
// 读取C++程序生成的路径点
|
// 配置C++程序启动参数
|
||||||
var lines = File.ReadAllLines(tempFile);
|
ProcessStartInfo startInfo = new ProcessStartInfo
|
||||||
PointF[] points = lines.Select(line => {
|
{
|
||||||
var parts = line.Split(','); // 解析坐标点
|
FileName = Path.Combine(Application.StartupPath, "resource", "border_renderer.exe"),
|
||||||
return new PointF(float.Parse(parts[0]), float.Parse(parts[1]));
|
Arguments = $"{Width} {Height} 10 \"{tempFile}\"", // 传递宽高和圆角半径
|
||||||
}).ToArray();
|
UseShellExecute = false, // 不显示命令行窗口
|
||||||
|
CreateNoWindow = true // 静默运行
|
||||||
// 创建GraphicsPath对象
|
};
|
||||||
borderPath = new System.Drawing.Drawing2D.GraphicsPath();
|
|
||||||
borderPath.AddLines(points); // 添加路径点
|
// 启动C++程序计算路径
|
||||||
|
using (var process = Process.Start(startInfo))
|
||||||
// 缓存路径对象
|
{
|
||||||
BorderCache.TryAdd(cacheKey, borderPath);
|
process.WaitForExit();
|
||||||
|
|
||||||
|
// 检查计算结果
|
||||||
|
if (process.ExitCode == 0 && File.Exists(tempFile))
|
||||||
|
{
|
||||||
|
// 读取C++程序生成的路径点
|
||||||
|
var lines = File.ReadAllLines(tempFile);
|
||||||
|
PointF[] points = lines.Select(line => {
|
||||||
|
var parts = line.Split(','); // 解析坐标点
|
||||||
|
return new PointF(float.Parse(parts[0]), float.Parse(parts[1]));
|
||||||
|
}).ToArray();
|
||||||
|
|
||||||
|
// 创建GraphicsPath对象
|
||||||
|
borderPath = new System.Drawing.Drawing2D.GraphicsPath();
|
||||||
|
borderPath.AddLines(points); // 添加路径点
|
||||||
|
|
||||||
|
// 缓存路径对象
|
||||||
|
BorderCache.TryAdd(cacheKey, borderPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
// 确保临时文件被删除
|
||||||
|
if (File.Exists(tempFile))
|
||||||
|
{
|
||||||
|
File.Delete(tempFile);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
finally
|
|
||||||
{
|
|
||||||
// 确保临时文件被删除
|
|
||||||
if (File.Exists(tempFile))
|
|
||||||
{
|
|
||||||
File.Delete(tempFile);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -243,6 +263,12 @@ namespace AppStore
|
|||||||
{
|
{
|
||||||
base.OnPaint(e);
|
base.OnPaint(e);
|
||||||
|
|
||||||
|
// 确保边框已初始化
|
||||||
|
if (BorderCache.IsEmpty)
|
||||||
|
{
|
||||||
|
InitializeBorder();
|
||||||
|
}
|
||||||
|
|
||||||
// 绘制背景
|
// 绘制背景
|
||||||
using (var brush = new SolidBrush(this.BackColor)) {
|
using (var brush = new SolidBrush(this.BackColor)) {
|
||||||
e.Graphics.FillRectangle(brush, this.ClientRectangle);
|
e.Graphics.FillRectangle(brush, this.ClientRectangle);
|
||||||
|
|||||||
47
MainForm.cs
@@ -710,7 +710,7 @@ namespace AppStore
|
|||||||
flowPanel.Margin = new Padding(0);
|
flowPanel.Margin = new Padding(0);
|
||||||
flowPanel.AutoSize = true;
|
flowPanel.AutoSize = true;
|
||||||
flowPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink;
|
flowPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink;
|
||||||
flowPanel.AutoScrollMinSize = new Size(0, 3550);
|
flowPanel.AutoScrollMinSize = new Size(0, 3800);//大概一行250像素
|
||||||
contentPanel.Controls.Add(flowPanel);
|
contentPanel.Controls.Add(flowPanel);
|
||||||
|
|
||||||
// 添加所有应用卡片并恢复位置
|
// 添加所有应用卡片并恢复位置
|
||||||
@@ -786,6 +786,11 @@ namespace AppStore
|
|||||||
"https://ghproxy.net/https://github.com/msys2/msys2-installer/releases/download/2025-02-21/msys2-x86_64-20250221.exe",
|
"https://ghproxy.net/https://github.com/msys2/msys2-installer/releases/download/2025-02-21/msys2-x86_64-20250221.exe",
|
||||||
"img/png/MSYS2.png"));
|
"img/png/MSYS2.png"));
|
||||||
|
|
||||||
|
flowPanel.Controls.Add(CreateAppCard(
|
||||||
|
"OpenJDK by Azul JDKs",
|
||||||
|
"https://cdn.azul.com/zulu/bin/zulu21.42.19-ca-jdk21.0.7-win_x64.msi",
|
||||||
|
"img/png/Azul_JDKs.png"));
|
||||||
|
|
||||||
flowPanel.Controls.Add(CreateAppCard(
|
flowPanel.Controls.Add(CreateAppCard(
|
||||||
".NET SDK 8.0",
|
".NET SDK 8.0",
|
||||||
"https://dotnet.microsoft.com/zh-cn/download/dotnet/thank-you/sdk-8.0.411-windows-x64-installer",
|
"https://dotnet.microsoft.com/zh-cn/download/dotnet/thank-you/sdk-8.0.411-windows-x64-installer",
|
||||||
@@ -821,6 +826,16 @@ namespace AppStore
|
|||||||
"https://ghproxy.net/https://github.com/game1024/OpenSpeedy/releases/download/v1.7.1/OpenSpeedy-v1.7.1.zip",
|
"https://ghproxy.net/https://github.com/game1024/OpenSpeedy/releases/download/v1.7.1/OpenSpeedy-v1.7.1.zip",
|
||||||
"img/png/openspeedy.png"));
|
"img/png/openspeedy.png"));
|
||||||
|
|
||||||
|
flowPanel.Controls.Add(CreateAppCard(
|
||||||
|
"Final2x",
|
||||||
|
"https://ghproxy.net/https://github.com/Tohrusky/Final2x/releases/download/2024-12-14/Final2x-windows-x64-setup.exe",
|
||||||
|
"img/png/Final2x.png"));
|
||||||
|
|
||||||
|
flowPanel.Controls.Add(CreateAppCard(
|
||||||
|
"Pixpin",
|
||||||
|
"https://download.pixpin.cn/PixPin_2.0.0.3.exe",
|
||||||
|
"img/png/pixpin.png"));
|
||||||
|
|
||||||
flowPanel.Controls.Add(CreateAppCard(
|
flowPanel.Controls.Add(CreateAppCard(
|
||||||
"QuickLook",
|
"QuickLook",
|
||||||
"https://ghproxy.net/https://github.com/QL-Win/QuickLook/releases/download/4.0.2/QuickLook-4.0.2.exe",
|
"https://ghproxy.net/https://github.com/QL-Win/QuickLook/releases/download/4.0.2/QuickLook-4.0.2.exe",
|
||||||
@@ -1011,27 +1026,52 @@ namespace AppStore
|
|||||||
"notepad--",
|
"notepad--",
|
||||||
"https://www.ghproxy.cn/https://github.com/cxasm/notepad--/releases/download/notepad-v3.3/Notepad--v3.3-plugin-Installer.exe",
|
"https://www.ghproxy.cn/https://github.com/cxasm/notepad--/releases/download/notepad-v3.3/Notepad--v3.3-plugin-Installer.exe",
|
||||||
"img/png/notepad--.png"));
|
"img/png/notepad--.png"));
|
||||||
|
|
||||||
|
flowPanel.Controls.Add(CreateAppCard(
|
||||||
|
"chatlog",
|
||||||
|
"https://www.ghproxy.cn/https://github.com/sjzar/chatlog/releases/download/v0.0.15/chatlog_0.0.15_windows_amd64.zip",
|
||||||
|
"img/jpg/github.jpg"));
|
||||||
|
|
||||||
flowPanel.Controls.Add(CreateAppCard(
|
flowPanel.Controls.Add(CreateAppCard(
|
||||||
"PowerToys",
|
"PowerToys",
|
||||||
"https://ghproxy.net/https://github.com/microsoft/PowerToys/releases/download/v0.91.1/PowerToysSetup-0.91.1-x64.exe",
|
"https://ghproxy.net/https://github.com/microsoft/PowerToys/releases/download/v0.91.1/PowerToysSetup-0.91.1-x64.exe",
|
||||||
"img/png/PowerToys.png"));
|
"img/png/PowerToys.png"));
|
||||||
|
|
||||||
|
flowPanel.Controls.Add(CreateAppCard(
|
||||||
|
"Powershell",
|
||||||
|
"https://ghproxy.net/https://github.com/Powershell/Powershell/releases/download/v7.5.2/Powershell-7.5.2-win-x64.exe",
|
||||||
|
"img/png/powershell.png"));
|
||||||
|
|
||||||
flowPanel.Controls.Add(CreateAppCard(
|
flowPanel.Controls.Add(CreateAppCard(
|
||||||
"terminal",
|
"terminal",
|
||||||
"https://ghproxy.net/https://github.com/microsoft/terminal/releases/download/v1.22.11141.0/Microsoft.WindowsTerminal_1.22.11141.0_x64.zip",
|
"https://ghproxy.net/https://github.com/microsoft/terminal/releases/download/v1.22.11141.0/Microsoft.WindowsTerminal_1.22.11141.0_x64.zip",
|
||||||
"img/png/terminal.png"));
|
"img/png/terminal.png"));
|
||||||
|
|
||||||
|
flowPanel.Controls.Add(CreateAppCard(
|
||||||
|
"edit",
|
||||||
|
"https://ghproxy.net/https://github.com/microsoft/edit/releases/download/v1.2.0/edit-1.2.0-x86_64-windows.zip",
|
||||||
|
"img/png/edit.png"));
|
||||||
|
|
||||||
flowPanel.Controls.Add(CreateAppCard(
|
flowPanel.Controls.Add(CreateAppCard(
|
||||||
"github_cli",
|
"github_cli",
|
||||||
"https://ghproxy.cn/https://github.com/cli/cli/releases/download/v2.74.2/gh_2.74.2_windows_arm64.msi",
|
"https://ghproxy.cn/https://github.com/cli/cli/releases/download/v2.74.2/gh_2.74.2_windows_arm64.msi",
|
||||||
"img/png/github_cli.png"));
|
"img/png/github_cli.png"));
|
||||||
|
|
||||||
|
flowPanel.Controls.Add(CreateAppCard(
|
||||||
|
"VideoCaptioner",
|
||||||
|
"https://ghproxy.cn/https://github.com/WEIFENG2333/VideoCaptioner/releases/download/v1.3.3/VideoCaptioner-Setup-win64-v1.3.3.exe",
|
||||||
|
"img/png/VideoCaptioner.png"));
|
||||||
|
|
||||||
flowPanel.Controls.Add(CreateAppCard(
|
flowPanel.Controls.Add(CreateAppCard(
|
||||||
"ReactOS",
|
"ReactOS",
|
||||||
"https://ghproxy.cn/https://github.com/reactos/reactos/releases/download/0.4.15-release/ReactOS-0.4.15-release-1-gdbb43bbaeb2-x86-iso.zip",
|
"https://ghproxy.cn/https://github.com/reactos/reactos/releases/download/0.4.15-release/ReactOS-0.4.15-release-1-gdbb43bbaeb2-x86-iso.zip",
|
||||||
"img/png/ReactOS.png"));
|
"img/png/ReactOS.png"));
|
||||||
|
|
||||||
|
flowPanel.Controls.Add(CreateAppCard(
|
||||||
|
"Ubuntu桌面发行版",
|
||||||
|
"https://releases.ubuntu.com/24.04/ubuntu-24.04.2-desktop-amd64.iso",
|
||||||
|
"img/png/Ubuntu.png"));
|
||||||
|
|
||||||
flowPanel.Controls.Add(CreateAppCard(
|
flowPanel.Controls.Add(CreateAppCard(
|
||||||
"typescript",
|
"typescript",
|
||||||
"https://ghproxy.net/https://github.com/microsoft/TypeScript/releases/download/v5.8.3/typescript-5.8.3.tgz",
|
"https://ghproxy.net/https://github.com/microsoft/TypeScript/releases/download/v5.8.3/typescript-5.8.3.tgz",
|
||||||
@@ -1042,6 +1082,11 @@ namespace AppStore
|
|||||||
"https://mirror.nju.edu.cn/gimp/gimp/v3.0/windows/gimp-3.0.4-setup.exe",
|
"https://mirror.nju.edu.cn/gimp/gimp/v3.0/windows/gimp-3.0.4-setup.exe",
|
||||||
"img/jpg/Gimp.jpg"));
|
"img/jpg/Gimp.jpg"));
|
||||||
|
|
||||||
|
flowPanel.Controls.Add(CreateAppCard(
|
||||||
|
"ClamAV",
|
||||||
|
"https://www.clamav.net/downloads/production/clamav-1.4.3.win.x64.msi",
|
||||||
|
"img/png/ClamAV.png"));
|
||||||
|
|
||||||
flowPanel.Controls.Add(CreateAppCard(
|
flowPanel.Controls.Add(CreateAppCard(
|
||||||
"Shotcut",
|
"Shotcut",
|
||||||
"https://sourceforge.net/projects/shotcut/files/v25.05.11/shotcut-win64-250511.exe/download",
|
"https://sourceforge.net/projects/shotcut/files/v25.05.11/shotcut-win64-250511.exe/download",
|
||||||
|
|||||||
@@ -76,6 +76,10 @@ namespace AppStore
|
|||||||
private static readonly Color DarkText = Color.FromArgb(240, 240, 240);
|
private static readonly Color DarkText = Color.FromArgb(240, 240, 240);
|
||||||
private static readonly Color DarkButtonHover = Color.FromArgb(60, 60, 60);
|
private static readonly Color DarkButtonHover = Color.FromArgb(60, 60, 60);
|
||||||
private static readonly Color DarkButtonActive = Color.FromArgb(70, 70, 70);
|
private static readonly Color DarkButtonActive = Color.FromArgb(70, 70, 70);
|
||||||
|
private static readonly Color DarkBorder = Color.FromArgb(80, 80, 80);
|
||||||
|
|
||||||
|
// 浅色主题边框颜色
|
||||||
|
private static readonly Color LightBorder = Color.FromArgb(180, 180, 180);
|
||||||
|
|
||||||
public static event Action<ThemeMode> ThemeChanged = delegate {};
|
public static event Action<ThemeMode> ThemeChanged = delegate {};
|
||||||
|
|
||||||
@@ -108,6 +112,9 @@ namespace AppStore
|
|||||||
public static Color ButtonActiveColor =>
|
public static Color ButtonActiveColor =>
|
||||||
_currentTheme == ThemeMode.Light ? LightButtonActive : DarkButtonActive;
|
_currentTheme == ThemeMode.Light ? LightButtonActive : DarkButtonActive;
|
||||||
|
|
||||||
|
public static Color BorderColor =>
|
||||||
|
_currentTheme == ThemeMode.Light ? LightBorder : DarkBorder;
|
||||||
|
|
||||||
public static void ApplyTheme(Control control)
|
public static void ApplyTheme(Control control)
|
||||||
{
|
{
|
||||||
ApplyThemeToControl(control);
|
ApplyThemeToControl(control);
|
||||||
|
|||||||
@@ -17,21 +17,7 @@
|
|||||||
<h2>核心功能</h2>
|
<h2>核心功能</h2>
|
||||||
|
|
||||||
<article class="feature">
|
<article class="feature">
|
||||||
<h3>应用程序管理</h3>
|
<h3>目前没有什么东西,别看了,害羞(✿◡‿◡)
|
||||||
<p>批量安装、卸载(目前没有)和更新应用程序(目前没有),管理启动项(目前没有)。</p>
|
|
||||||
<p>优势:集中管理所有应用,节省时间,避免系统臃肿。</p>
|
|
||||||
</article>
|
|
||||||
|
|
||||||
<article class="feature">
|
|
||||||
<h3>资源监控(之后可能在内置工具里有)</h3>
|
|
||||||
<p>实时监控CPU、内存、磁盘和网络使用情况。</p>
|
|
||||||
<p>优势:直观的图表展示,及时发现资源瓶颈。</p>
|
|
||||||
</article>
|
|
||||||
|
|
||||||
<article class="feature">
|
|
||||||
<h3>文件管理(之后可能在内置工具里有)</h3>
|
|
||||||
<p>高级文件搜索、批量重命名和快速文件分类。</p>
|
|
||||||
<p>优势:提升文件管理效率,支持正则表达式搜索。</p>
|
|
||||||
</article>
|
</article>
|
||||||
</section>
|
</section>
|
||||||
</main>
|
</main>
|
||||||
|
|||||||
BIN
img/png/Azul_JDKs.png
Normal file
|
After Width: | Height: | Size: 2.3 KiB |
BIN
img/png/ClamAV.png
Normal file
|
After Width: | Height: | Size: 36 KiB |
BIN
img/png/Final2x.png
Normal file
|
After Width: | Height: | Size: 5.6 KiB |
BIN
img/png/Ubuntu.png
Normal file
|
After Width: | Height: | Size: 853 B |
BIN
img/png/VideoCaptioner.png
Normal file
|
After Width: | Height: | Size: 6.1 KiB |
BIN
img/png/edit.png
Normal file
|
After Width: | Height: | Size: 1.0 KiB |
BIN
img/png/pixpin.png
Normal file
|
After Width: | Height: | Size: 239 KiB |
BIN
img/png/powershell.png
Normal file
|
After Width: | Height: | Size: 2.3 KiB |
@@ -2,7 +2,7 @@
|
|||||||
; 有关创建 Inno Setup 脚本文件的详细信息,请参阅帮助文档!
|
; 有关创建 Inno Setup 脚本文件的详细信息,请参阅帮助文档!
|
||||||
|
|
||||||
#define MyAppName "kortapp-z"
|
#define MyAppName "kortapp-z"
|
||||||
#define MyAppVersion "1.2.0"
|
#define MyAppVersion "1.2.5"
|
||||||
#define MyAppPublisher "zsyg"
|
#define MyAppPublisher "zsyg"
|
||||||
#define MyAppURL "https://github.com/zs-yg/kortapp-z"
|
#define MyAppURL "https://github.com/zs-yg/kortapp-z"
|
||||||
#define MyAppExeName "kortapp-z.exe"
|
#define MyAppExeName "kortapp-z.exe"
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
; 有关创建 Inno Setup 脚本文件的详细信息,请参阅帮助文档!
|
; 有关创建 Inno Setup 脚本文件的详细信息,请参阅帮助文档!
|
||||||
|
|
||||||
#define MyAppName "kortapp-z"
|
#define MyAppName "kortapp-z"
|
||||||
#define MyAppVersion "1.2.0"
|
#define MyAppVersion "1.2.5"
|
||||||
#define MyAppPublisher "zsyg"
|
#define MyAppPublisher "zsyg"
|
||||||
#define MyAppURL "https://github.com/zs-yg/kortapp-z"
|
#define MyAppURL "https://github.com/zs-yg/kortapp-z"
|
||||||
#define MyAppExeName "kortapp-z.exe"
|
#define MyAppExeName "kortapp-z.exe"
|
||||||
|
|||||||
20
logger.cs
@@ -75,5 +75,25 @@ namespace AppStore
|
|||||||
}
|
}
|
||||||
Log(warningMessage);
|
Log(warningMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void LogDebug(string message, Exception? ex = null)
|
||||||
|
{
|
||||||
|
string debugMessage = $"DEBUG: {message}";
|
||||||
|
if (ex != null)
|
||||||
|
{
|
||||||
|
debugMessage += $"\nException: {ex}\nStackTrace: {ex.StackTrace}";
|
||||||
|
}
|
||||||
|
Log(debugMessage);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void LogTip(string message, Exception? ex = null)
|
||||||
|
{
|
||||||
|
string tipMessage = $"TIP: {message}";
|
||||||
|
if (ex != null)
|
||||||
|
{
|
||||||
|
tipMessage += $"\nException: {ex}\nStackTrace: {ex.StackTrace}";
|
||||||
|
}
|
||||||
|
Log(tipMessage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
30
others/C++/gcc_OCR/Makefile
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
# 编译器设置
|
||||||
|
CXX = g++
|
||||||
|
CXXFLAGS = -std=c++17 -Wall -Wextra -Iinclude
|
||||||
|
LDFLAGS = -lfltk -ltesseract -lleptonica
|
||||||
|
|
||||||
|
# 源文件和目标文件
|
||||||
|
SRCS = src/main.cpp src/gui_window.cpp src/ocr_engine.cpp \
|
||||||
|
src/file_io.cpp src/error_handler.cpp src/image_processor.cpp
|
||||||
|
OBJS = $(patsubst src/%.cpp,obj/%.o,$(SRCS))
|
||||||
|
|
||||||
|
# 目标可执行文件
|
||||||
|
TARGET = ocr_app.exe
|
||||||
|
|
||||||
|
# 默认目标
|
||||||
|
all: $(TARGET)
|
||||||
|
|
||||||
|
# 链接规则
|
||||||
|
$(TARGET): $(OBJS)
|
||||||
|
$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)
|
||||||
|
|
||||||
|
# 编译规则
|
||||||
|
obj/%.o: src/%.cpp
|
||||||
|
@mkdir -p obj
|
||||||
|
$(CXX) $(CXXFLAGS) -c -o $@ $<
|
||||||
|
|
||||||
|
# 清理
|
||||||
|
clean:
|
||||||
|
rm -rf obj $(TARGET)
|
||||||
|
|
||||||
|
.PHONY: all clean
|
||||||
19
others/C++/gcc_OCR/include/common.hpp
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
#ifndef COMMON_HPP
|
||||||
|
#define COMMON_HPP
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include <memory>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
// 公共宏定义
|
||||||
|
#define APP_NAME "OCR识别器"
|
||||||
|
#define APP_VERSION "1.0.0"
|
||||||
|
|
||||||
|
// 公共类型定义
|
||||||
|
using String = std::string;
|
||||||
|
|
||||||
|
// 错误处理宏
|
||||||
|
#define THROW_EXCEPTION(msg) throw std::runtime_error(std::string(__FILE__) + ":" + std::to_string(__LINE__) + " " + msg)
|
||||||
|
|
||||||
|
#endif // COMMON_HPP
|
||||||
40
others/C++/gcc_OCR/include/config.hpp
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
#ifndef CONFIG_HPP
|
||||||
|
#define CONFIG_HPP
|
||||||
|
|
||||||
|
#include "../include/common.hpp"
|
||||||
|
#include <map>
|
||||||
|
|
||||||
|
struct AppConfig {
|
||||||
|
String language; // OCR识别语言
|
||||||
|
String lastDir; // 最后打开的目录
|
||||||
|
int windowWidth; // 窗口宽度
|
||||||
|
int windowHeight; // 窗口高度
|
||||||
|
};
|
||||||
|
|
||||||
|
class ConfigManager {
|
||||||
|
public:
|
||||||
|
ConfigManager();
|
||||||
|
|
||||||
|
// 加载配置
|
||||||
|
bool loadConfig(const String& filePath = "config.ini");
|
||||||
|
|
||||||
|
// 保存配置
|
||||||
|
bool saveConfig(const String& filePath = "config.ini");
|
||||||
|
|
||||||
|
// 获取配置
|
||||||
|
AppConfig getConfig() const;
|
||||||
|
|
||||||
|
// 更新配置
|
||||||
|
void updateConfig(const AppConfig& newConfig);
|
||||||
|
|
||||||
|
private:
|
||||||
|
AppConfig config;
|
||||||
|
|
||||||
|
// 解析INI文件
|
||||||
|
void parseIni(const String& content);
|
||||||
|
|
||||||
|
// 生成INI文件内容
|
||||||
|
String generateIni() const;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // CONFIG_HPP
|
||||||
28
others/C++/gcc_OCR/include/error_handler.hpp
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#ifndef ERROR_HANDLER_HPP
|
||||||
|
#define ERROR_HANDLER_HPP
|
||||||
|
|
||||||
|
#include "../include/common.hpp"
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
|
// 错误代码枚举
|
||||||
|
enum class ErrorCode {
|
||||||
|
FILE_IO_ERROR,
|
||||||
|
OCR_INIT_ERROR,
|
||||||
|
OCR_PROCESS_ERROR,
|
||||||
|
GUI_ERROR,
|
||||||
|
UNKNOWN_ERROR
|
||||||
|
};
|
||||||
|
|
||||||
|
// 自定义异常类
|
||||||
|
class OCRException : public std::runtime_error {
|
||||||
|
public:
|
||||||
|
ErrorCode code;
|
||||||
|
|
||||||
|
OCRException(ErrorCode ec, const String& msg)
|
||||||
|
: std::runtime_error(msg), code(ec) {}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 错误处理函数
|
||||||
|
void handleError(const std::exception& e);
|
||||||
|
|
||||||
|
#endif // ERROR_HANDLER_HPP
|
||||||
16
others/C++/gcc_OCR/include/file_io.hpp
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#ifndef FILE_IO_HPP
|
||||||
|
#define FILE_IO_HPP
|
||||||
|
|
||||||
|
#include "../include/common.hpp"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
// 保存文本到文件
|
||||||
|
bool saveTextToFile(const String& filePath, const String& content);
|
||||||
|
|
||||||
|
// 从文件加载文本
|
||||||
|
String loadTextFromFile(const String& filePath);
|
||||||
|
|
||||||
|
// 获取支持的图像格式列表
|
||||||
|
std::vector<String> getSupportedImageFormats();
|
||||||
|
|
||||||
|
#endif // FILE_IO_HPP
|
||||||
50
others/C++/gcc_OCR/include/gui_window.hpp
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
#ifndef GUI_WINDOW_HPP
|
||||||
|
#define GUI_WINDOW_HPP
|
||||||
|
|
||||||
|
#include <FL/Fl.H>
|
||||||
|
#include <FL/Fl_Window.H>
|
||||||
|
#include <FL/Fl_Button.H>
|
||||||
|
#include <FL/Fl_Text_Display.H>
|
||||||
|
#include <FL/Fl_Text_Buffer.H>
|
||||||
|
#include <FL/Fl_Choice.H>
|
||||||
|
#include "../include/common.hpp"
|
||||||
|
|
||||||
|
class GUIWindow {
|
||||||
|
public:
|
||||||
|
GUIWindow(int width, int height, const char* title);
|
||||||
|
~GUIWindow();
|
||||||
|
|
||||||
|
// 设置OCR结果文本
|
||||||
|
void setOCRResult(const String& text);
|
||||||
|
|
||||||
|
// 按钮状态控制
|
||||||
|
void disableButtons();
|
||||||
|
void enableButtons();
|
||||||
|
|
||||||
|
// 获取当前语言设置
|
||||||
|
String getLanguage() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Fl_Window* window;
|
||||||
|
Fl_Text_Display* textDisplay;
|
||||||
|
Fl_Text_Buffer* textBuffer;
|
||||||
|
Fl_Button* openButton;
|
||||||
|
Fl_Button* saveButton;
|
||||||
|
Fl_Choice* languageChoice;
|
||||||
|
|
||||||
|
// 支持的语言列表
|
||||||
|
static constexpr const char* LANGUAGES[3] = {"英文", "简体中文", "中英文混合"};
|
||||||
|
static constexpr const char* LANGUAGE_CODES[3] = {"eng", "chi_sim", "eng+chi_sim"};
|
||||||
|
|
||||||
|
// 回调函数
|
||||||
|
static void openCallback(Fl_Widget* w, void* data);
|
||||||
|
static void saveCallback(Fl_Widget* w, void* data);
|
||||||
|
|
||||||
|
// 初始化UI
|
||||||
|
void initUI();
|
||||||
|
|
||||||
|
// 异常处理辅助方法
|
||||||
|
static void handleException(void* data);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // GUI_WINDOW_HPP
|
||||||
31
others/C++/gcc_OCR/include/image_processor.hpp
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
#ifndef IMAGE_PROCESSOR_HPP
|
||||||
|
#define IMAGE_PROCESSOR_HPP
|
||||||
|
|
||||||
|
#include "../include/common.hpp"
|
||||||
|
#include <leptonica/allheaders.h>
|
||||||
|
|
||||||
|
class ImageProcessor {
|
||||||
|
public:
|
||||||
|
// 从文件加载图像并进行预处理
|
||||||
|
static Pix* loadAndPreprocess(const String& filePath);
|
||||||
|
|
||||||
|
// 图像预处理
|
||||||
|
static Pix* preprocess(Pix* image);
|
||||||
|
|
||||||
|
// 转换为灰度图像
|
||||||
|
static Pix* convertToGrayscale(Pix* image);
|
||||||
|
|
||||||
|
// 二值化处理
|
||||||
|
static Pix* binarize(Pix* image);
|
||||||
|
|
||||||
|
// 调整对比度
|
||||||
|
static Pix* adjustContrast(Pix* image, float factor);
|
||||||
|
|
||||||
|
// 调整亮度
|
||||||
|
static Pix* adjustBrightness(Pix* image, int delta);
|
||||||
|
|
||||||
|
// 降噪处理
|
||||||
|
static Pix* removeNoise(Pix* image);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // IMAGE_PROCESSOR_HPP
|
||||||
30
others/C++/gcc_OCR/include/ocr_engine.hpp
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
#ifndef OCR_ENGINE_HPP
|
||||||
|
#define OCR_ENGINE_HPP
|
||||||
|
|
||||||
|
#include <tesseract/baseapi.h>
|
||||||
|
#include <leptonica/allheaders.h>
|
||||||
|
#include "../include/common.hpp"
|
||||||
|
|
||||||
|
class OCREngine {
|
||||||
|
public:
|
||||||
|
OCREngine();
|
||||||
|
~OCREngine();
|
||||||
|
|
||||||
|
// 设置识别语言
|
||||||
|
bool setLanguage(const String& lang);
|
||||||
|
|
||||||
|
// 从图像文件识别文本
|
||||||
|
String recognizeFromFile(const String& filePath);
|
||||||
|
|
||||||
|
// 从内存图像识别文本
|
||||||
|
String recognizeFromImage(Pix* image);
|
||||||
|
|
||||||
|
private:
|
||||||
|
tesseract::TessBaseAPI* api;
|
||||||
|
String currentLanguage;
|
||||||
|
|
||||||
|
// 初始化Tesseract
|
||||||
|
void initTesseract();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // OCR_ENGINE_HPP
|
||||||
27
others/C++/gcc_OCR/include/result_display.hpp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#ifndef RESULT_DISPLAY_HPP
|
||||||
|
#define RESULT_DISPLAY_HPP
|
||||||
|
|
||||||
|
#include "../include/common.hpp"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class ResultDisplay {
|
||||||
|
public:
|
||||||
|
// 格式化OCR结果
|
||||||
|
static String formatResult(const String& rawText);
|
||||||
|
|
||||||
|
// 校正常见OCR错误
|
||||||
|
static String correctCommonErrors(const String& text);
|
||||||
|
|
||||||
|
// 分割段落
|
||||||
|
static std::vector<String> splitParagraphs(const String& text);
|
||||||
|
|
||||||
|
// 高亮低置信度区域
|
||||||
|
static String highlightLowConfidence(const String& text, const std::vector<float>& confidences);
|
||||||
|
|
||||||
|
// 导出为不同格式
|
||||||
|
static bool exportAsText(const String& filePath, const String& content);
|
||||||
|
static bool exportAsHtml(const String& filePath, const String& content);
|
||||||
|
static bool exportAsPdf(const String& filePath, const String& content);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // RESULT_DISPLAY_HPP
|
||||||
20
others/C++/gcc_OCR/include/utils.hpp
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
#ifndef UTILS_HPP
|
||||||
|
#define UTILS_HPP
|
||||||
|
|
||||||
|
#include "../include/common.hpp"
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
// 字符串工具
|
||||||
|
String trim(const String& str);
|
||||||
|
std::vector<String> split(const String& str, char delimiter);
|
||||||
|
String join(const std::vector<String>& strings, const String& delimiter);
|
||||||
|
|
||||||
|
// 图像工具
|
||||||
|
bool isImageFile(const String& filePath);
|
||||||
|
String getFileExtension(const String& filePath);
|
||||||
|
|
||||||
|
// 系统工具
|
||||||
|
String getCurrentDateTime();
|
||||||
|
String getHomeDirectory();
|
||||||
|
|
||||||
|
#endif // UTILS_HPP
|
||||||
27
others/C++/gcc_OCR/src/error_handler.cpp
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#include "../include/error_handler.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
#include <FL/Fl.H>
|
||||||
|
#include <FL/fl_ask.H>
|
||||||
|
|
||||||
|
void handleError(const std::exception& e) {
|
||||||
|
// 记录错误到控制台
|
||||||
|
std::cerr << "错误: " << e.what() << std::endl;
|
||||||
|
|
||||||
|
// 显示用户友好的错误消息
|
||||||
|
if (Fl::first_window()) {
|
||||||
|
fl_alert("%s", e.what());
|
||||||
|
} else {
|
||||||
|
std::cerr << "无法显示错误对话框: 没有可用的GUI窗口" << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String errorCodeToString(ErrorCode code) {
|
||||||
|
switch(code) {
|
||||||
|
case ErrorCode::FILE_IO_ERROR: return "文件IO错误";
|
||||||
|
case ErrorCode::OCR_INIT_ERROR: return "OCR初始化错误";
|
||||||
|
case ErrorCode::OCR_PROCESS_ERROR: return "OCR处理错误";
|
||||||
|
case ErrorCode::GUI_ERROR: return "GUI错误";
|
||||||
|
case ErrorCode::UNKNOWN_ERROR: return "未知错误";
|
||||||
|
default: return "未定义的错误代码";
|
||||||
|
}
|
||||||
|
}
|
||||||
39
others/C++/gcc_OCR/src/file_io.cpp
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
#include "../include/file_io.hpp"
|
||||||
|
#include "../include/error_handler.hpp"
|
||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
bool saveTextToFile(const String& filePath, const String& content) {
|
||||||
|
std::ofstream outFile(filePath);
|
||||||
|
if (!outFile) {
|
||||||
|
THROW_EXCEPTION("无法打开文件进行写入: " + filePath);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
outFile << content;
|
||||||
|
outFile.close();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
String loadTextFromFile(const String& filePath) {
|
||||||
|
std::ifstream inFile(filePath);
|
||||||
|
if (!inFile) {
|
||||||
|
THROW_EXCEPTION("无法打开文件进行读取: " + filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::stringstream buffer;
|
||||||
|
buffer << inFile.rdbuf();
|
||||||
|
inFile.close();
|
||||||
|
return buffer.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<String> getSupportedImageFormats() {
|
||||||
|
return {
|
||||||
|
"*.png",
|
||||||
|
"*.jpg",
|
||||||
|
"*.jpeg",
|
||||||
|
"*.bmp",
|
||||||
|
"*.tif",
|
||||||
|
"*.tiff"
|
||||||
|
};
|
||||||
|
}
|
||||||
106
others/C++/gcc_OCR/src/gui_window.cpp
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
#include "../include/gui_window.hpp"
|
||||||
|
#include "../include/file_io.hpp"
|
||||||
|
#include "../include/ocr_engine.hpp"
|
||||||
|
#include <FL/Fl_File_Chooser.H>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
|
// 异常信息结构体
|
||||||
|
struct ExceptionData {
|
||||||
|
GUIWindow* window;
|
||||||
|
String errorMsg;
|
||||||
|
};
|
||||||
|
|
||||||
|
GUIWindow::GUIWindow(int width = 900, int height = 700, const char* title = "OCR识别工具") {
|
||||||
|
window = new Fl_Window(width, height, title);
|
||||||
|
initUI();
|
||||||
|
window->end();
|
||||||
|
window->show();
|
||||||
|
}
|
||||||
|
|
||||||
|
GUIWindow::~GUIWindow() {
|
||||||
|
delete textBuffer;
|
||||||
|
delete window;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GUIWindow::initUI() {
|
||||||
|
textBuffer = new Fl_Text_Buffer();
|
||||||
|
textDisplay = new Fl_Text_Display(20, 20, 860, 550, "识别结果");
|
||||||
|
textDisplay->buffer(textBuffer);
|
||||||
|
|
||||||
|
// 添加语言选择下拉菜单
|
||||||
|
languageChoice = new Fl_Choice(70, 580, 300, 25, "识别语言");
|
||||||
|
for (int i = 0; i < 3; ++i) {
|
||||||
|
languageChoice->add(LANGUAGES[i]);
|
||||||
|
}
|
||||||
|
languageChoice->value(0); // 默认选择英文
|
||||||
|
|
||||||
|
openButton = new Fl_Button(390, 580, 150, 25, "打开图片");
|
||||||
|
openButton->callback(openCallback, this);
|
||||||
|
|
||||||
|
saveButton = new Fl_Button(550, 580, 150, 25, "保存结果");
|
||||||
|
saveButton->callback(saveCallback, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GUIWindow::setOCRResult(const String& text) {
|
||||||
|
textBuffer->text(text.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
String GUIWindow::getLanguage() const {
|
||||||
|
return LANGUAGE_CODES[languageChoice->value()];
|
||||||
|
}
|
||||||
|
|
||||||
|
void GUIWindow::handleException(void* data) {
|
||||||
|
ExceptionData* exData = static_cast<ExceptionData*>(data);
|
||||||
|
exData->window->setOCRResult("无法识别");
|
||||||
|
exData->window->enableButtons();
|
||||||
|
fl_alert("识别错误: %s", exData->errorMsg.c_str());
|
||||||
|
delete exData;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GUIWindow::openCallback(Fl_Widget* w, void* data) {
|
||||||
|
(void)w;
|
||||||
|
GUIWindow* win = static_cast<GUIWindow*>(data);
|
||||||
|
const char* filename = fl_file_chooser("选择图片文件", "*.{png,jpg,bmp}", "");
|
||||||
|
if (filename) {
|
||||||
|
win->setOCRResult("正在识别...");
|
||||||
|
win->disableButtons();
|
||||||
|
|
||||||
|
std::thread([win, filename]() {
|
||||||
|
try {
|
||||||
|
OCREngine ocr;
|
||||||
|
if (!ocr.setLanguage(win->getLanguage())) {
|
||||||
|
throw std::runtime_error("无法设置识别语言");
|
||||||
|
}
|
||||||
|
String result = ocr.recognizeFromFile(filename);
|
||||||
|
Fl::awake([](void* data) {
|
||||||
|
auto* ctx = static_cast<std::pair<GUIWindow*, String>*>(data);
|
||||||
|
ctx->first->setOCRResult(ctx->second.empty() ? "无法识别" : ctx->second);
|
||||||
|
ctx->first->enableButtons();
|
||||||
|
delete ctx;
|
||||||
|
}, new std::pair<GUIWindow*, String>(win, result));
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
ExceptionData* exData = new ExceptionData{win, e.what()};
|
||||||
|
Fl::awake(handleException, exData);
|
||||||
|
}
|
||||||
|
}).detach();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void GUIWindow::disableButtons() {
|
||||||
|
openButton->deactivate();
|
||||||
|
saveButton->deactivate();
|
||||||
|
}
|
||||||
|
|
||||||
|
void GUIWindow::enableButtons() {
|
||||||
|
openButton->activate();
|
||||||
|
saveButton->activate();
|
||||||
|
}
|
||||||
|
|
||||||
|
void GUIWindow::saveCallback(Fl_Widget* w, void* data) {
|
||||||
|
(void)w;
|
||||||
|
GUIWindow* win = static_cast<GUIWindow*>(data);
|
||||||
|
const char* filename = fl_file_chooser("保存识别结果", "*.txt", "");
|
||||||
|
if (filename && win->textBuffer->length() > 0) {
|
||||||
|
saveTextToFile(filename, win->textBuffer->text());
|
||||||
|
}
|
||||||
|
}
|
||||||
64
others/C++/gcc_OCR/src/image_processor.cpp
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
#include "../include/image_processor.hpp"
|
||||||
|
#include "../include/error_handler.hpp"
|
||||||
|
#include <leptonica/allheaders.h>
|
||||||
|
|
||||||
|
Pix* ImageProcessor::loadAndPreprocess(const String& filePath) {
|
||||||
|
Pix* image = pixRead(filePath.c_str());
|
||||||
|
if (!image) {
|
||||||
|
THROW_EXCEPTION("无法加载图像文件: " + filePath);
|
||||||
|
}
|
||||||
|
return preprocess(image);
|
||||||
|
}
|
||||||
|
|
||||||
|
Pix* ImageProcessor::preprocess(Pix* image) {
|
||||||
|
Pix* processed = convertToGrayscale(image);
|
||||||
|
processed = adjustContrast(processed, 1.5f);
|
||||||
|
processed = binarize(processed);
|
||||||
|
processed = removeNoise(processed);
|
||||||
|
return processed;
|
||||||
|
}
|
||||||
|
|
||||||
|
Pix* ImageProcessor::convertToGrayscale(Pix* image) {
|
||||||
|
Pix* gray = pixConvertRGBToGray(image, 0.3f, 0.59f, 0.11f);
|
||||||
|
if (!gray) {
|
||||||
|
pixDestroy(&image);
|
||||||
|
THROW_EXCEPTION("灰度转换失败");
|
||||||
|
}
|
||||||
|
return gray;
|
||||||
|
}
|
||||||
|
|
||||||
|
Pix* ImageProcessor::binarize(Pix* image) {
|
||||||
|
// 使用更精细的局部二值化
|
||||||
|
if (pixOtsuAdaptiveThreshold(image, 16, 16, 5, 5, 0.1f, nullptr, nullptr) != 0) {
|
||||||
|
pixDestroy(&image);
|
||||||
|
THROW_EXCEPTION("二值化失败");
|
||||||
|
}
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
Pix* ImageProcessor::adjustContrast(Pix* image, float factor) {
|
||||||
|
Pix* contrast = pixContrastTRC(nullptr, image, factor);
|
||||||
|
if (!contrast) {
|
||||||
|
pixDestroy(&image);
|
||||||
|
THROW_EXCEPTION("对比度调整失败");
|
||||||
|
}
|
||||||
|
return contrast;
|
||||||
|
}
|
||||||
|
|
||||||
|
Pix* ImageProcessor::adjustBrightness(Pix* image, int delta) {
|
||||||
|
if (pixMultConstantGray(image, 1.0f + delta/255.0f) != 0) {
|
||||||
|
pixDestroy(&image);
|
||||||
|
THROW_EXCEPTION("亮度调整失败");
|
||||||
|
}
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
Pix* ImageProcessor::removeNoise(Pix* image) {
|
||||||
|
// 增强降噪效果
|
||||||
|
Pix* denoised = pixCleanImage(image, 2, 0, 2, 10);
|
||||||
|
if (!denoised) {
|
||||||
|
pixDestroy(&image);
|
||||||
|
THROW_EXCEPTION("降噪失败");
|
||||||
|
}
|
||||||
|
return denoised;
|
||||||
|
}
|
||||||
21
others/C++/gcc_OCR/src/main.cpp
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#include "../include/common.hpp"
|
||||||
|
#include "../include/gui_window.hpp"
|
||||||
|
#include "../include/ocr_engine.hpp"
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
(void)argc;
|
||||||
|
(void)argv;
|
||||||
|
try {
|
||||||
|
// 初始化GUI窗口
|
||||||
|
GUIWindow window(900, 700, "OCR识别器");
|
||||||
|
|
||||||
|
// 初始化OCR引擎
|
||||||
|
OCREngine ocr;
|
||||||
|
|
||||||
|
// 运行主循环
|
||||||
|
return Fl::run();
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
std::cerr << "错误: " << e.what() << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
}
|
||||||
68
others/C++/gcc_OCR/src/ocr_engine.cpp
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
#include "../include/ocr_engine.hpp"
|
||||||
|
#include "../include/error_handler.hpp"
|
||||||
|
#include <tesseract/baseapi.h>
|
||||||
|
#include <leptonica/allheaders.h>
|
||||||
|
|
||||||
|
OCREngine::OCREngine() : api(new tesseract::TessBaseAPI()), currentLanguage("eng") {
|
||||||
|
initTesseract();
|
||||||
|
}
|
||||||
|
|
||||||
|
OCREngine::~OCREngine() {
|
||||||
|
if (api) {
|
||||||
|
api->End();
|
||||||
|
delete api;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool OCREngine::setLanguage(const String& lang) {
|
||||||
|
// 支持多语言混合识别,如"eng+chi_sim"
|
||||||
|
if (api->Init("C:/msys64/ucrt64/share/tessdata/", lang.c_str())) {
|
||||||
|
// 尝试回退到英文
|
||||||
|
if (api->Init("C:/msys64/ucrt64/share/tessdata/", "eng")) {
|
||||||
|
THROW_EXCEPTION("无法设置OCR语言: " + lang + " 且回退到英文失败");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
currentLanguage = "eng";
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
currentLanguage = lang;
|
||||||
|
|
||||||
|
// 针对中文优化识别参数
|
||||||
|
if (lang.find("chi") != String::npos) {
|
||||||
|
api->SetVariable("tessedit_char_whitelist",
|
||||||
|
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
"的一是在不了有和人这中大为上个国我以要他时来用们生到作地于出就分对成会可主发年动同工也能下过子说产种面而方后多定行学法所民得经十三之进着等部度家电力里如水化高自二理起小物现实加量都两体制机当使点从业本去把性好应开它合还因由其些然前外天政四日那社义事平形相全表间样与关各重新线内数正心反你明看原又么利比或但质气第向道命此变条只没结解问意建月公无系军很情者最立代想已通并提直题党程展五果料象员革位入常文总次品式活设及管特件长求老头基资边流路级少图山统接知较将组见计别她手角期根论运农指几九区强放决西被干做必战先回则任取据处队南给色光门即保治北造百规热领七海口东导器压志世金增争济阶油思术极交受联什认六共权收证改清己美再采转更单风切打白教速花带安场身车例真务具万每目至达走积示议声报斗完类八离华名确才科张信马节话米整空元况今集温传土许步群广石记需段研界拉林律叫且究观越织装影算低持音众书布复容儿须际商非验连断深难近矿千周委素技备半办青省列习响约支般史感劳便团往酸历市克何除消构府称太准精值号率族维划选标写存候毛亲快效斯院查江型眼王按格养易置派层片始却专状育厂京识适属圆包火住调满县局照参红细引听该铁价严龙飞");
|
||||||
|
api->SetPageSegMode(tesseract::PSM_SPARSE_TEXT_OSD);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
String OCREngine::recognizeFromFile(const String& filePath) {
|
||||||
|
Pix* image = pixRead(filePath.c_str());
|
||||||
|
if (!image) {
|
||||||
|
THROW_EXCEPTION("无法加载图像文件: " + filePath);
|
||||||
|
}
|
||||||
|
|
||||||
|
String result = recognizeFromImage(image);
|
||||||
|
pixDestroy(&image);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
String OCREngine::recognizeFromImage(Pix* image) {
|
||||||
|
api->SetImage(image);
|
||||||
|
char* text = api->GetUTF8Text();
|
||||||
|
if (!text) {
|
||||||
|
THROW_EXCEPTION("OCR识别失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
String result(text);
|
||||||
|
delete[] text;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OCREngine::initTesseract() {
|
||||||
|
if (api->Init("C:/msys64/ucrt64/share/tessdata/", currentLanguage.c_str())) {
|
||||||
|
THROW_EXCEPTION("无法初始化Tesseract OCR引擎");
|
||||||
|
}
|
||||||
|
api->SetPageSegMode(tesseract::PSM_AUTO);
|
||||||
|
}
|
||||||