using OpenCvSharp;
|
using OpenCvSharp.WpfExtensions;
|
using System;
|
using System.Collections.Generic;
|
using System.Collections.ObjectModel;
|
using System.Collections.Specialized;
|
using System.IO;
|
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.Media;
|
using System.Windows.Media.Imaging;
|
using System.Windows.Shapes;
|
|
namespace SmartScanner
|
{
|
/// <summary>
|
/// ImageDisplayWindow.xaml 的交互逻辑
|
/// </summary>
|
public partial class ImageDisplayWindow : System.Windows.Window
|
{
|
private List<string> _imagePaths = new List<string>();
|
private int _currentIndex = -1;
|
public ImageDisplayWindow()
|
{
|
InitializeComponent();
|
}
|
public void AddImage(List<string> imagePath)
|
{
|
for (int i = 0; i < imagePath.Count; i++)
|
{
|
if (!File.Exists(imagePath[i]))
|
{
|
MessageBox.Show($"图片不存在: {imagePath}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
return;
|
}
|
}
|
// 添加到列表
|
//_imagePaths = imagePath;
|
_imagePaths = new List<string>(imagePath);
|
ShowImage(0);
|
}
|
|
private void ShowImage(int index)
|
{
|
if (index < 0 || index >= _imagePaths.Count) return;
|
|
_currentIndex = index;
|
string imagePath = _imagePaths[index];
|
|
try
|
{
|
// 使用OpenCvSharp读取图片
|
using (Mat image = Cv2.ImRead(imagePath, ImreadModes.Color))
|
{
|
if (image.Empty())
|
{
|
throw new Exception("图片加载失败");
|
}
|
|
// 使用Dispatcher确保UI线程更新
|
Dispatcher.Invoke(() =>
|
{
|
ResultImage.Source = image.ToBitmapSource();
|
Title = $"图片查看 ({index + 1}/{_imagePaths.Count}) - {System.IO.Path.GetFileName(imagePath)}";
|
|
// 更新按钮状态
|
PrevButton.IsEnabled = index > 0;
|
NextButton.IsEnabled = index < _imagePaths.Count - 1;
|
});
|
}
|
}
|
catch (Exception ex)
|
{
|
MessageBox.Show($"加载图片失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
}
|
}
|
|
private void PrevButton_Click(object sender, RoutedEventArgs e)
|
{
|
ShowImage(_currentIndex - 1);
|
}
|
|
private void NextButton_Click(object sender, RoutedEventArgs e)
|
{
|
ShowImage(_currentIndex + 1);
|
}
|
public bool ShowImage_main(string imagePath)
|
{
|
try
|
{
|
// 使用OpenCvSharp读取图片
|
using (Mat image = Cv2.ImRead(imagePath, ImreadModes.Color))
|
{
|
if (image.Empty())
|
{
|
return false;
|
}
|
|
// 使用Dispatcher确保UI线程更新
|
Dispatcher.Invoke(() =>
|
{
|
ResultImage.Source = image.ToBitmapSource();
|
Title = $"图片查看:{System.IO.Path.GetFileName(imagePath)}";
|
|
// 更新按钮状态
|
PrevButton.IsEnabled = false;
|
NextButton.IsEnabled = false;
|
});
|
}
|
}
|
catch (Exception ex)
|
{
|
MessageBox.Show($"加载图片失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
return false;
|
}
|
return true;
|
}
|
}
|
}
|