<Button Content="Select Image Folder" HorizontalAlignment="Left" Margin="10,100,0,0" VerticalAlignment="Top" Width="120" Click="SelectImageFolder_Click"/>
private string imageFolderPath;private void SelectImageFolder_Click(object sender, RoutedEventArgs e){ FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog(); if (folderBrowserDialog.ShowDialog() == DialogResult.OK) { imageFolderPath = folderBrowserDialog.SelectedPath; ResultTextBox.Text += $"Selected image folder: {imageFolderPath}\n"; }}
string[] imageFiles = Directory.GetFiles(imageFolderPath, "*.*", SearchOption.AllDirectories) .Where(file => file.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase) || file.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase) || file.EndsWith(".png", StringComparison.OrdinalIgnoreCase)).ToArray();
{ "region": { "x": 100, "y": 200, "width": 300, "height": 150 }}
在代码中读取该配置文件的方法如下:
using System.IO;using System.Text.Json;public class OcrRegionConfig{ public Region Region { get; set; }}public class Region{ public int X { get; set; } public int Y { get; set; } public int Width { get; set; } public int Height { get; set; }}public OcrRegionConfig LoadOcrConfig(){ string configPath = "ocr_config.json"; if (File.Exists(configPath)) { string json = File.ReadAllText(configPath); return JsonSerializer.Deserialize<OcrRegionConfig>(json); } return null;}
public async Task<string> RecognizeTextFromImage(string imagePath, Region region){ byte[] fileBytes = File.ReadAllBytes(imagePath); string base64File = Convert.ToBase64String(fileBytes); Credential cred = new Credential { SecretId = "YOUR_SECRET_ID", SecretKey = "YOUR_SECRET_KEY" }; ClientProfile clientProfile = new ClientProfile(); HttpProfile httpProfile = new HttpProfile(); httpProfile.Endpoint = "ocr.tencentcloudapi.com"; clientProfile.HttpProfile = httpProfile; OcrClient client = new OcrClient(cred, "ap-guangzhou", clientProfile); GeneralBasicOCRRequest req = new GeneralBasicOCRRequest(); req.ImageBase64 = base64File; if (region != null) { req.Region = new Region { X = region.X, Y = region.Y, Width = region.Width, Height = region.Height }; } GeneralBasicOCRResponse resp = await client.GeneralBasicOCR(req); string recognizedText = ""; foreach (var textDetection in resp.TextDetections) { recognizedText += textDetection.DetectedText + " "; } return recognizedText.Trim();}
OcrRegionConfig config = LoadOcrConfig();List<ImageInfo> imageInfos = new List<ImageInfo>();foreach (string imageFile in imageFiles){ string recognizedText = await RecognizeTextFromImage(imageFile, config?.Region); imageInfos.Add(new ImageInfo { FilePath = imageFile, RecognizedText = recognizedText });}
这里ImageInfo是一个自定义的数据结构,用于存储图片路径和识别出的文字,定义如下:
public class ImageInfo{ public string FilePath { get; set; } public string RecognizedText { get; set; }}
public void RenameImageBasedOnText(string imagePath, string recognizedText){ string folderPath = Path.GetDirectoryName(imagePath); string fileExtension = Path.GetExtension(imagePath); string newFileName = $"{ExtractKeyInfo(recognizedText)}{fileExtension}"; string newFilePath = Path.Combine(folderPath, newFileName); File.Move(imagePath, newFilePath);}
其中ExtractKeyInfo方法用于从识别出的文字中提取关键信息作为文件名的一部分,需要根据实际需求编写提取逻辑,例如通过正则表达式匹配。
2. 批量重命名图片:
foreach (var imageInfo in imageInfos){ RenameImageBasedOnText(imageInfo.FilePath, imageInfo.RecognizedText);}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。