分享:图片识别改名,能识别图片中的文字并批量改名的工具,用WPF和阿里云来完成

下面为你详细介绍如何使用 WPF(Windows Presentation Foundation)和阿里云 OCR(光学字符识别)服务开发一个能识别图片文字并批量改名的工具。

项目背景

在日常工作和生活中,我们常常会遇到大量图片文件,这些图片文件名可能没有实际意义,手动为其命名既耗时又容易出错。借助 OCR 技术能够识别图片中的文字信息,将这些文字作为图片的新文件名,可提高文件管理效率。阿里云 OCR 具备高精度、高稳定性的特点,能准确识别多种图片格式中的文字。结合 WPF 开发的桌面应用程序,能为用户提供直观便捷的操作界面。

界面设计

  • 文件选择按钮:用于选择需要处理的图片文件夹。
  • 处理按钮:点击后开始识别图片文字并改名。
  • 进度条:显示处理进度。
  • 日志输出框:展示处理过程中的信息,如文件名、识别结果等。

详细代码步骤过程

1. 创建 WPF 项目

在 Visual Studio 中创建一个新的 WPF 应用程序项目。

2. 安装阿里云 OCR SDK

通过 NuGet 包管理器安装 Aliyun.SDK.ocr20191230 包。

3. 设计 XAML 界面

以下是 MainWindow.xaml 的代码:

复制代码
<Window x:Class="ImageOCRRenamer.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"/>
        <Button Content="开始处理" HorizontalAlignment="Left" Margin="200,20,0,0" VerticalAlignment="Top" Width="150" Click="ProcessButton_Click"/>
        <ProgressBar x:Name="ProgressBar" HorizontalAlignment="Left" Height="20" Margin="20,60,0,0" VerticalAlignment="Top" Width="740"/>
        <TextBox x:Name="LogTextBox" HorizontalAlignment="Left" Height="320" Margin="20,100,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="740" IsReadOnly="True"/>
    </Grid>
</Window>
4. 编写 C# 代码

以下是 MainWindow.xaml.cs 的代码:

复制代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using Aliyun.SDK.ocr20191230;
using Aliyun.SDK.ocr20191230.Models;
using AlibabaCloud.TeaUtil;
using AlibabaCloud.TeaUtil.Models;
using AlibabaCloud.DarabonbaOpenApi.Models;

namespace ImageOCRRenamer
{
    public partial class MainWindow : Window
    {
        private string _selectedFolder;
        private List<string> _imageFiles;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void SelectFolderButton_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new System.Windows.Forms.FolderBrowserDialog();
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                _selectedFolder = dialog.SelectedPath;
                _imageFiles = Directory.GetFiles(_selectedFolder, "*.jpg").Concat(Directory.GetFiles(_selectedFolder, "*.png")).ToList();
                LogTextBox.Text += $"已选择文件夹:{_selectedFolder},共找到 {_imageFiles.Count} 张图片。\n";
            }
        }

        private async void ProcessButton_Click(object sender, RoutedEventArgs e)
        {
            if (string.IsNullOrEmpty(_selectedFolder) || _imageFiles == null || _imageFiles.Count == 0)
            {
                MessageBox.Show("请先选择图片文件夹。");
                return;
            }

            ProgressBar.Maximum = _imageFiles.Count;
            ProgressBar.Value = 0;

            var client = CreateClient("your-access-key-id", "your-access-key-secret");

            for (int i = 0; i < _imageFiles.Count; i++)
            {
                var imageFile = _imageFiles[i];
                try
                {
                    var ocrResult = await PerformOCR(client, imageFile);
                    var newFileName = GenerateNewFileName(ocrResult);
                    RenameFile(imageFile, newFileName);
                    LogTextBox.Text += $"处理 {imageFile} 成功,新文件名:{newFileName}\n";
                }
                catch (Exception ex)
                {
                    LogTextBox.Text += $"处理 {imageFile} 失败:{ex.Message}\n";
                }
                ProgressBar.Value = i + 1;
            }

            MessageBox.Show("处理完成。");
        }

        private static Aliyun.SDK.ocr20191230.Client CreateClient(string accessKeyId, string accessKeySecret)
        {
            Config config = new Config
            {
                AccessKeyId = accessKeyId,
                AccessKeySecret = accessKeySecret
            };
            config.Endpoint = "ocr.cn-hangzhou.aliyuncs.com";
            return new Aliyun.SDK.ocr20191230.Client(config);
        }

        private async Task<string> PerformOCR(Aliyun.SDK.ocr20191230.Client client, string imageFile)
        {
            var content = File.ReadAllBytes(imageFile);
            var base64Image = Convert.ToBase64String(content);

            RecognizeGeneralRequest recognizeGeneralRequest = new RecognizeGeneralRequest
            {
                ImageBase64 = base64Image
            };
            RuntimeOptions runtime = new RuntimeOptions();
            try
            {
                var response = await client.RecognizeGeneralWithOptions(recognizeGeneralRequest, runtime);
                var result = response.Body.Data.Content;
                return result;
            }
            catch (Exception error)
            {
                Console.WriteLine(TeaException.ToJSONString(error));
                throw;
            }
        }

        private string GenerateNewFileName(string ocrResult)
        {
            // 简单处理,去除非法字符
            var validChars = new string(ocrResult.Where(c => !Path.GetInvalidFileNameChars().Contains(c)).ToArray());
            return $"{validChars}.jpg";
        }

        private void RenameFile(string oldFilePath, string newFileName)
        {
            var directory = Path.GetDirectoryName(oldFilePath);
            var newFilePath = Path.Combine(directory, newFileName);
            File.Move(oldFilePath, newFilePath);
        }
    }
}

总结

本项目借助 WPF 构建了一个直观的桌面应用程序界面,利用阿里云 OCR 服务实现了图片文字识别功能,并完成了图片批量改名操作。通过该工具,用户能够轻松地将图片文件名替换为图片中的文字信息,提高了文件管理效率。不过,在实际使用时需要注意:

  • 要在阿里云控制台创建 AccessKey 并替换代码中的 your-access-key-idyour-access-key-secret
  • 对 OCR 识别结果的处理可根据具体需求进行优化,例如去除无用字符、添加前缀后缀等。
  • 该工具仅支持 JPG 和 PNG 格式的图片,若需支持其他格式,可修改代码中的文件筛选条件。
相关推荐
heimeiyingwang29 分钟前
【架构实战】日志体系ELK:集中化日志管理实践
elk·架构·wpf
CPU不够了1 小时前
WPF 多选下拉+搜索过滤_wpf下拉选项增加搜索
wpf
FuckPatience1 小时前
WPF 列表控件自动拉伸子元素的宽度
wpf
weixin_408099671 小时前
易语言调用OCR API实现批量图片文字识别:从接口对接到多文件处理(附完整源码)
ocr·文字识别·api接口·易语言·批量识别·石榴智能·精易模块
LCG元2 小时前
【Go后端开发】从 0 到生产级:高性能分布式网关全实现 + 接口限流熔断降级实战
分布式·golang·wpf
枫叶林FYL17 小时前
项目九:异步高性能爬虫与数据采集中枢 —— 基于 Crawl<sub>4</sub>AI 与 Playwright 的现代化数据采集平台 项目总览
爬虫·python·深度学习·wpf
AI人工智能+21 小时前
不动产权证书识别技术:融合了计算机视觉、自然语言处理(NLP)和人工智能的深度技术栈
人工智能·计算机视觉·语言模型·ocr·不动产权证书识别
她说彩礼65万1 天前
WPF 多值转换器
wpf
Maydaycxc1 天前
跨境电商多账号自动化:RPA对接指纹浏览器与OCR识图实战
自动化·ocr·rpa
Miss roro1 天前
法律文书信息自动提取:OCR识别与AI技术在案件管理中的应用
人工智能·ocr·法律科技·律所管理系统·案件管理系统