同系列文章
源代码下载
源代码
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.Collections.Generic; using System.IO; using System.Windows; using System.Windows.Controls; using System.Windows.Media.Imaging; namespace Splash { public partial class MainWindow : Window { private Ocr OcrClient; private Dictionary<string, object> options = new Dictionary<string, object>{ { "detect_direction", "true"}, // 检测图像朝向 { "detect_risk", "true"} // 开启身份证风险类型 }; 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(); int Tag = System.Convert.ToInt32(((Button)sender).Tag); if (Tag == 0) ImageIDCardFront.Source = bi; else ImageIDCardBack.Source = bi; // 带参数调用身份证识别 var result = OcrClient.Idcard(source, (Tag == 0) ? "front" : "back", options); var error_code = result["error_code"]; if (error_code != null) { MessageBoxPlus.Show(this, result["error_msg"].ToString(), "错误", MessageBoxButton.OK, MessageBoxImage.Warning); } else { string image_status = result["image_status"].ToString(); if (string.Equals(image_status, "normal")) { if (Tag == 0) { // 身份证正面信息 TextBlockName.Text = result["words_result"]["姓名"]["words"].ToString(); TextBlockSex.Text = result["words_result"]["性别"]["words"].ToString(); TextBlockNation.Text = result["words_result"]["民族"]["words"].ToString(); TextBlockBorn.Text = result["words_result"]["出生"]["words"].ToString(); TextBlockAddress.Text = result["words_result"]["住址"]["words"].ToString(); TextBlockCardNo.Text = result["words_result"]["公民身份号码"]["words"].ToString(); } else { // 身份证背面信息 TextBlockIssuedAt.Text = result["words_result"]["签发机关"]["words"].ToString(); TextBlockPeriod.Text = result["words_result"]["签发日期"]["words"].ToString() + "-" + result["words_result"]["失效日期"]["words"].ToString(); } string risk_type = result["risk_type"].ToString(); if (!string.Equals(risk_type, "normal")) { MessageBoxPlus.Show(this, "risk_type=" + risk_type, "警告", MessageBoxButton.OK, MessageBoxImage.Warning); } } else { MessageBoxPlus.Show(this, "image_status=" + image_status, "错误", MessageBoxButton.OK, MessageBoxImage.Error); } } } } } 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"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Button Grid.Row="0" Margin="4" Tag="0" 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="ImageIDCardFront" Width="400" Height="200"/> </Border> <Button Grid.Row="2" Margin="4" Tag="1" Content="请选择身份证背面图像…" Padding="8,0" Height="32" HorizontalAlignment="Left" Click="ButtonSelectImage_Click"></Button> <Border Grid.Row="3" Margin="4" HorizontalAlignment="Left" BorderBrush="LightGray" BorderThickness="1"> <Image Name="ImageIDCardBack" 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 Grid.Row="0"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <Grid Grid.Column="0"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition MinWidth="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="出生"/> <Label Grid.Row="3" Grid.Column="0" Content="住址"/> <TextBlock Grid.Row="0" Grid.Column="1" Name="TextBlockName" Foreground="Blue" VerticalAlignment="Center"/> <Grid Grid.Row="1" Grid.Column="1"> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition Width="Auto"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Name="TextBlockSex" Foreground="Blue" VerticalAlignment="Center"/> <Label Grid.Column="1" Content="民族"/> <TextBlock Grid.Column="2" Name="TextBlockNation" Foreground="Blue" VerticalAlignment="Center"/> </Grid> <TextBlock Grid.Row="2" Grid.Column="1" Name="TextBlockBorn" Foreground="Blue" VerticalAlignment="Center"/> <TextBlock Grid.Row="3" Grid.Column="1" Name="TextBlockAddress" Foreground="Blue" VerticalAlignment="Center" TextWrapping="Wrap"/> </Grid> <Image Grid.Column="1" Width="128" Height="128" Name="ImageCardPhoto" Visibility="Collapsed"/> </Grid> <Grid Grid.Row="1"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Label Grid.Column="0" Content="公民身份号码"/> <TextBlock Grid.Column="1" Foreground="Blue" Name="TextBlockCardNo" VerticalAlignment="Center"/> </Grid> <Grid Grid.Row="2"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Label Grid.Row="0" Grid.Column="0" Content="签发机关"/> <Label Grid.Row="1" Grid.Column="0" Content="有效期限"/> <TextBlock Grid.Row="0" Grid.Column="1" Foreground="Blue" Name="TextBlockIssuedAt" VerticalAlignment="Center"/> <TextBlock Grid.Row="1" Grid.Column="1" Foreground="Blue" Name="TextBlockPeriod" VerticalAlignment="Center"/> </Grid> </Grid> </GroupBox> </Grid> </Window>