Exemplo n.º 1
0
        public void Create(int width, int height, PixelFormat pixelFormat)
        {
            ag.Bitmap.Config config = ag.Bitmap.Config.Argb8888;
            switch (pixelFormat)
            {
            case PixelFormat.Format32bppRgb:
                throw new NotImplementedException();                         // TODO
                //config = ag.Bitmap.Config.Argb8888;
                break;

            case PixelFormat.Format24bppRgb:
                throw new NotImplementedException();                         // TODO
                //config = ag.Bitmap.Config.Argb8888;
                break;

            /*case PixelFormat.Format16bppRgb555:
             *      config = ag.Bitmap.Config.Argb8888;
             *      break;*/
            case PixelFormat.Format32bppRgba:
                config = ag.Bitmap.Config.Argb8888;
                break;

            default:
                throw new ArgumentOutOfRangeException("pixelFormat", pixelFormat, "Not supported");
            }

            Control = ag.Bitmap.CreateBitmap(width, height, config);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Convert a Bitmap to a Mat
 /// </summary>
 /// <param name="mat">The mat to copy Bitmap into</param>
 /// <param name="bitmap">The bitmap to copy into mat</param>
 public static void ToMat(this Android.Graphics.Bitmap bitmap, Mat mat)
 {
     Android.Graphics.Bitmap.Config config = bitmap.GetConfig();
     if (config.Equals(Android.Graphics.Bitmap.Config.Argb8888))
     {
         using (BitmapArgb8888Image bi = new BitmapArgb8888Image(bitmap))
         {
             CvInvoke.CvtColor(bi, mat, ColorConversion.Rgba2Bgra);
         }
     }
     else if (config.Equals(Android.Graphics.Bitmap.Config.Rgb565))
     {
         Size  size   = new Size(bitmap.Width, bitmap.Height);
         int[] values = new int[size.Width * size.Height];
         bitmap.GetPixels(values, 0, size.Width, 0, 0, size.Width, size.Height);
         GCHandle handle = GCHandle.Alloc(values, GCHandleType.Pinned);
         using (Mat bgra = new Mat(size, DepthType.Cv8U, 4, handle.AddrOfPinnedObject(), size.Width * 4))
         {
             bgra.CopyTo(mat);
         }
         handle.Free();
     }
     else
     {
         throw new NotImplementedException(String.Format("Coping from Bitmap of {0} is not implemented", config));
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Convert Image object to Bitmap
        /// </summary>
        /// <param name="config">The Bitmap Config</param>
        /// <returns>The Bitmap</returns>
        public Android.Graphics.Bitmap ToBitmap(Android.Graphics.Bitmap.Config config)
        {
            System.Drawing.Size size = 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(this);
                    //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(this);
                return(result);
            }
            else
            {
                throw new NotImplementedException("Only Bitmap config of Argb888 or Rgb565 is supported.");
            }
        }
 public ImageRequest(String url, IListener listener, int maxWidth, int maxHeight, Android.Widget.ImageView.ScaleType scaleType,
                     Android.Graphics.Bitmap.Config decodeConfig, IErrorListener errorListener)
     : base(Method.GET, url, errorListener)
 {
     SetRetryPolicy(new DefaultRetryPolicy(IMAGE_TIMEOUT_MS, IMAGE_MAX_RETRIES, IMAGE_BACKOFF_MULT));
     mListener     = listener;
     mDecodeConfig = decodeConfig;
     mMaxWidth     = maxWidth;
     mMaxHeight    = maxHeight;
     mScaleType    = scaleType;
 }
 public ImageRequest(String url, IListener listener, int maxWidth, int maxHeight, Android.Widget.ImageView.ScaleType scaleType,
     Android.Graphics.Bitmap.Config decodeConfig, IErrorListener errorListener)
     : base(Method.GET, url, errorListener)
 {
     SetRetryPolicy(new DefaultRetryPolicy(IMAGE_TIMEOUT_MS, IMAGE_MAX_RETRIES, IMAGE_BACKOFF_MULT));
     mListener = listener;
     mDecodeConfig = decodeConfig;
     mMaxWidth = maxWidth;
     mMaxHeight = maxHeight;
     mScaleType = scaleType;
 }
Exemplo n.º 6
0
        /// <summary>
        /// Convert the Mat to Bitmap
        /// </summary>
        /// <param name="mat">The Mat 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)
        {
            System.Drawing.Size size = mat.Size;

            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);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Convert the image to Bitmap
        /// </summary>
        /// <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 IInputArray image, Android.Graphics.Bitmap.Config config = null)
        {
            using (InputArray iaImage = image.GetInputArray())
            {
                System.Drawing.Size size = iaImage.GetSize();

                if (config == null)
                {
                    config = Android.Graphics.Bitmap.Config.Argb8888;
                }

                Android.Graphics.Bitmap result = Android.Graphics.Bitmap.CreateBitmap(size.Width, size.Height, config);
                image.ToBitmap(result);
                return(result);
            }
        }
Exemplo n.º 8
0
        /// <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.");
            }
        }
Exemplo n.º 9
0
        public static Bitmap drawTextToBitmap(Context gContext, int gResId, String gText)
        {
            Resources resources = gContext.Resources;
            float     scale     = resources.DisplayMetrics.Density;
            Bitmap    bitmap    =
                BitmapFactory.DecodeResource(resources, gResId);

            Android.Graphics.Bitmap.Config bitmapConfig =
                bitmap.GetConfig();
            // set default bitmap config if none
            if (bitmapConfig == null)
            {
                bitmapConfig = Android.Graphics.Bitmap.Config.Argb8888;
            }
            // resource bitmaps are imutable,
            // so we need to convert it to mutable one
            bitmap = bitmap.Copy(bitmapConfig, true);

            Canvas canvas = new Canvas(bitmap);
            // new antialised Paint
            //Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
            Paint paint = new Paint(PaintFlags.AntiAlias);

            // text color - #3D3D3D
            paint.Color = new Color(61, 61, 61);
            // text size in pixels
            paint.TextSize = ((int)(15 * scale));
            // text shadow
            //paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

            // draw text to the Canvas center
            Rect bounds = new Rect();

            paint.GetTextBounds(gText, 0, gText.Length, bounds);

            int x = (bitmap.Width - bounds.Width()) / 2;
            int y = (bitmap.Height + bounds.Height()) / 2;

            canvas.DrawText(gText, x - 5, y, paint);

            return(bitmap);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Convert the image to Bitmap
        /// </summary>
        /// <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 IInputArray image, Android.Graphics.Bitmap bitmap)
        {
            using (InputArray iaImage = image.GetInputArray())
            {
                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 = iaImage.GetChannels();
                    using (Mat m = new Mat(new Size(bitmap.Width, bitmap.Height), DepthType.Cv8U, 4, bitmap.LockPixels(), bitmap.RowBytes))
                    {
                        if (channels == 1)
                        {
                            CvInvoke.CvtColor(image, m, ColorConversion.Gray2Rgba);
                        }
                        else if (channels == 3)
                        {
                            CvInvoke.CvtColor(image, m, ColorConversion.Bgr2Rgba);
                        }
                        else if (channels == 4)
                        {
                            CvInvoke.CvtColor(image, m, ColorConversion.Bgra2Rgba);
                        }
                        else
                        {
                            throw new NotImplementedException(
                                      String.Format("InputArray of {0} channels is supported.", channels));
                        }
                        bitmap.UnlockPixels();
                    }
                }
                else if (config == Android.Graphics.Bitmap.Config.Rgb565)
                {
                    int channels = iaImage.GetChannels();
                    using (Mat m = new Mat(new Size(bitmap.Width, bitmap.Height), DepthType.Cv8U, 2, bitmap.LockPixels(), bitmap.RowBytes))
                    {
                        if (channels == 1)
                        {
                            CvInvoke.CvtColor(image, m, ColorConversion.Gray2Bgr565);
                        }
                        else if (channels == 3)
                        {
                            CvInvoke.CvtColor(image, m, ColorConversion.Bgr2Bgr565);
                        }
                        else if (channels == 4)
                        {
                            CvInvoke.CvtColor(image, m, ColorConversion.Bgra2Bgr565);
                        }
                        else
                        {
                            throw new NotImplementedException(
                                      String.Format("InputArray of {0} channels is supported.", channels));
                        }
                        bitmap.UnlockPixels();
                    }
                }
                else
                {
                    throw new NotImplementedException("Only Bitmap config of Argb888 or Rgb565 is supported.");
                }
            }
        }
 public ImageRequest(String url, IListener listener, int maxWidth, int maxHeight,
                     Android.Graphics.Bitmap.Config decodeConfig, IErrorListener errorListener)
     : this(url, listener, maxWidth, maxHeight, Android.Widget.ImageView.ScaleType.CenterInside, decodeConfig, errorListener)
 {
 }
Exemplo n.º 12
0
        // WHAT IS THE DEFAULT FONT FOR ANDROID?
                #endif

        // Partial class implementation of draw() in c# to allow use of unsafe code..
        public unsafe void draw(IBitmapDrawable source, flash.geom.Matrix matrix = null, ColorTransform colorTransform = null, string blendMode = null,
                                Rectangle clipRect = null, Boolean smoothing = false)
        {
                        #if PLATFORM_MONOMAC || PLATFORM_MONOTOUCH
            if (source is flash.text.TextField)
            {
                flash.text.TextField  tf     = source as flash.text.TextField;
                flash.text.TextFormat format = tf.defaultTextFormat;

                // $$TODO figure out how to get rid of this extra data copy
                var sizeToDraw = (width * height) << 2;
                if (sizeToDraw == 0)
                {
                    return;
                }

                string fontName = format.font;
                float  fontSize = (format.size is double) ? (float)(double)format.size : ((format.size is int) ? (float)(int)format.size : 10);

                // Check if the font is installed?
                bool hasFont = true;
                if (!sHasFont.TryGetValue(fontName, out hasFont))
                {
                                        #if PLATFORM_MONOTOUCH
                    UIFont font = UIFont.FromName(fontName, 10);

                    sHasFont[fontName] = hasFont = font != null;
                    if (font != null)
                    {
                        font.Dispose();
                    }
                                        #elif PLATFORM_MONOMAC
                    NSFont font = NSFont.FromFontName(fontName, 10);

                    sHasFont[fontName] = hasFont = font != null;
                    if (font != null)
                    {
                        font.Dispose();
                    }
                                        #else
                    sHasFont[fontName] = false;
                                        #endif
                }
                if (!hasFont)
                {
                    fontName = DEFAULT_FONT;
                }

                fixed(uint *data = mData)
                {
                    using (CGBitmapContext context = new CGBitmapContext(new IntPtr(data), width, height, 8, 4 * width,
                                                                         CGColorSpace.CreateDeviceRGB(),
                                                                         CGImageAlphaInfo.PremultipliedLast))
                    {
                        uint    tfColor = format.color != null ? (uint)(format.color) : 0;
                        float   r       = (float)((tfColor >> 16) & 0xFF) / 255.0f;
                        float   g       = (float)((tfColor >> 8) & 0xFF) / 255.0f;
                        float   b       = (float)((tfColor >> 0) & 0xFF) / 255.0f;
                        float   a       = (float)(tf.alpha);
                        CGColor color   = new CGColor(r, g, b, a);
                        context.SetFillColor(color);
                        context.SetStrokeColor(color);
                        context.SelectFont(fontName, fontSize, CGTextEncoding.MacRoman);
                        context.SetAllowsAntialiasing(((tf.antiAliasType as string) == flash.text.AntiAliasType.ADVANCED));

                        double x = matrix.tx;
                        double y = matrix.ty;

                        // invert y because the CG origin is bottom,left
                        y = height - tf.textHeight - y;

                        // align text
                        switch (format.align)
                        {
                        case TextFormatAlign.LEFT:
                            // no adjustment required
                            break;

                        case TextFormatAlign.CENTER:
                            // center x
                            x += width / 2;
                            x -= tf.textWidth / 2;
                            break;

                        case TextFormatAlign.RIGHT:
                            // right justify x
                            x += width;
                            x -= tf.textWidth;
                            break;

                        default:
                            throw new System.NotImplementedException();
                        }

                        // draw text
                        context.ShowTextAtPoint((float)x, (float)y, tf.text);
                    }
                }
            }
            else
                                #elif PLATFORM_MONODROID
            if (source is flash.text.TextField)
            {
                flash.text.TextField  tf     = source as flash.text.TextField;
                flash.text.TextFormat format = tf.defaultTextFormat;

                // $$TODO figure out how to get rid of this extra data copy
                var data = new byte[width * height * 4];
                System.Buffer.BlockCopy(mData, 0, data, 0, data.Length);

                Android.Graphics.Bitmap.Config config = Android.Graphics.Bitmap.Config.Argb8888;
                Android.Graphics.Bitmap        bitmap = Android.Graphics.Bitmap.CreateBitmap(width, height, config);

                Canvas canvas = new Canvas(bitmap);
                var    x      = matrix.tx;
                var    y      = matrix.ty;

                // invert y because the CG origin is bottom,left
                // y = height - tf.textHeight - y;

                // align text
                switch (format.align)
                {
                case TextFormatAlign.LEFT:
                    // no adjustment required
                    break;

                case TextFormatAlign.CENTER:
                    // center x
                    x += width / 2;
                    x -= tf.textWidth / 2;
                    break;

                case TextFormatAlign.RIGHT:
                    // right justify x
                    x += width;
                    x -= tf.textWidth;
                    break;

                default:
                    throw new System.NotImplementedException();
                }

                Paint paint = new Paint(PaintFlags.AntiAlias);

                paint.Color    = Color.Black;
                paint.TextSize = (float)format.size;
                paint.SetTypeface(Typeface.Create(format.font, TypefaceStyle.Normal));
                paint.TextAlign = Paint.Align.Center;

                canvas.DrawText(tf.text, (float)x, (float)y, paint);

                mData = new uint[bitmap.Width * bitmap.Height];
                var buffer = new int[bitmap.Width * bitmap.Height];
                bitmap.GetPixels(buffer, 0, width, 0, 0, width, height);

                for (int i = 0; i < buffer.Length; i++)
                {
                    mData[i] = (uint)buffer[i];
                }
            }

            else
                                #endif
            if (source is flash.display.BitmapData)
            {
                //naive implementation ,
                //to be implemented:
                // -smoothing / antialiasing,
                // -blend mode
                // -colorTransform
                // -cliprect
                BitmapData        sourceBitmap = source as BitmapData;
                flash.geom.Matrix matInverse   = (matrix != null) ? matrix.clone() : new flash.geom.Matrix();
                matInverse.invert();

                for (int y = 0; y < mHeight; y++)
                {
                    for (int x = 0; x < mWidth; x++)
                    {
                        int x2 = (int)(x * matInverse.a + y * matInverse.c + matInverse.tx);
                        int y2 = (int)(x * matInverse.b + y * matInverse.d + matInverse.ty);
                        if (x2 >= 0 && y2 >= 0 && x2 < sourceBitmap.width && y2 < sourceBitmap.height)
                        {
                            mData[x + y * mWidth] = sourceBitmap.mData[x2 + y2 * sourceBitmap.mWidth];
                        }
                    }
                }
            }
            else
            {
                _root.trace_fn.trace("NotImplementedWarning: BitmapData.draw()");
            }
        }