|
|
@ -83,75 +83,77 @@ namespace Emgu.CV |
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Convert the Mat to Bitmap
|
|
|
|
/// Convert the image to Bitmap
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="mat">The Mat to convert to Bitmap</param>
|
|
|
|
/// <param name="image">The image to convert to Bitmap</param>
|
|
|
|
/// <param name="config">The bitmap config type. If null, Argb8888 will be used</param>
|
|
|
|
/// <returns>The Bitmap</returns>
|
|
|
|
public static Android.Graphics.Bitmap ToBitmap(this Mat mat, Android.Graphics.Bitmap.Config config = null) |
|
|
|
public static Android.Graphics.Bitmap ToBitmap(this IInputArray image, Android.Graphics.Bitmap.Config config = null) |
|
|
|
{ |
|
|
|
System.Drawing.Size size = mat.Size; |
|
|
|
using (InputArray iaImage = image.GetInputArray()) |
|
|
|
{ |
|
|
|
System.Drawing.Size size = iaImage.GetSize(); |
|
|
|
|
|
|
|
if (config == null) |
|
|
|
config = Android.Graphics.Bitmap.Config.Argb8888; |
|
|
|
if (config == null) |
|
|
|
config = Android.Graphics.Bitmap.Config.Argb8888; |
|
|
|
|
|
|
|
Android.Graphics.Bitmap result = Android.Graphics.Bitmap.CreateBitmap(size.Width, size.Height, config); |
|
|
|
mat.ToBitmap(result); |
|
|
|
return result; |
|
|
|
Android.Graphics.Bitmap result = Android.Graphics.Bitmap.CreateBitmap(size.Width, size.Height, config); |
|
|
|
image.ToBitmap(result); |
|
|
|
return result; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Convert the Mat to Bitmap
|
|
|
|
/// Convert the image to Bitmap
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="mat">The Mat to convert to Bitmap</param>
|
|
|
|
/// <param name="image">The image to convert to Bitmap</param>
|
|
|
|
/// <param name="bitmap">The bitmap, must be of the same size and has bitmap config type of either Argb888 or Rgb565</param>
|
|
|
|
/// <returns>The Bitmap</returns>
|
|
|
|
public static void ToBitmap(this Mat mat, Android.Graphics.Bitmap bitmap) |
|
|
|
public static void ToBitmap(this IInputArray image, Android.Graphics.Bitmap bitmap) |
|
|
|
{ |
|
|
|
System.Drawing.Size size = mat.Size; |
|
|
|
if (!(size.Width == bitmap.Width && size.Height == bitmap.Height)) |
|
|
|
using (InputArray iaImage = image.GetInputArray()) |
|
|
|
{ |
|
|
|
throw new Exception("Bitmap size doesn't match the Mat size"); |
|
|
|
} |
|
|
|
System.Drawing.Size size = iaImage.GetSize(); |
|
|
|
if (!(size.Width == bitmap.Width && size.Height == bitmap.Height)) |
|
|
|
{ |
|
|
|
throw new Exception("Bitmap size doesn't match the Mat size"); |
|
|
|
} |
|
|
|
|
|
|
|
Android.Graphics.Bitmap.Config config = bitmap.GetConfig(); |
|
|
|
if (config == Android.Graphics.Bitmap.Config.Argb8888) |
|
|
|
{ |
|
|
|
int channels = mat.NumberOfChannels; |
|
|
|
using (BitmapArgb8888Image bi = new BitmapArgb8888Image(bitmap)) |
|
|
|
Android.Graphics.Bitmap.Config config = bitmap.GetConfig(); |
|
|
|
if (config == Android.Graphics.Bitmap.Config.Argb8888) |
|
|
|
{ |
|
|
|
if (channels == 1) |
|
|
|
int channels = iaImage.GetChannels(); |
|
|
|
using (BitmapArgb8888Image bi = new BitmapArgb8888Image(bitmap)) |
|
|
|
{ |
|
|
|
CvInvoke.CvtColor(mat, bi.Mat, ColorConversion.Gray2Rgba); |
|
|
|
} |
|
|
|
else if (channels == 3) |
|
|
|
{ |
|
|
|
CvInvoke.CvtColor(mat, bi, ColorConversion.Bgr2Rgba); |
|
|
|
} |
|
|
|
else if (channels == 4) |
|
|
|
{ |
|
|
|
CvInvoke.CvtColor(mat, bi, ColorConversion.Bgra2Rgba); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
using (Image<Rgba, Byte> tmp = mat.ToImage<Rgba, Byte>()) |
|
|
|
if (channels == 1) |
|
|
|
{ |
|
|
|
tmp.Copy(bi, null); |
|
|
|
CvInvoke.CvtColor(image, bi.Mat, ColorConversion.Gray2Rgba); |
|
|
|
} |
|
|
|
else if (channels == 3) |
|
|
|
{ |
|
|
|
CvInvoke.CvtColor(image, bi, ColorConversion.Bgr2Rgba); |
|
|
|
} |
|
|
|
else if (channels == 4) |
|
|
|
{ |
|
|
|
CvInvoke.CvtColor(image, bi, ColorConversion.Bgra2Rgba); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
throw new NotImplementedException( |
|
|
|
String.Format("InputArray of {0} channels is supported.", channels)); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
else if (config == Android.Graphics.Bitmap.Config.Rgb565) |
|
|
|
{ |
|
|
|
using (BitmapRgb565Image bi = new BitmapRgb565Image(bitmap)) |
|
|
|
using (Image<Bgr, Byte> tmp = mat.ToImage<Bgr, Byte>()) |
|
|
|
bi.ConvertFrom(tmp); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
throw new NotImplementedException("Only Bitmap config of Argb888 or Rgb565 is supported."); |
|
|
|
} |
|
|
|
else if (config == Android.Graphics.Bitmap.Config.Rgb565) |
|
|
|
{ |
|
|
|
using (BitmapRgb565Image bi = new BitmapRgb565Image(bitmap)) |
|
|
|
bi.ConvertFrom(image); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
throw new NotImplementedException("Only Bitmap config of Argb888 or Rgb565 is supported."); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|