当前位置: 首页 > news >正文

怎么做文学动漫网站公司logo设计图片欣赏

怎么做文学动漫网站,公司logo设计图片欣赏,屏蔽蜘蛛抓取 对网站有什么影响,深圳seo公司助力网络营销飞跃下面为你详细介绍如何使用 WPF(Windows Presentation Foundation)和阿里云 OCR(光学字符识别)服务开发一个能识别图片文字并批量改名的工具。 项目背景 在日常工作和生活中,我们常常会遇到大量图片文件,这…

下面为你详细介绍如何使用 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-id 和 your-access-key-secret
  • 对 OCR 识别结果的处理可根据具体需求进行优化,例如去除无用字符、添加前缀后缀等。
  • 该工具仅支持 JPG 和 PNG 格式的图片,若需支持其他格式,可修改代码中的文件筛选条件。
http://www.dtcms.com/a/584999.html

相关文章:

  • 网站建设 模块厦门网站建设哪家不错
  • 深圳做高端网站建设公司做家装的网站有什么不同
  • 武威网站建设DS716 II 做网站
  • 网站开发授权书如何更换网站域名
  • 做企业网站的轻量级cms一站式互联网营销平台
  • 长沙产品网站建设国外哪些网站可以注册域名
  • 汕头自助建站系统手机建网站
  • 网站建设教学后记宜昌市高新区建设局网站
  • 山西网站建设排名深圳自适应网站的公司
  • wordpress 安装中文字体如何为网站做seo体检
  • 国内高端网站定制江山建设工程信息网站
  • 皖icp合肥网站开发公司车身广告设计图片
  • 服饰类网站模板烟台网站建设 烟台网亿网络公司
  • 镇江网站建设要多少钱wordpress自定义登陆
  • 做英语阅读的网站360免费wifi总是断断续续的掉线
  • 自己免费怎么制作网站吗动漫设计和动漫制作技术的区别
  • 帮别人做非法网站长沙高端网站制作公司
  • led企业网站策划wordpress 替换google
  • 网站建设需求分析的实施继续浏览此网站(不推荐)
  • 白沟做网站邢台网站网页设计
  • 3d设计网站外贸网站用wordpress
  • 长春企业网站设计培训学校类网站建设方案
  • 外贸公司的网站企业网站
  • 网站怎么申请微信支付接口seo英文怎么读
  • wordpress批量增加用户权限邢台网络优化技术公司
  • 网站建设刂金手指下拉十五规模以上工业企业个数
  • 网站开发做什么的uniapp开发者中心
  • 偃师建设局网站重庆网站建设技术支持重庆互联网
  • 网站搭建代码织梦网站做seo优化
  • 成都 企业 网站建设西安建筑人才网