设备对接动态获取图片不能动态切换问题

问题描述:通过和身份证读卡器对接获取基本信息和身份证照片信息,但切换身份证时,基本信息能随动变化,但照片不会。

代码:

xaml:

        <Image Name="image1" Height="197" Width="Auto" VerticalAlignment="Bottom" Visibility="Visible" Grid.Column="1" Grid.ColumnSpan="1" 
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
{
    /// <summary>
    /// UserControl1.xaml 的交互逻辑
    /// </summary>
    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("\0", "").Trim();
                //性别
                sex_TextBox.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(sex).Replace("\0", "").Trim();
                //民族
                nation.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(people).Replace("\0", "").Trim();
                //出生日期
                birthday_TextBox.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(birthday).Replace("\0", "").Trim();
                //身份证号
                id_card_number.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(number).Replace("\0", "").Trim();
                //地址
                address_TextBox.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(address).Replace("\0", "").Trim();
                //签发机关
                sign_organization.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(signdate).Replace("\0", "").Trim();
                //有效期限
                expiration_date.Text = System.Text.Encoding.GetEncoding("GB2312").GetString(validtermOfStart).Replace("\0", "").Trim() + "-" + System.Text.Encoding.GetEncoding("GB2312").GetString(validtermOfEnd).Replace("\0", "").Trim();
                //SAMID
                samid_TextBox.Text = "安全模块号:" + System.Text.Encoding.GetEncoding("GB2312").GetString(samid).Replace("\0", "").Trim();
                

            }
            catch (Exception e)
            {
                exceptionText.Text = string.Concat("FillData-catchException:", e.ToString());
            }
        }

        private void exitView_Click(object sender, RoutedEventArgs e)
        {
            timer.Stop();//关闭定时器
            CVRSDK.CVR_CloseComm();//关闭串口
        }
    }
}

解决方案:

将图片先读到内存中,不要使用URI的方式                    

 BitmapImage bitmapImage = new BitmapImage(); //初始化BitmapImage类的一个新实例
            bitmapImage.BeginInit(); //表示BitmapImage初始化开始
            bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
            
            var byteImage = File.ReadAllBytes("D:\\1.jpg");
            bitmapImage.StreamSource = new MemoryStream(byteImage);

            bitmapImage.EndInit();//表示BitmapImage初始化结束
            
            image.Source = bitmapImage;
            


或者:

image1.Source = ReadImageFile(uri);
 public BitmapSource ReadImageFile(string path)
        {
            
            FileStream fs = File.OpenRead(path); //OpenRead
            int filelength = 0;
            filelength = (int)fs.Length; //获得文件长度 
            Byte[] image = new Byte[filelength]; //建立一个字节数组 
            fs.Read(image, 0, filelength); //按字节流读取 
            //Image ima = new Image();
            BitmapSource bitsou = new ImageSourceConverter().ConvertFrom(image) as BitmapSource;
            fs.Close();
            return bitsou;
        }





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值