|
@@ -1,16 +1,14 @@
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.Drawing;
|
|
|
+using System.Drawing.Imaging;
|
|
|
+using System.IO;
|
|
|
+using System.IO.Ports;
|
|
|
using System.Linq;
|
|
|
-using System.Text;
|
|
|
-using System.Threading.Tasks;
|
|
|
using System.Windows;
|
|
|
-using System.Windows.Controls;
|
|
|
-using System.Windows.Data;
|
|
|
-using System.Windows.Documents;
|
|
|
-using System.Windows.Input;
|
|
|
+using System.Windows.Forms;
|
|
|
using System.Windows.Media;
|
|
|
using System.Windows.Media.Imaging;
|
|
|
-using System.Windows.Shapes;
|
|
|
|
|
|
namespace FaceChange
|
|
|
{
|
|
@@ -19,9 +17,202 @@ namespace FaceChange
|
|
|
/// </summary>
|
|
|
public partial class SettingsWindow : Window
|
|
|
{
|
|
|
+ AppContext appContext = AppContext.GetAppContext();
|
|
|
+ SerialTool serialTool = SerialTool.GetSerialTool();
|
|
|
+
|
|
|
+ private List<FaceInfo> faces;
|
|
|
+
|
|
|
public SettingsWindow()
|
|
|
{
|
|
|
InitializeComponent();
|
|
|
+
|
|
|
+ faces = appContext.FaceInfos;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Window_Loaded(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ if (appContext.PortOpen)
|
|
|
+ {
|
|
|
+ Devices.Items.Clear();
|
|
|
+ Devices.Items.Add(appContext.SerialPort.PortName);
|
|
|
+ Devices.SelectedIndex = 0;
|
|
|
+ Devices.IsEnabled = false;
|
|
|
+ Connect.Content = "断开连接";
|
|
|
+ Scan.IsEnabled = false;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Connect.Content = "连接设备";
|
|
|
+ Scan_Click(null, null);
|
|
|
+ }
|
|
|
+ // 读取配置
|
|
|
+ var prop = Properties.Settings.Default.Faces;
|
|
|
+ if (prop != null && !prop.Trim().Equals(""))
|
|
|
+ {
|
|
|
+ var faceStringList = prop.Split('|').ToList();
|
|
|
+ foreach (var faceString in faceStringList)
|
|
|
+ {
|
|
|
+ var fileName = faceString.Substring(
|
|
|
+ faceString.LastIndexOf(@"\", StringComparison.Ordinal) + 1);
|
|
|
+ try
|
|
|
+ {
|
|
|
+ faces.Add(new FaceInfo(
|
|
|
+ new BitmapImage(new Uri(faceString)),
|
|
|
+ fileName,
|
|
|
+ faceString.Replace(fileName, @"")));
|
|
|
+ }
|
|
|
+ catch (Exception exception)
|
|
|
+ {
|
|
|
+ Console.WriteLine(exception);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ foreach (var face in faces)
|
|
|
+ {
|
|
|
+ ListBox.Items.Add(face);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ FaceCount.Content = ListBox.Items.Count.ToString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
|
|
|
+ {
|
|
|
+ var list = new List<string>();
|
|
|
+ foreach (var face in faces)
|
|
|
+ {
|
|
|
+ list.Add(face.Path + face.Name);
|
|
|
+ }
|
|
|
+ Properties.Settings.Default.Faces = string.Join("|", list);
|
|
|
+ Properties.Settings.Default.Save();
|
|
|
+ Properties.Settings.Default.Reload();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static BitmapImage BitmapToBitmapImage(Bitmap bitmap)
|
|
|
+ {
|
|
|
+ using (MemoryStream stream = new MemoryStream())
|
|
|
+ {
|
|
|
+ bitmap.Save(stream, ImageFormat.Png);
|
|
|
+ stream.Position = 0;
|
|
|
+ BitmapImage result = new BitmapImage();
|
|
|
+ result.BeginInit();
|
|
|
+ result.CacheOption = BitmapCacheOption.OnLoad;
|
|
|
+ result.StreamSource = stream;
|
|
|
+ result.EndInit();
|
|
|
+ result.Freeze();
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Scan_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ // 清除当前串口号中的所有串口名称
|
|
|
+ Devices.Items.Clear();
|
|
|
+ var portList = serialTool.FindPort();
|
|
|
+ if (portList.Count == 0)
|
|
|
+ {
|
|
|
+ System.Windows.Forms.MessageBox.Show("没有找到可用设备!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ portList.ForEach(
|
|
|
+ delegate (string portName) {
|
|
|
+ Devices.Items.Add(portName);
|
|
|
+ });
|
|
|
+ //使 ListBox 显示第1个添加的索引
|
|
|
+ Devices.SelectedIndex = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Connect_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ if (appContext.PortOpen)
|
|
|
+ {
|
|
|
+ // 关闭串口
|
|
|
+ if (serialTool.ClosePort(appContext.SerialPort))
|
|
|
+ {
|
|
|
+ Devices.IsEnabled = true;
|
|
|
+ Scan.IsEnabled = true;
|
|
|
+ Scan_Click(null, null);
|
|
|
+ Connect.Content = "连接设备";
|
|
|
+ appContext.SerialPort = null;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ System.Windows.Forms.MessageBox.Show("断开连接失败!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ // 检测串口设置
|
|
|
+ string portName = Devices.Text.Trim();
|
|
|
+ if (portName == "")
|
|
|
+ {
|
|
|
+ System.Windows.Forms.MessageBox.Show("没有发现可连接设备!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 打开串口
|
|
|
+ SerialPort serialPort = serialTool.OpenPort(portName, "9600", "8", "1", "无", appContext.DataReceived);
|
|
|
+ if (serialPort == null)
|
|
|
+ {
|
|
|
+ // 打开串口失败
|
|
|
+ System.Windows.Forms.MessageBox.Show("连接设备失败!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Devices.IsEnabled = false;
|
|
|
+ Scan.IsEnabled = false;
|
|
|
+ Connect.Content = "断开连接";
|
|
|
+ appContext.SerialPort = serialPort;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Delete_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ var index = ListBox.SelectedIndex;
|
|
|
+ if (index == -1)
|
|
|
+ {
|
|
|
+ System.Windows.Forms.MessageBox.Show("请先选一项!", "提示", MessageBoxButtons.OK);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ faces.RemoveAt(index);
|
|
|
+ ListBox.Items.RemoveAt(index);
|
|
|
+ FaceCount.Content = ListBox.Items.Count.ToString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void DeleteALL_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ var messageBoxButtons = MessageBoxButtons.OKCancel;
|
|
|
+ var dr = System.Windows.Forms.MessageBox.Show("将清空列表,确定吗?", "提示", messageBoxButtons, MessageBoxIcon.Warning);
|
|
|
+ if (dr != System.Windows.Forms.DialogResult.OK) return;
|
|
|
+ faces.Clear();
|
|
|
+ ListBox.Items.Clear();
|
|
|
+ FaceCount.Content = ListBox.Items.Count.ToString();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Add_Click(object sender, RoutedEventArgs e)
|
|
|
+ {
|
|
|
+ var ofd = new OpenFileDialog
|
|
|
+ {
|
|
|
+ Filter = "图片资源(所有类型)|*.jpg;*.jpeg;*.png;*.bmp|所有文件|*.*",
|
|
|
+ ValidateNames = true,
|
|
|
+ CheckPathExists = true,
|
|
|
+ CheckFileExists = true,
|
|
|
+ Multiselect = true
|
|
|
+ };
|
|
|
+ if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
|
|
|
+ var strFileNames = ofd.FileNames;
|
|
|
+ foreach (var strFileName in strFileNames)
|
|
|
+ {
|
|
|
+ var fileName = strFileName.Substring(
|
|
|
+ strFileName.LastIndexOf(@"\", StringComparison.Ordinal) + 1);
|
|
|
+ var faceInfo = new FaceInfo(
|
|
|
+ new BitmapImage(new Uri(strFileName)),
|
|
|
+ fileName,
|
|
|
+ strFileName.Replace(fileName, @""));
|
|
|
+ ListBox.Items.Add(faceInfo);
|
|
|
+ faces.Add(faceInfo);
|
|
|
+ }
|
|
|
+ FaceCount.Content = ListBox.Items.Count.ToString();
|
|
|
}
|
|
|
}
|
|
|
}
|