MainWindow.xaml.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Windows;
  3. using System.Drawing;
  4. using Microsoft.Win32;
  5. using System.Windows.Media.Imaging;
  6. using System.Drawing.Imaging;
  7. using System.Runtime.InteropServices;
  8. using RasonicSignalDetectionCSLib;
  9. namespace RasonicSignalDetectionDemo
  10. {
  11. /// <summary>
  12. /// MainWindow.xaml 的交互逻辑
  13. /// </summary>
  14. public partial class MainWindow : Window
  15. {
  16. public MainWindow()
  17. {
  18. InitializeComponent();
  19. _imgorig = null;
  20. ImgOrig.Source = _imgorig;
  21. }
  22. RasonicSignalDetection _process = new RasonicSignalDetection();
  23. private BitmapImage _imgorig;
  24. private string _origImgFolder;
  25. private void DetectImage(object sender, RoutedEventArgs e)
  26. {
  27. try
  28. {
  29. OpenFileDialog openFileD = new OpenFileDialog();
  30. openFileD.Filter = "图片文件|*.png;*.bmp;*.jpg;*.jpeg";
  31. openFileD.Multiselect = false;
  32. openFileD.Title = "选择图像";
  33. if (openFileD.ShowDialog() ?? false)
  34. {
  35. Bitmap img = new Bitmap(Bitmap.FromFile(openFileD.FileName));
  36. RawImage rawImage = BitmapToRawImage(img);
  37. RasonicSignalDetectResult result = _process.DetectRasonicSignal(rawImage);
  38. if (result == RasonicSignalDetectResult.NoSignal)
  39. {
  40. StatusText1.Text = "无信号";
  41. }
  42. else if(result == RasonicSignalDetectResult.Signal)
  43. {
  44. StatusText1.Text = "有信号";
  45. }
  46. else
  47. {
  48. StatusText1.Text = "出现错误";
  49. }
  50. _imgorig = BitmapToBitmapImage(img);
  51. ImgOrig.Source = _imgorig; ;
  52. System.Drawing.Rectangle rect = System.Drawing.Rectangle.Empty;
  53. img.Dispose();
  54. }
  55. }
  56. catch (Exception excep)
  57. {
  58. MessageBox.Show("发生错误:" + excep.Message);
  59. }
  60. }
  61. /// <summary>
  62. /// 转为BitmapImage
  63. /// </summary>
  64. /// <param name="img"></param>
  65. /// <returns></returns>
  66. public static BitmapImage BitmapToBitmapImage( Bitmap img)
  67. {
  68. BitmapImage bmpimg = new BitmapImage();
  69. using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
  70. {
  71. img.Save(ms, ImageFormat.Png);
  72. bmpimg.BeginInit();
  73. bmpimg.StreamSource = ms;
  74. bmpimg.CacheOption = BitmapCacheOption.OnLoad;
  75. bmpimg.EndInit();
  76. bmpimg.Freeze();
  77. }
  78. return bmpimg;
  79. }
  80. /// <summary>
  81. /// 将System.Drawimg.Bitmap转为RawImage
  82. /// </summary>
  83. /// <param name="image"></param>
  84. /// <param name="keepChannel"></param>
  85. /// <returns></returns>
  86. public static RawImage BitmapToRawImage(Bitmap image, bool keepChannel = false)
  87. {
  88. int width = image.Width;
  89. int height = image.Height;
  90. System.Drawing.Imaging.PixelFormat pixelFormat = image.PixelFormat;
  91. if (pixelFormat != System.Drawing.Imaging.PixelFormat.Format24bppRgb && pixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb &&
  92. pixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppPArgb && pixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppRgb &&
  93. pixelFormat != System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
  94. {
  95. throw new Exception("Unexpected image pixel format:" + pixelFormat);
  96. }
  97. int origChannel = 3;
  98. if (pixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb)
  99. {
  100. origChannel = 3;
  101. }
  102. else if (pixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed)
  103. {
  104. origChannel = 1;
  105. }
  106. else
  107. {
  108. origChannel = 4;
  109. }
  110. int dstChannel = 3;
  111. if (keepChannel)
  112. {
  113. dstChannel = origChannel;
  114. }
  115. var bmData = image.LockBits(new System.Drawing.Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, pixelFormat);
  116. byte[] dstDataArray = new byte[width * height * dstChannel];
  117. unsafe
  118. {
  119. int stride = bmData.Stride;
  120. int dstStride = width * dstChannel;
  121. IntPtr ptr = bmData.Scan0;
  122. IntPtr ptrH, ptrW;
  123. for (int nh = 0; nh < height; nh++)
  124. {
  125. ptrH = IntPtr.Add(ptr, nh * stride);
  126. if (origChannel == dstChannel)
  127. {
  128. Marshal.Copy(ptrH, dstDataArray, nh * dstStride, dstStride);
  129. }
  130. else if (origChannel > dstChannel)
  131. {
  132. for (int nw = 0; nw < width; nw++)
  133. {
  134. ptrW = IntPtr.Add(ptrH, nw * origChannel);
  135. Marshal.Copy(ptrW, dstDataArray, nh * dstStride + nw * dstChannel, dstChannel);
  136. }
  137. }
  138. else
  139. {
  140. for (int nw = 0; nw < width; nw++)
  141. {
  142. ptrW = IntPtr.Add(ptrH, nw * origChannel);
  143. for (int nc = 0; nc < dstChannel; nc++)
  144. {
  145. Marshal.Copy(ptrW, dstDataArray, nh * dstStride + nw * dstChannel + nc, 1);
  146. }
  147. }
  148. }
  149. }
  150. }
  151. image.UnlockBits(bmData);
  152. return new RawImage(dstDataArray, width, height, dstChannel);
  153. }
  154. }
  155. }