This commit is contained in:
DucDangAnh 2020-06-20 17:11:33 +07:00
parent 5a07b2710b
commit eac82673c8
5 changed files with 416 additions and 23 deletions

View File

@ -45,8 +45,8 @@
<Reference Include="InTheHand.Net.Personal, Version=3.5.605.0, Culture=neutral, PublicKeyToken=ea38caa273134499, processorArchitecture=MSIL"> <Reference Include="InTheHand.Net.Personal, Version=3.5.605.0, Culture=neutral, PublicKeyToken=ea38caa273134499, processorArchitecture=MSIL">
<HintPath>..\packages\32feet.NET.3.5.0.0\lib\net\InTheHand.Net.Personal.dll</HintPath> <HintPath>..\packages\32feet.NET.3.5.0.0\lib\net\InTheHand.Net.Personal.dll</HintPath>
</Reference> </Reference>
<Reference Include="Network, Version=6.1.4.20, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Network.6.1.4.20\lib\net46\Network.dll</HintPath> <HintPath>..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />

View File

@ -28,35 +28,201 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
this.pictureBox1 = new System.Windows.Forms.PictureBox(); this.pictureBoxFaceImage = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); 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(); this.SuspendLayout();
// //
// pictureBox1 // pictureBoxFaceImage
// //
this.pictureBox1.Location = new System.Drawing.Point(12, 12); this.pictureBoxFaceImage.BackColor = System.Drawing.SystemColors.GradientInactiveCaption;
this.pictureBox1.Name = "pictureBox1"; this.pictureBoxFaceImage.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.pictureBox1.Size = new System.Drawing.Size(350, 280); this.pictureBoxFaceImage.Location = new System.Drawing.Point(12, 12);
this.pictureBox1.TabIndex = 0; this.pictureBoxFaceImage.Name = "pictureBoxFaceImage";
this.pictureBox1.TabStop = false; 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 // BiTable
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1350, 729); this.ClientSize = new System.Drawing.Size(798, 616);
this.Controls.Add(this.pictureBox1); 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.Name = "BiTable";
this.Text = "BI Table"; this.Text = "Cam giấy tờ";
this.Load += new System.EventHandler(this.BiTable_Load); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxFaceImage)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.pictureBoxIDCard)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.dgvBluetoothDevices)).EndInit();
this.ResumeLayout(false); this.ResumeLayout(false);
this.PerformLayout();
} }
#endregion #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;
} }
} }

View File

@ -1,25 +1,249 @@
using System; using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data; using System.Data;
using System.Drawing;
using System.Linq; using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; 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 namespace BITable
{ {
public partial class BiTable : Form public partial class BiTable : Form
{ {
private BluetoothClient client;
private BluetoothDeviceInfo[] devices;
private string currentBluetoothDeviceName;
public BiTable() public BiTable()
{ {
InitializeComponent(); 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<RequestResult>(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);
} }
} }
} }

View File

@ -117,4 +117,7 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<metadata name="BluetoothDeviceName.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
</root> </root>

View File

@ -4,8 +4,8 @@
<package id="ConsoleTables" version="2.3.0" targetFramework="net461" /> <package id="ConsoleTables" version="2.3.0" targetFramework="net461" />
<package id="InTheHand.Pontoon" version="10.2018.3.19" targetFramework="net461" /> <package id="InTheHand.Pontoon" version="10.2018.3.19" targetFramework="net461" />
<package id="Microsoft.CSharp" version="4.5.0" targetFramework="net461" /> <package id="Microsoft.CSharp" version="4.5.0" targetFramework="net461" />
<package id="Network" version="6.1.4.20" targetFramework="net461" />
<package id="Network.Bluetooth" version="4.0.1803.22" targetFramework="net461" /> <package id="Network.Bluetooth" version="4.0.1803.22" targetFramework="net461" />
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net461" />
<package id="System.Net.Sockets" version="4.3.0" targetFramework="net461" /> <package id="System.Net.Sockets" version="4.3.0" targetFramework="net461" />
<package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net461" /> <package id="System.Runtime.InteropServices.RuntimeInformation" version="4.3.0" targetFramework="net461" />
<package id="System.Threading" version="4.3.0" targetFramework="net461" /> <package id="System.Threading" version="4.3.0" targetFramework="net461" />