public Bitmap ToBitmap(Bitmap.Config config) { System.Drawing.Size size = Size; if (config == Bitmap.Config.Argb8888) { Bitmap result = Bitmap.CreateBitmap(size.Width, size.Height, Bitmap.Config.Argb8888); using (BitmapArgb8888Image bi = new BitmapArgb8888Image(result)) { bi.ConvertFrom(this); //CvInvoke.cvSet(bi, new MCvScalar(0, 0, 255, 255), IntPtr.Zero); } return(result); } else if (config == Bitmap.Config.Rgb565) { Bitmap result = Bitmap.CreateBitmap(size.Width, size.Height, Bitmap.Config.Rgb565); using (BitmapRgb565Image bi = new BitmapRgb565Image(result)) bi.ConvertFrom(this); return(result); } else { throw new NotImplementedException("Only Bitmap config of Argb888 or Rgb565 is supported."); } }
/// <summary> /// Convert Image object to Bitmap /// </summary> /// <param name="config">The Bitmap Config</param> /// <returns>The Bitmap</returns> public static Android.Graphics.Bitmap ToBitmap <TColor, TDepth>(this Image <TColor, TDepth> image, Android.Graphics.Bitmap.Config config) where TColor : struct, IColor where TDepth : new() { System.Drawing.Size size = image.Size; if (config == Android.Graphics.Bitmap.Config.Argb8888) { Android.Graphics.Bitmap result = Android.Graphics.Bitmap.CreateBitmap(size.Width, size.Height, Android.Graphics.Bitmap.Config.Argb8888); using (BitmapArgb8888Image bi = new BitmapArgb8888Image(result)) { bi.ConvertFrom(image); //CvInvoke.cvSet(bi, new MCvScalar(0, 0, 255, 255), IntPtr.Zero); } return(result); } else if (config == Android.Graphics.Bitmap.Config.Rgb565) { Android.Graphics.Bitmap result = Android.Graphics.Bitmap.CreateBitmap(size.Width, size.Height, Android.Graphics.Bitmap.Config.Rgb565); using (BitmapRgb565Image bi = new BitmapRgb565Image(result)) bi.ConvertFrom(image); return(result); } else { throw new NotImplementedException("Only Bitmap config of Argb888 or Rgb565 is supported."); } }
public Bitmap ToBitmap(Bitmap.Config config) { System.Drawing.Size size = Size; if (config == Bitmap.Config.Argb8888) { Bitmap result = Bitmap.CreateBitmap(size.Width, size.Height, Bitmap.Config.Argb8888); using (BitmapArgb8888Image bi = new BitmapArgb8888Image(result)) using (Image <Rgba, Byte> tmp = ToImage <Rgba, Byte>()) { tmp.Copy(bi, null); } return(result); } else if (config == Bitmap.Config.Rgb565) { Bitmap result = Bitmap.CreateBitmap(size.Width, size.Height, Bitmap.Config.Rgb565); using (BitmapRgb565Image bi = new BitmapRgb565Image(result)) using (Image <Bgr, Byte> tmp = ToImage <Bgr, Byte>()) bi.ConvertFrom(tmp); return(result); } else { throw new NotImplementedException("Only Bitmap config of Argb888 or Rgb565 is supported."); } }
/// <summary> /// Convert the Mat to Bitmap /// </summary> /// <param name="mat">The Mat 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) { System.Drawing.Size size = mat.Size; 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)) { if (channels == 1) { 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>()) { tmp.Copy(bi, null); } } } } 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."); } }
/// <summary> /// Convert the Mat to Bitmap /// </summary> /// <param name="config">The bitmap config type. If null, Argb8888 will be used</param> /// <returns>The Bitmap</returns> public Bitmap ToBitmap(Bitmap.Config config = null) { System.Drawing.Size size = Size; if (config == null || config == Bitmap.Config.Argb8888) { Bitmap result = Bitmap.CreateBitmap(size.Width, size.Height, Bitmap.Config.Argb8888); int channels = NumberOfChannels; using (BitmapArgb8888Image bi = new BitmapArgb8888Image(result)) { if (channels == 1) { CvInvoke.CvtColor(this, bi.Mat, ColorConversion.Gray2Rgba); } else if (channels == 3) { CvInvoke.CvtColor(this, bi, ColorConversion.Bgr2Rgba); } else if (channels == 4) { CvInvoke.CvtColor(this, bi, ColorConversion.Bgra2Rgba); } else { using (Image <Rgba, Byte> tmp = ToImage <Rgba, Byte>()) { tmp.Copy(bi, null); } } } return(result); } else if (config == Bitmap.Config.Rgb565) { Bitmap result = Bitmap.CreateBitmap(size.Width, size.Height, Bitmap.Config.Rgb565); using (BitmapRgb565Image bi = new BitmapRgb565Image(result)) using (Image <Bgr, Byte> tmp = ToImage <Bgr, Byte>()) bi.ConvertFrom(tmp); return(result); } else { throw new NotImplementedException("Only Bitmap config of Argb888 or Rgb565 is supported."); } }
protected override void OnDraw(Android.Graphics.Canvas canvas) { base.OnDraw(canvas); lock (this) { Image <Bgr, byte> image = _bgrBuffers.GetBuffer(0); if (image != null && !_imageSize.IsEmpty && canvas != null) { Stopwatch w = Stopwatch.StartNew(); if ((_bmpImage != null) && (!_imageSize.Equals(_bmpImage.Size))) { _bmpImage.Dispose(); _bmpImage = null; _bmp.Dispose(); _bmp = null; } if (_bmpImage == null) { _bmp = Android.Graphics.Bitmap.CreateBitmap(_imageSize.Width, _imageSize.Height, Android.Graphics.Bitmap.Config.Rgb565); _bmpImage = new BitmapRgb565Image(_bmp); } _bmpImage.ConvertFrom(image); canvas.DrawBitmap(_bmp, 0, 0, _paint); w.Stop(); _watch.Stop(); #if DEBUG canvas.DrawText(String.Format("{0:F2} FPS; {1}x{2}; Render Time: {3} ms", 1.0 / _watch.ElapsedMilliseconds * 1000, _imageSize.Width, _imageSize.Height, w.ElapsedMilliseconds), 20, 20, _paint); #endif _watch.Reset(); _watch.Start(); } } }