123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- using System;
- using System.Windows;
- using System.Drawing;
- using Microsoft.Win32;
- using System.Windows.Media.Imaging;
- using System.Drawing.Imaging;
- using System.Runtime.InteropServices;
- using RasonicSignalDetectionCSLib;
- namespace RasonicSignalDetectionDemo
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- _imgorig = null;
- ImgOrig.Source = _imgorig;
- }
- RasonicSignalDetection _process = new RasonicSignalDetection();
- private BitmapImage _imgorig;
- private string _origImgFolder;
- private void DetectImage(object sender, RoutedEventArgs e)
- {
- try
- {
- OpenFileDialog openFileD = new OpenFileDialog();
- openFileD.Filter = "图片文件|*.png;*.bmp;*.jpg;*.jpeg";
- openFileD.Multiselect = false;
- openFileD.Title = "选择图像";
- if (openFileD.ShowDialog() ?? false)
- {
- Bitmap img = new Bitmap(Bitmap.FromFile(openFileD.FileName));
- RawImage rawImage = BitmapToRawImage(img);
- RasonicSignalDetectResult result = _process.DetectRasonicSignal(rawImage);
- if (result == RasonicSignalDetectResult.NoSignal)
- {
- StatusText1.Text = "无信号";
- }
- else if(result == RasonicSignalDetectResult.Signal)
- {
- StatusText1.Text = "有信号";
- }
- else
- {
- StatusText1.Text = "出现错误";
- }
- _imgorig = BitmapToBitmapImage(img);
- ImgOrig.Source = _imgorig; ;
- System.Drawing.Rectangle rect = System.Drawing.Rectangle.Empty;
- img.Dispose();
- }
- }
- catch (Exception excep)
- {
- MessageBox.Show("发生错误:" + excep.Message);
- }
- }
-
-
- /// <summary>
- /// 转为BitmapImage
- /// </summary>
- /// <param name="img"></param>
- /// <returns></returns>
- public static BitmapImage BitmapToBitmapImage( Bitmap img)
- {
- BitmapImage bmpimg = new BitmapImage();
- using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
- {
- img.Save(ms, ImageFormat.Png);
- bmpimg.BeginInit();
- bmpimg.StreamSource = ms;
- bmpimg.CacheOption = BitmapCacheOption.OnLoad;
- bmpimg.EndInit();
- bmpimg.Freeze();
- }
- return bmpimg;
- }
- /// <summary>
- /// 将System.Drawimg.Bitmap转为RawImage
- /// </summary>
- /// <param name="image"></param>
- /// <param name="keepChannel"></param>
- /// <returns></returns>
- public static RawImage BitmapToRawImage(Bitmap image, bool keepChannel = false)
- {
- int width = image.Width;
- int height = image.Height;
- System.Drawing.Imaging.PixelFormat pixelFormat = image.PixelFormat;
- if (pixelFormat != System.Drawing.Imaging.PixelFormat.Format24bppRgb && pixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb &&
- pixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppPArgb && pixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppRgb &&
- pixelFormat != System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
- {
- throw new Exception("Unexpected image pixel format:" + pixelFormat);
- }
- int origChannel = 3;
- if (pixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb)
- {
- origChannel = 3;
- }
- else if (pixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
- {
- origChannel = 1;
- }
- else
- {
- origChannel = 4;
- }
- int dstChannel = 3;
- if (keepChannel)
- {
- dstChannel = origChannel;
- }
- var bmData = image.LockBits(new System.Drawing.Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, pixelFormat);
- byte[] dstDataArray = new byte[width * height * dstChannel];
- unsafe
- {
- int stride = bmData.Stride;
- int dstStride = width * dstChannel;
- IntPtr ptr = bmData.Scan0;
- IntPtr ptrH, ptrW;
- for (int nh = 0; nh < height; nh++)
- {
- ptrH = IntPtr.Add(ptr, nh * stride);
- if (origChannel == dstChannel)
- {
- Marshal.Copy(ptrH, dstDataArray, nh * dstStride, dstStride);
- }
- else if (origChannel > dstChannel)
- {
- for (int nw = 0; nw < width; nw++)
- {
- ptrW = IntPtr.Add(ptrH, nw * origChannel);
- Marshal.Copy(ptrW, dstDataArray, nh * dstStride + nw * dstChannel, dstChannel);
- }
- }
- else
- {
- for (int nw = 0; nw < width; nw++)
- {
- ptrW = IntPtr.Add(ptrH, nw * origChannel);
- for (int nc = 0; nc < dstChannel; nc++)
- {
- Marshal.Copy(ptrW, dstDataArray, nh * dstStride + nw * dstChannel + nc, 1);
- }
- }
- }
- }
- }
- image.UnlockBits(bmData);
- return new RawImage(dstDataArray, width, height, dstChannel);
- }
-
- }
- }
|