在很多实际工作场景中,我们可能会遇到大量的图片文件,这些图片中包含特定区域的文字信息,比如发票图片上的发票号码、合同图片上的合同编号等。手动识别并为图片命名效率极低且容易出错。使用自动批量识别 JPG 图片上的区域文字,并直接提取文字为图片命名的软件,可以大大提高工作效率,减少人工操作带来的错误。
以下是基于 WPF 和腾讯 API 实现批量图片自定义区域文字识别,并用文字内容改名和导出表格的完整步骤:
TencentCloudSDK
,用于调用腾讯云的文字识别 API。在 MainWindow.xaml
中设计界面,包含选择图片文件夹、自定义区域设置、开始识别、导出表格等功能。
xml
<Window x:Class="ImageOCR.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="批量图片自定义区域文字识别" Height="450" Width="800">
<Grid>
<Button Content="选择图片文件夹" HorizontalAlignment="Left" Margin="20,20,0,0" VerticalAlignment="Top" Width="150" Click="SelectFolderButton_Click"/>
<TextBox x:Name="FolderPathTextBox" HorizontalAlignment="Left" Height="23" Margin="180,20,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="300" IsReadOnly="True"/>
<Label Content="自定义区域 (X,Y,Width,Height):" HorizontalAlignment="Left" Margin="20,60,0,0" VerticalAlignment="Top"/>
<TextBox x:Name="RegionTextBox" HorizontalAlignment="Left" Height="23" Margin="200,60,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="150"/>
<Button Content="开始识别" HorizontalAlignment="Left" Margin="20,100,0,0" VerticalAlignment="Top" Width="150" Click="StartRecognitionButton_Click"/>
<Button Content="导出表格" HorizontalAlignment="Left" Margin="200,100,0,0" VerticalAlignment="Top" Width="150" Click="ExportExcelButton_Click" IsEnabled="False"/>
<ListBox x:Name="ResultListBox" HorizontalAlignment="Left" Height="250" Margin="20,140,0,0" VerticalAlignment="Top" Width="740"/>
</Grid>
</Window>
在 MainWindow.xaml.cs
中实现界面交互和业务逻辑。
csharp
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Forms;
using ClosedXML.Excel;
using TencentCloud.Common;
using TencentCloud.Common.Profile;
using TencentCloud.Ocr.V20181119;
using TencentCloud.Ocr.V20181119.Models;
namespace ImageOCR
{
public partial class MainWindow : Window
{
private string _folderPath;
private List<(string OriginalName, string NewName, string Text)> _results = new List<(string, string, string)>();
public MainWindow()
{
InitializeComponent();
}
private void SelectFolderButton_Click(object sender, RoutedEventArgs e)
{
using (var folderBrowser = new FolderBrowserDialog())
{
if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
_folderPath = folderBrowser.SelectedPath;
FolderPathTextBox.Text = _folderPath;
}
}
}
private void StartRecognitionButton_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(_folderPath))
{
System.Windows.MessageBox.Show("请选择图片文件夹");
return;
}
var region = ParseRegion(RegionTextBox.Text);
if (region == null)
{
System.Windows.MessageBox.Show("自定义区域格式错误,请输入 X,Y,Width,Height");
return;
}
_results.Clear();
ResultListBox.Items.Clear();
var imageFiles = Directory.GetFiles(_folderPath, "*.jpg").Concat(Directory.GetFiles(_folderPath, "*.png"));
foreach (var imageFile in imageFiles)
{
var text = RecognizeText(imageFile, region.Value);
var newName = GenerateNewName(text);
_results.Add((Path.GetFileName(imageFile), newName, text));
ResultListBox.Items.Add($"原文件名: {Path.GetFileName(imageFile)}, 识别结果: {text}, 新文件名: {newName}");
}
RenameFiles();
ExportExcelButton.IsEnabled = true;
}
private void ExportExcelButton_Click(object sender, RoutedEventArgs e)
{
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.Worksheets.Add("识别结果");
worksheet.Cell(1, 1).Value = "原文件名";
worksheet.Cell(1, 2).Value = "新文件名";
worksheet.Cell(1, 3).Value = "识别文本";
for (int i = 0; i < _results.Count; i++)
{
worksheet.Cell(i + 2, 1).Value = _results[i].OriginalName;
worksheet.Cell(i + 2, 2).Value = _results[i].NewName;
worksheet.Cell(i + 2, 3).Value = _results[i].Text;
}
var saveFileDialog = new System.Windows.Forms.SaveFileDialog
{
Filter = "Excel文件 (*.xlsx)|*.xlsx",
FileName = "识别结果.xlsx"
};
if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
workbook.SaveAs(saveFileDialog.FileName);
System.Windows.MessageBox.Show("表格导出成功");
}
}
}
private (int X, int Y, int Width, int Height)? ParseRegion(string input)
{
var parts = input.Split(',');
if (parts.Length == 4 && int.TryParse(parts[0], out int x) && int.TryParse(parts[1], out int y) && int.TryParse(parts[2], out int width) && int.TryParse(parts[3], out int height))
{
return (x, y, width, height);
}
return null;
}
private string RecognizeText(string imageFile, (int X, int Y, int Width, int Height) region)
{
var cred = new Credential
{
SecretId = "your_secret_id",
SecretKey = "your_secret_key"
};
var clientProfile = new ClientProfile();
var httpProfile = new HttpProfile();
httpProfile.Endpoint = "ocr.tencentcloudapi.com";
clientProfile.HttpProfile = httpProfile;
var client = new OcrClient(cred, "ap-guangzhou", clientProfile);
var req = new GeneralBasicOCRRequest();
var imageBytes = File.ReadAllBytes(imageFile);
var imageBase64 = Convert.ToBase64String(imageBytes);
req.ImageBase64 = imageBase64;
req.RegionCoordSet = new long[] { region.X, region.Y, region.X + region.Width, region.Y + region.Height };
var resp = client.GeneralBasicOCRSync(req);
var text = string.Join("", resp.TextDetections.Select(d => d.DetectedText));
return text;
}
private string GenerateNewName(string text)
{
return $"{text}.jpg";
}
private void RenameFiles()
{
foreach (var result in _results)
{
var originalPath = Path.Combine(_folderPath, result.OriginalName);
var newPath = Path.Combine(_folderPath, result.NewName);
if (File.Exists(originalPath))
{
File.Move(originalPath, newPath);
}
}
}
}
}
Button
、TextBox
和 ListBox
实现文件夹选择、自定义区域设置、开始识别、导出表格和结果显示功能。FolderBrowserDialog
选择包含图片的文件夹。X,Y,Width,Height
格式的字符串解析为坐标和尺寸。GeneralBasicOCR
API 对图片指定区域进行文字识别。ClosedXML
库将识别结果导出为 Excel 文件。your_secret_id
和 your_secret_key
替换为你自己的腾讯云 API 密钥。通过以上步骤,你可以实现基于 WPF 和腾讯 API 的批量图片自定义区域文字识别,并用文字内容改名和导出表格的功能。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。