diff --git a/BITable/BITable.csproj b/BITable/BITable.csproj index 64af22f..d7fe48c 100644 --- a/BITable/BITable.csproj +++ b/BITable/BITable.csproj @@ -45,8 +45,8 @@ ..\packages\32feet.NET.3.5.0.0\lib\net\InTheHand.Net.Personal.dll - - ..\packages\Network.6.1.4.20\lib\net46\Network.dll + + ..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll diff --git a/BITable/BiTable.Designer.cs b/BITable/BiTable.Designer.cs index a981fcc..8380bcc 100644 --- a/BITable/BiTable.Designer.cs +++ b/BITable/BiTable.Designer.cs @@ -28,35 +28,201 @@ /// private void InitializeComponent() { - this.pictureBox1 = new System.Windows.Forms.PictureBox(); - ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); + this.pictureBoxFaceImage = new System.Windows.Forms.PictureBox(); + this.pictureBoxIDCard = new System.Windows.Forms.PictureBox(); + this.btnScanBluetoothDevices = new System.Windows.Forms.Button(); + this.btnFaceAndPaper = new System.Windows.Forms.Button(); + this.btnFace = new System.Windows.Forms.Button(); + this.btnFontPaper = new System.Windows.Forms.Button(); + this.btnBehindPaper = new System.Windows.Forms.Button(); + this.btnFaceCamera = new System.Windows.Forms.Button(); + this.btnPaperCamera = new System.Windows.Forms.Button(); + this.dgvBluetoothDevices = new System.Windows.Forms.DataGridView(); + this.BluetoothDeviceName = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.lblConnectedDevice = new System.Windows.Forms.Label(); + this.lblStatus = new System.Windows.Forms.Label(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxFaceImage)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxIDCard)).BeginInit(); + ((System.ComponentModel.ISupportInitialize)(this.dgvBluetoothDevices)).BeginInit(); this.SuspendLayout(); // - // pictureBox1 + // pictureBoxFaceImage // - this.pictureBox1.Location = new System.Drawing.Point(12, 12); - this.pictureBox1.Name = "pictureBox1"; - this.pictureBox1.Size = new System.Drawing.Size(350, 280); - this.pictureBox1.TabIndex = 0; - this.pictureBox1.TabStop = false; + this.pictureBoxFaceImage.BackColor = System.Drawing.SystemColors.GradientInactiveCaption; + this.pictureBoxFaceImage.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; + this.pictureBoxFaceImage.Location = new System.Drawing.Point(12, 12); + this.pictureBoxFaceImage.Name = "pictureBoxFaceImage"; + this.pictureBoxFaceImage.Size = new System.Drawing.Size(350, 350); + this.pictureBoxFaceImage.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; + this.pictureBoxFaceImage.TabIndex = 0; + this.pictureBoxFaceImage.TabStop = false; + // + // pictureBoxIDCard + // + this.pictureBoxIDCard.BackColor = System.Drawing.SystemColors.GradientInactiveCaption; + this.pictureBoxIDCard.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; + this.pictureBoxIDCard.Location = new System.Drawing.Point(12, 368); + this.pictureBoxIDCard.Name = "pictureBoxIDCard"; + this.pictureBoxIDCard.Size = new System.Drawing.Size(350, 237); + this.pictureBoxIDCard.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; + this.pictureBoxIDCard.TabIndex = 0; + this.pictureBoxIDCard.TabStop = false; + // + // btnScanBluetoothDevices + // + this.btnScanBluetoothDevices.Location = new System.Drawing.Point(395, 212); + this.btnScanBluetoothDevices.Name = "btnScanBluetoothDevices"; + this.btnScanBluetoothDevices.Size = new System.Drawing.Size(388, 38); + this.btnScanBluetoothDevices.TabIndex = 4; + this.btnScanBluetoothDevices.Text = "Quét thiết bị"; + this.btnScanBluetoothDevices.UseVisualStyleBackColor = true; + this.btnScanBluetoothDevices.Click += new System.EventHandler(this.btnScanBluetoothDevices_Click); + // + // btnFaceAndPaper + // + this.btnFaceAndPaper.Location = new System.Drawing.Point(396, 368); + this.btnFaceAndPaper.Name = "btnFaceAndPaper"; + this.btnFaceAndPaper.Size = new System.Drawing.Size(123, 43); + this.btnFaceAndPaper.TabIndex = 5; + this.btnFaceAndPaper.Text = "Mặt + Giấy tờ"; + this.btnFaceAndPaper.UseVisualStyleBackColor = true; + this.btnFaceAndPaper.Click += new System.EventHandler(this.btnFaceAndPaper_Click); + // + // btnFace + // + this.btnFace.Location = new System.Drawing.Point(660, 368); + this.btnFace.Name = "btnFace"; + this.btnFace.Size = new System.Drawing.Size(123, 43); + this.btnFace.TabIndex = 5; + this.btnFace.Text = "Mặt"; + this.btnFace.UseVisualStyleBackColor = true; + this.btnFace.Click += new System.EventHandler(this.btnFace_Click); + // + // btnFontPaper + // + this.btnFontPaper.Location = new System.Drawing.Point(396, 441); + this.btnFontPaper.Name = "btnFontPaper"; + this.btnFontPaper.Size = new System.Drawing.Size(123, 43); + this.btnFontPaper.TabIndex = 5; + this.btnFontPaper.Text = "Giấy tờ trước"; + this.btnFontPaper.UseVisualStyleBackColor = true; + this.btnFontPaper.Click += new System.EventHandler(this.btnFontPaper_Click); + // + // btnBehindPaper + // + this.btnBehindPaper.Location = new System.Drawing.Point(660, 441); + this.btnBehindPaper.Name = "btnBehindPaper"; + this.btnBehindPaper.Size = new System.Drawing.Size(123, 43); + this.btnBehindPaper.TabIndex = 5; + this.btnBehindPaper.Text = "Giấy tờ sau"; + this.btnBehindPaper.UseVisualStyleBackColor = true; + this.btnBehindPaper.Click += new System.EventHandler(this.btnBehindPaper_Click); + // + // btnFaceCamera + // + this.btnFaceCamera.Location = new System.Drawing.Point(396, 513); + this.btnFaceCamera.Name = "btnFaceCamera"; + this.btnFaceCamera.Size = new System.Drawing.Size(123, 43); + this.btnFaceCamera.TabIndex = 5; + this.btnFaceCamera.Text = "Cam khuôn mặt"; + this.btnFaceCamera.UseVisualStyleBackColor = true; + this.btnFaceCamera.Click += new System.EventHandler(this.btnFaceCamera_Click); + // + // btnPaperCamera + // + this.btnPaperCamera.Location = new System.Drawing.Point(660, 513); + this.btnPaperCamera.Name = "btnPaperCamera"; + this.btnPaperCamera.Size = new System.Drawing.Size(123, 43); + this.btnPaperCamera.TabIndex = 5; + this.btnPaperCamera.Text = "Cam giấy tờ"; + this.btnPaperCamera.UseVisualStyleBackColor = true; + this.btnPaperCamera.Click += new System.EventHandler(this.btnPaperCamera_Click); + // + // dgvBluetoothDevices + // + this.dgvBluetoothDevices.AllowUserToAddRows = false; + this.dgvBluetoothDevices.AllowUserToDeleteRows = false; + this.dgvBluetoothDevices.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.dgvBluetoothDevices.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { + this.BluetoothDeviceName}); + this.dgvBluetoothDevices.Location = new System.Drawing.Point(396, 13); + this.dgvBluetoothDevices.Name = "dgvBluetoothDevices"; + this.dgvBluetoothDevices.ReadOnly = true; + this.dgvBluetoothDevices.RowHeadersVisible = false; + this.dgvBluetoothDevices.RowTemplate.Height = 25; + this.dgvBluetoothDevices.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect; + this.dgvBluetoothDevices.Size = new System.Drawing.Size(387, 156); + this.dgvBluetoothDevices.TabIndex = 6; + this.dgvBluetoothDevices.CellMouseDoubleClick += new System.Windows.Forms.DataGridViewCellMouseEventHandler(this.dgvBluetoothDevices_CellMouseDoubleClick); + // + // BluetoothDeviceName + // + this.BluetoothDeviceName.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill; + this.BluetoothDeviceName.HeaderText = "Tên thiết bị Bluetooth"; + this.BluetoothDeviceName.Name = "BluetoothDeviceName"; + this.BluetoothDeviceName.ReadOnly = true; + // + // lblConnectedDevice + // + this.lblConnectedDevice.AutoSize = true; + this.lblConnectedDevice.Location = new System.Drawing.Point(395, 176); + this.lblConnectedDevice.Name = "lblConnectedDevice"; + this.lblConnectedDevice.Size = new System.Drawing.Size(171, 13); + this.lblConnectedDevice.TabIndex = 7; + this.lblConnectedDevice.Text = "Trạng thái: chưa kết nối tới thiết bị!"; + // + // lblStatus + // + this.lblStatus.AutoSize = true; + this.lblStatus.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.lblStatus.Location = new System.Drawing.Point(558, 291); + this.lblStatus.Name = "lblStatus"; + this.lblStatus.Size = new System.Drawing.Size(60, 24); + this.lblStatus.TabIndex = 8; + this.lblStatus.Text = "label1"; // // BiTable // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(1350, 729); - this.Controls.Add(this.pictureBox1); + this.ClientSize = new System.Drawing.Size(798, 616); + this.Controls.Add(this.lblStatus); + this.Controls.Add(this.lblConnectedDevice); + this.Controls.Add(this.dgvBluetoothDevices); + this.Controls.Add(this.btnPaperCamera); + this.Controls.Add(this.btnBehindPaper); + this.Controls.Add(this.btnFace); + this.Controls.Add(this.btnFaceCamera); + this.Controls.Add(this.btnFontPaper); + this.Controls.Add(this.btnFaceAndPaper); + this.Controls.Add(this.btnScanBluetoothDevices); + this.Controls.Add(this.pictureBoxIDCard); + this.Controls.Add(this.pictureBoxFaceImage); this.Name = "BiTable"; - this.Text = "BI Table"; - this.Load += new System.EventHandler(this.BiTable_Load); - ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); + this.Text = "Cam giấy tờ"; + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxFaceImage)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.pictureBoxIDCard)).EndInit(); + ((System.ComponentModel.ISupportInitialize)(this.dgvBluetoothDevices)).EndInit(); this.ResumeLayout(false); + this.PerformLayout(); } #endregion - private System.Windows.Forms.PictureBox pictureBox1; + private System.Windows.Forms.PictureBox pictureBoxFaceImage; + private System.Windows.Forms.PictureBox pictureBoxIDCard; + private System.Windows.Forms.Button btnScanBluetoothDevices; + private System.Windows.Forms.Button btnFaceAndPaper; + private System.Windows.Forms.Button btnFace; + private System.Windows.Forms.Button btnFontPaper; + private System.Windows.Forms.Button btnBehindPaper; + private System.Windows.Forms.Button btnFaceCamera; + private System.Windows.Forms.Button btnPaperCamera; + private System.Windows.Forms.DataGridView dgvBluetoothDevices; + private System.Windows.Forms.DataGridViewTextBoxColumn BluetoothDeviceName; + private System.Windows.Forms.Label lblConnectedDevice; + private System.Windows.Forms.Label lblStatus; } } diff --git a/BITable/BiTable.cs b/BITable/BiTable.cs index 982c941..301cdcb 100644 --- a/BITable/BiTable.cs +++ b/BITable/BiTable.cs @@ -1,25 +1,249 @@ using System; -using System.Collections.Generic; -using System.ComponentModel; using System.Data; -using System.Drawing; using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Windows.Forms; +using InTheHand.Net.Bluetooth; +using InTheHand.Net.Sockets; +using System.IO; +using System.Threading.Tasks; +using System.Drawing.Printing; +using Newtonsoft.Json; +using System.Drawing; + namespace BITable { public partial class BiTable : Form { + private BluetoothClient client; + private BluetoothDeviceInfo[] devices; + private string currentBluetoothDeviceName; + public BiTable() { InitializeComponent(); + BluetoothRadio.PrimaryRadio.Mode = RadioMode.Connectable; + client = new BluetoothClient(); + lblStatus.Text = string.Empty; } - private void BiTable_Load(object sender, EventArgs e) + private void ScanBluetoothDevices() { + devices = client.DiscoverDevices(); + foreach (var device in devices) + { + Console.WriteLine(device.DeviceName); + dgvBluetoothDevices.Invoke(new Action(() => + { + dgvBluetoothDevices.Rows.Add(device.DeviceName); + })); + } + } + private void btnScanBluetoothDevices_Click(object sender, EventArgs e) + { + Task.Factory.StartNew(new Action(() => + { + ScanBluetoothDevices(); + })); + } + + private void dgvBluetoothDevices_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) + { + currentBluetoothDeviceName = dgvBluetoothDevices.Rows[e.RowIndex].Cells[0].Value.ToString(); + var neededDevice = devices.Where(x => x.DeviceName == currentBluetoothDeviceName).FirstOrDefault(); + if (neededDevice != null) + { + client.BeginConnect(neededDevice.DeviceAddress, BluetoothService.SerialPort, new AsyncCallback(Connect), client); + } + } + + private void Connect(IAsyncResult result) + { + if (client.Connected) + { + SendRequestToDevice("{\"option\":\"0\", \"value\":\"0B8N62R9\"}\n"); + lblConnectedDevice.Invoke(new Action(() => + { + lblConnectedDevice.Text = $"Trạng thái: Đã kết nối tới {currentBluetoothDeviceName}"; + })); + } + else + { + lblConnectedDevice.Invoke(new Action(() => + { + lblConnectedDevice.Text = $"Trạng thái: Không thuể kết nối tới {currentBluetoothDeviceName}"; + })); + } + } + + private string SendRequestToDevice(string jsonObject) + { + Stream stream = client.GetStream(); + stream.ReadTimeout = 10000; + StreamWriter streamWriter = new StreamWriter(stream); + streamWriter.Write(jsonObject); //"{\"option\":\"0\", \"value\":\"0B8N62R9\"}\n" + streamWriter.Flush(); + StreamReader streamReader = new StreamReader(stream); + string result = streamReader.ReadLine(); + return result; + } + + private string CreateJsonString(int option, int timeout) + { + return "{\"option\":\"" + option + "\", \"timeout\":\"" + timeout + "\"}\n"; + } + + private void ShowImageOnUI(RequestResult requestResult) + { + foreach (var item in requestResult.Data) + { + if (!string.IsNullOrEmpty(item.Base64)) + { + if (item.Type == "MT" || item.Type == "MS" || item.Type == "PRE-CMT") + { + pictureBoxIDCard.Invoke(new Action(() => + { + pictureBoxIDCard.Image?.Dispose(); + pictureBoxIDCard.Image = ParseImageFromBase64(item.Base64); + })); + } + else + { + pictureBoxFaceImage.Invoke(new Action(() => + { + pictureBoxFaceImage.Image?.Dispose(); + pictureBoxFaceImage.Image = ParseImageFromBase64(item.Base64); + })); + } + } + } + switch (requestResult.Code) + { + case 2: + MessageBox.Show("Không chụp được ảnh chân dung", "Cảnh báo"); + break; + case 3: + MessageBox.Show("Không chụp được ảnh CMT (mặt trước hoặc măt sau)", "Cảnh báo"); + break; + case 4: + MessageBox.Show("Không chụp được ảnh chân dung và ảnh CMT", "Cảnh báo"); + break; + case 5: + MessageBox.Show("Không chụp được ảnh mặt trước CMT", "Cảnh báo"); + break; + case 6: + MessageBox.Show("Không chụp được ảnh mặt sau CMT", "Cảnh báo"); + break; + case 7: + MessageBox.Show("Không chụp được ảnh preview camera chân dung", "Cảnh báo"); + break; + case 8: + MessageBox.Show("Không chụp được ảnh preview CMT", "Cảnh báo"); + break; + case 400: + MessageBox.Show("Request bị sai cú pháp", "Cảnh báo"); + break; + case 401: + MessageBox.Show("Thiết bị đang xử lý request khác", "Cảnh báo"); + break; + } + } + + private void DoRequest(int option, bool isClearFaceImage, bool isClearIDCard) + { + Task.Factory.StartNew(new Action(() => + { + lblStatus.Invoke(new Action(() => + { + lblStatus.Text = "ĐANG XỬ LÝ"; + })); + if (isClearIDCard) + { + pictureBoxIDCard.Invoke(new Action(() => + { + pictureBoxIDCard.Image?.Dispose(); + pictureBoxIDCard.Image = null; + })); + } + + if (isClearFaceImage) + { + pictureBoxFaceImage.Invoke(new Action(() => + { + pictureBoxFaceImage.Image?.Dispose(); + pictureBoxFaceImage.Image = null; + })); + } + string jsonObject = CreateJsonString(option, 6); + string result = SendRequestToDevice(jsonObject); + var objectParsed = JsonConvert.DeserializeObject(result); + ShowImageOnUI(objectParsed); + })).ContinueWith(t => + { + lblStatus.Invoke(new Action(() => + { + lblStatus.Text = string.Empty; + })); + }); + } + + private void btnFaceAndPaper_Click(object sender, EventArgs e) + { + DoRequest(1, true, true); + } + + private Bitmap ParseImageFromBase64(string imageBase64) + { + byte[] imageData = Convert.FromBase64String(imageBase64); + using (var ms = new MemoryStream(imageData)) + { + Bitmap image = new Bitmap(ms); + return image; + } + } + + public class Base64Image + { + [JsonProperty("base64")] + public string Base64 { get; set; } + + [JsonProperty("type")] + public string Type { get; set; } + } + + public class RequestResult + { + [JsonProperty("data")] + public Base64Image[] Data { get; set; } + + [JsonProperty("code")] + public int Code { get; set; } + } + + private void btnFace_Click(object sender, EventArgs e) + { + DoRequest(2, true, false); + } + + private void btnFontPaper_Click(object sender, EventArgs e) + { + DoRequest(3, false, true); + } + + private void btnBehindPaper_Click(object sender, EventArgs e) + { + DoRequest(4, false, true); + } + + private void btnFaceCamera_Click(object sender, EventArgs e) + { + DoRequest(5, true, false); + } + + private void btnPaperCamera_Click(object sender, EventArgs e) + { + DoRequest(6, false, true); } } } diff --git a/BITable/BiTable.resx b/BITable/BiTable.resx index 1af7de1..286a97a 100644 --- a/BITable/BiTable.resx +++ b/BITable/BiTable.resx @@ -117,4 +117,7 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + True + \ No newline at end of file diff --git a/BITable/packages.config b/BITable/packages.config index 65c7954..6dc80e6 100644 --- a/BITable/packages.config +++ b/BITable/packages.config @@ -4,8 +4,8 @@ - +