问题描述:通过和身份证读卡器对接获取基本信息和身份证照片信息,但切换身份证时,基本信息能随动变化,但照片不会。
代码:
xaml:
C#:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;
using AlarmCenter.GWUserControlLibrary;
using AlarmCenter.GWWPFControls;
using AlarmCenter.GWWpfCustomControlLibrary;
using AlarmCenter.DataCenter;
using System.Windows.Threading;
using System.IO;
namespace IdCardReaderWPF
{
///
/// UserControl1.xaml 的交互逻辑
///
public partial class HomePage : GWPageContentControl
{
//创建一个定时器
DispatcherTimer timer = new DispatcherTimer();
//初始化连接所需的USB或者COM端口
int iRetUSB = 3;
int iRetCOM = 3;
//相片的URL
public string uri = "D:/AlarmCenter/bin/zp.bmp";
public HomePage()
{
InitializeComponent();
timer.Tick += new EventHandler(timer_Tick);
timer.Interval = TimeSpan.FromMilliseconds(1000);//设置定时器循环时间
timer.Start();//开启定时器
}
private void timer_Tick(object sender, EventArgs e)
{
//若初始化连接成功
if (initComm())
{
authenticateAndReadSat.Text = string.Concat("初始化连接成功", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
//若卡认证和读卡成功
if (authenticateAndRead())
{
authenticateAndReadSat.Text = string.Concat("卡认证和读卡成功", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
fillData();
}
else
{
authenticateAndReadSat.Text = string.Concat("卡认证和读卡失败", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
}
}
else
{
authenticateAndReadSat.Text = string.Concat("初始化连接失败", DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));
}
}
private Boolean initComm()
{
bool flag_InitComm = false;
try
{
int iPort;
for (iPort = 1001; iPort <= 1016; iPort++)
{
iRetUSB = CVRSDK.CVR_InitComm(iPort);
if (iRetUSB == 1)
{
break;
}
}
if (iRetUSB != 1)
{
for (iPort = 1; iPort <= 4; iPort++)
{
iRetCOM = CVRSDK.CVR_InitComm(iPort);
if (iRetCOM == 1)
{
break;
}
}
}
if ((iRetCOM == 1) || (iRetUSB == 1))
{
flag_InitComm = true;
}
}
catch (Exception e)
{
exceptionText.Text = string.Concat("CVR_InitComm-catchException:", e.ToString());
return flag_InitComm;
}
return flag_InitComm;
}
private Boolean authenticateAndRead()
{
bool flag_authenticateAndRead = false;
int authenticate = CVRSDK.CVR_Authenticate();
// 若认证卡成功,才开始读卡
if (authenticate == 1)
{
checkImagExist();
int readContent = CVRSDK.CVR_Read_Content(4);
if (readContent == 1)
{
flag_authenticateAndRead = true;//读卡操作成功
}
}
return flag_authenticateAndRead;
}
private void checkImagExist() {
string filename = "D:\AlarmCenter\bin\zp.bmp";
//若存在该文件,则删除该文件
if (File.Exists(filename)) {
try
{
File.Delete(filename);
}
catch (Exception e)
{
exceptionText.Text = string.Concat("checkImagExist-catchException:", e.ToString());
}
}
}
public void fillData()
{
try
{
byte[] name = new byte[30];
int length = 30;
CVRSDK.GetPeopleName(ref name[0], ref length);
//MessageBox.Show();
byte[] number = new byte[30];
length = 36;
CVRSDK.GetPeopleIDCode(ref number[0], ref length);
byte[] people = new byte[30];
length = 3;
CVRSDK.GetPeopleNation(ref people[0], ref length);
byte[] validtermOfStart = new byte[30];
length = 16;
CVRSDK.GetStartDate(ref validtermOfStart[0], ref length);
byte[] birthday = new byte[30];
length = 16;
CVRSDK.GetPeopleBirthday(ref birthday[0], ref length);
byte[] address = new byte[30];
length = 70;
CVRSDK.GetPeopleAddress(ref address[0], ref length);
byte[] validtermOfEnd = new byte[30];
length = 16;
CVRSDK.GetEndDate(ref validtermOfEnd[0], ref length);
byte[] signdate = new byte[30];
length = 30;
CVRSDK.GetDepartment(ref signdate[0], ref length);
byte[] sex = new byte[30];
length = 3;
CVRSDK.GetPeopleSex(ref sex[0], ref length);
byte[] samid = new byte[32];
CVRSDK.CVR_GetSAMID(ref samid[0]);
BitmapImage bitmapImage = new BitmapImage(); //初始化BitmapImage类的一个新实例
bitmapImage.BeginInit(); //表示BitmapImage初始化开始
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.UriSource = new Uri(uri);//获取或设置BitmapImage的Uri源
bitmapImage.EndInit();//表示BitmapImage初始化结束
image1.Source = bitmapImage;
//姓名
name_TextBox.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(name).Replace("