.Net6 图片转文字PictureToTxt

Posted 花开花落的个人博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.Net6 图片转文字PictureToTxt相关的知识,希望对你有一定的参考价值。

一、创建工程

创建工程后,打开Nuget

输入Spire.OCR,进行安装

二、代码编写

using Spire.OCR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace PictureToTxt

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    
        public MainWindow()
        
            InitializeComponent();
        

        private void Convert_Click(object sender, RoutedEventArgs e)
        
            string filePath = FilePath.Text;
            try
            
                if (!string.IsNullOrEmpty(filePath))
                
                    OcrScanner scanner = new OcrScanner();
                    if (scanner.Scan(filePath)==true)
                    
                        ConvertText.Text = scanner.Text.ToString();
                    
                
            
            catch (Exception ex)
            
                MessageBox.Show(ex.Message);
            
        

        private void Grid_DragEnter(object sender, DragEventArgs e)
        
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            
                e.Effects = DragDropEffects.Link;
            
            else
            
                e.Effects = DragDropEffects.None;
            
        

        private void Grid_Drop(object sender, DragEventArgs e)
        
            try
            
                var fileName = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
                FilePath.Text = fileName;
            
            catch (Exception ex)
            
                MessageBox.Show(ex.Message);
            
        

        private void TextBox_PreviewDrop(object sender, DragEventArgs e)
        
            try
            
                ((TextBox)sender).Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            
            catch (Exception ex)
            
                MessageBox.Show(ex.Message);
            
        

        private void TextBox_PreviewDragOver(object sender, DragEventArgs e)
        
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            
                e.Effects = DragDropEffects.Link;
                e.Handled = true;
            
            else
             
                e.Effects = DragDropEffects.None;
            
        

    

界面

<Window x:Class="PictureToTxt.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:PictureToTxt"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" AllowDrop="True" Drop="Grid_Drop" DragEnter="Grid_DragEnter">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="17*"/>
            <ColumnDefinition Width="3*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="100"/>
            <RowDefinition Height="40*"/>
        </Grid.RowDefinitions>
        <Grid Margin="10,10,10,10" Grid.Row="0" Grid.ColumnSpan="2">
            <GroupBox Header="将文件拖入此位置"></GroupBox>
            <Label Content="图片URL:" HorizontalAlignment="Left" Margin="45,0,0,0" VerticalAlignment="Center"/>
            <TextBox x:Name="FilePath" AllowDrop="True" HorizontalAlignment="Left" Margin="115,0,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Center" Width="405"  Drop="Grid_Drop" DragEnter="Grid_DragEnter"/>
            <Button x:Name="Convert"  Content="转换" HorizontalAlignment="Left" Margin="580,0,0,0" VerticalAlignment="Center" Click="Convert_Click"/>
        </Grid>
        <TextBox x:Name="ConvertText"  Grid.Row="1" TextWrapping="Wrap" Text="" Grid.ColumnSpan="2" />
    </Grid>
</Window>

修改工程为x64

复制动态链接库,这一点很重要

找到Spire.OCR包的位置,找到运行库的依赖,复制到程序根目录

复制后

运行程序(不能用调试模式),拖动图片到GroupBox中,然后点击转换

以上是关于.Net6 图片转文字PictureToTxt的主要内容,如果未能解决你的问题,请参考以下文章

.Net6 图片转文字PictureToTxt

wps的pdf扫描识别异常是啥原因

怎样做才能实现图片转文字

Python实现文字转图片

Python实现文字转图片

快速实现图片转文字的方法