using System; using System.Data; using System.Linq; using System.Windows.Forms; using InTheHand.Net.Bluetooth; using InTheHand.Net.Sockets; using System.IO; using System.Threading.Tasks; 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 ScanBluetoothDevices() { dgvBluetoothDevices.Invoke(new Action(() => { dgvBluetoothDevices.Rows.Clear(); })); 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(() => { btnScanBluetoothDevices.Invoke(new Action(() => { btnScanBluetoothDevices.Enabled = false; })); ScanBluetoothDevices(); })).ContinueWith(t => { btnScanBluetoothDevices.Invoke(new Action(() => { btnScanBluetoothDevices.Enabled = true; })); }); } 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); } } }