百度AI(2)文字识别の银行卡识别(C#)

同系列文章

源代码下载

BaiduAI.zip

演示程序界面

源代码
MainWindow.xaml.cs

/* ----------------------------------------------------------
 * 文件名称:MainWindow.xaml.cs
 * 
 * 作者:秦建辉
 * 
 * 微信:splashcn
 * 
 * 博客:http://www.firstsolver.com/wordpress/
 * 
 * 开发环境:
 *      Visual Studio V2017
 *      .NET Framework 4 Client Profile
 *      Baidu.AI 3.3.0.29135
 * 
 * 版本历史:
 *      V1.0	2018年01月03日
 *              百度AI 银行卡识别演示示例
 *              
 * 参考资料:
 *      http://ai.baidu.com/
 *      https://www.newtonsoft.com/json
------------------------------------------------------------ */
using Baidu.Aip.Ocr;
using Com.FirstSolver.Splash;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;

namespace Splash
{
    public partial class MainWindow : Window
    {
        private static readonly string[] BankCardType = { "不能识别", "借记卡", "信用卡" };
        private Ocr OcrClient;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            string API_KEY = "你的 Api Key";
            string SECRET_KEY = "你的 Secret Key";
            OcrClient = new Ocr(API_KEY, SECRET_KEY);
        }

        private void ButtonSelectImage_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog
                {
                    Filter = "Image|*.jpg;*.bmp;*.png;*.tif;*.tga;*.ras;*.jp2;*.j2k;*.jpe",
                    DereferenceLinks = true
                };

                this.CenterChild();
                if (dlg.ShowDialog(Owner).Value == true)
                {
                    using (FileStream fs = new FileStream(dlg.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                    {
                        byte[] source = new byte[fs.Length];
                        fs.Read(source, 0, (int)fs.Length);

                        BitmapImage bi = new BitmapImage();
                        bi.BeginInit();
                        bi.StreamSource = new MemoryStream(source);
                        bi.EndInit();

                        ImageBankCard.Source = bi;

                        // 银行卡识别
                        var result = OcrClient.Bankcard(source);
                        var error_code = result["error_code"];
                        if (error_code != null)
                        {
                            MessageBoxPlus.Show(this, result["error_msg"].ToString(), "错误", MessageBoxButton.OK, MessageBoxImage.Warning);
                        }
                        else
                        {
                            TextBlockCardNo.Text = result["result"]["bank_card_number"].ToString();
                            TextBlockBankName.Text = result["result"]["bank_name"].ToString();
                            TextBlockCardType.Text = BankCardType[System.Convert.ToInt32(result["result"]["bank_card_type"].ToString())];
                        }
                    }
                }
            }
            catch (System.Exception exception)
            {
                ShowExceptionMessage(exception);
            }
        }

        // 显示异常信息
        public void ShowExceptionMessage(System.Exception exception)
        {
            string ErrorMessage = exception.Message;
            if (string.IsNullOrEmpty(ErrorMessage)) ErrorMessage = exception.InnerException.Message;
            MessageBoxPlus.Show(this, ErrorMessage, "异常", MessageBoxButton.OK, MessageBoxImage.Error);
        }
    }
}

MainWindow.xaml

<Window x:Class="Splash.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="百度AI 银行卡识别演示示例" SizeToContent="WidthAndHeight" WindowStartupLocation="CenterScreen" Icon="IDCardOCR.ico" Loaded="Window_Loaded" FontSize="16">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

        <GroupBox Grid.Column="0" Padding="4" Header="图像选择">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>

                <Button Grid.Row="0" Margin="4" Content="请选择银行卡图像…" Padding="8,0" Height="32" HorizontalAlignment="Left" Click="ButtonSelectImage_Click"></Button>
                <Border Grid.Row="1" Margin="4" HorizontalAlignment="Left" BorderBrush="LightGray" BorderThickness="1">
                    <Image Name="ImageBankCard" Width="400" Height="200"/>
                </Border>
            </Grid>
        </GroupBox>

        <GroupBox Grid.Column="1" Padding="4" Header="识别结果">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="200"/>
                </Grid.ColumnDefinitions>

                <Label Grid.Row="0" Grid.Column="0" Content="银行卡号"/>
                <Label Grid.Row="1" Grid.Column="0" Content="发卡行"/>
                <Label Grid.Row="2" Grid.Column="0" Content="卡片类型"/>

                <TextBlock Grid.Row="0" Grid.Column="1" Name="TextBlockCardNo" Foreground="Blue" VerticalAlignment="Center"/>
                <TextBlock Grid.Row="1" Grid.Column="1" Name="TextBlockBankName" Foreground="Blue" VerticalAlignment="Center"/>
                <TextBlock Grid.Row="2" Grid.Column="1" Name="TextBlockCardType" Foreground="Blue" VerticalAlignment="Center"/>
            </Grid>
        </GroupBox>
    </Grid>
</Window>

Comments are closed.