コード例 #1
0
        private void SaveSectorImage()
        {
            Bitmap.Config config = Bitmap.Config.Argb8888;

            Bitmap newBitmap = Bitmap.CreateBitmap(1000, 1000, config);
            int    cellSize  = 100;
            Canvas canvas    = new Canvas(newBitmap);
            Rect   destRect  = new Rect(0, 0, 1024, 1024);
            Bitmap bkgnd     = BitmapHelper.GetImageBitmapFromUrl(parent.pop.curSector.DefaultUrl);
            Rect   srcRect   = new Rect(0, 0, bkgnd.Width, bkgnd.Height);
            Paint  thePaint  = new Paint(PaintFlags.AntiAlias);

            canvas.DrawBitmap(bkgnd, srcRect, destRect, thePaint);
            SectorObj curSector = parent.pop.curSector;

            foreach (StructureObj curStructure in parent.pop.curSector.structures)
            {
                bkgnd    = BitmapHelper.GetImageBitmapFromUrl(curStructure.imageURL);
                srcRect  = new Rect(0, 0, bkgnd.Width, bkgnd.Height);
                destRect = new Rect(curStructure.xLoc * cellSize, curStructure.yLoc * cellSize,
                                    (curStructure.xLoc + curStructure.xSize) * cellSize,
                                    (curStructure.yLoc + curStructure.ySize) * cellSize);
                canvas.DrawBitmap(bkgnd, srcRect, destRect, thePaint);
            }
            // at this point, the bitmap should be drawn
            using (System.IO.MemoryStream photoStream = new System.IO.MemoryStream())
            {
                newBitmap.Compress(Bitmap.CompressFormat.Jpeg, 90, photoStream);
                photoStream.Flush();
                PhabrikServer.UploadImage(photoStream, "sector", (newURL) =>
                {
                    parent.UpdateSectorUrl(curSector, newURL);
                });
            }
        }
コード例 #2
0
ファイル: AndroidImageCodec.cs プロジェクト: suntabu/axiom
        private Media.PixelFormat Convert(Bitmap.Config config)
        {
            if (config != null && config.Name() != null)
            {
                switch (config.Name().ToLower())
                {
                case "alpha_8":
                    return(Media.PixelFormat.A8);

                case "rgb_565":
                    return(Media.PixelFormat.R5G6B5);

                case "argb_4444":
                    return(Media.PixelFormat.A4R4G4B4);

                case "argb_8888":
                    return(Media.PixelFormat.A8R8G8B8);

                default:
                    LogManager.Instance.Write("[AndroidImageCodec] Failed to find conversion for Bitmap.Config.{0}.", config.Name());
                    return(Media.PixelFormat.Unknown);
                }
            }
            return(Media.PixelFormat.Unknown);
        }
コード例 #3
0
        private Bitmap GetBitmapMarker(Context context, int icon, string text)
        {
            var   resources = context.Resources;
            float scale     = resources.DisplayMetrics.Density;
            var   bitmap    = BitmapFactory.DecodeResource(resources, icon);

            Bitmap.Config bitmapConfig = bitmap.GetConfig();

            if (bitmapConfig == null)
            {
                bitmapConfig = Bitmap.Config.Argb8888;
            }
            bitmap = bitmap.Copy(bitmapConfig, true);

            var canvas = new Canvas(bitmap);
            var paint  = new Paint(PaintFlags.AntiAlias | PaintFlags.FakeBoldText)
            {
                Color    = Android.Graphics.Color.White,
                TextSize = (int)(12 * scale),
            };

            paint.SetShadowLayer(1f, 0f, 1f, Android.Graphics.Color.DarkGray);
            var bounds = new Rect();

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

            //center in full pin icon top circle
            float x = (float)((bitmap.Width - bounds.Width()) / 2.2);
            float y = (float)((bitmap.Height + bounds.Height()) / 2.5);

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

            return(bitmap);
        }
コード例 #4
0
        public Bitmap Transform(Bitmap source)
        {
            int size = Math.Min(source.Width, source.Height);

            int x = (source.Width - size) / 2;
            int y = (source.Height - size) / 2;

            Bitmap squaredBitmap = Bitmap.CreateBitmap(source, x, y, size, size);

            if (squaredBitmap != source)
            {
                source.Recycle();
            }

            Bitmap.Config config = source.GetConfig() != null?source.GetConfig() : Bitmap.Config.Rgb565;

            Bitmap bitmap = Bitmap.CreateBitmap(size, size, config);

            Canvas       canvas = new Canvas(bitmap);
            Paint        paint  = new Paint();
            BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.Clamp, BitmapShader.TileMode.Clamp);

            paint.SetShader(shader);
            paint.AntiAlias = true;

            float r = size / 2f;

            canvas.DrawCircle(r, r, r, paint);

            squaredBitmap.Recycle();
            return(bitmap);
        }
コード例 #5
0
        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.");
            }
        }
コード例 #6
0
        public static Bitmap DrawTextToBitmap(Bitmap bitmap, String Text, int TextSize)
        {
            Bitmap.Config bitmapConfig =
                bitmap.GetConfig();
            // set default bitmap config if none
            if (bitmapConfig == null)
            {
                bitmapConfig = Bitmap.Config.Argb8888;
            }

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

            // text color - #3D3D3D
            paint.Color = Color.White;
            // text size in pixels
            paint.TextSize = TextSize;
            // text shadow
            paint.SetShadowLayer(1f, 0f, 1f, Color.Black);

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

            paint.GetTextBounds(Text, 0, Text.Length, bounds);
            int x = (bitmap.Width - bounds.Width()) / 2;
            int y = (bitmap.Height + bounds.Height()) / 2;

            canvas.DrawText(Text, x, y, paint);

            return(bitmap);
        }
コード例 #7
0
        private Bitmap RecolorBitmapMonochrome(Bitmap source, Color?color)
        {
            int width = source.Width, height = source.Height;

            Bitmap.Config pixelFormat = Bitmap.Config.Argb8888;

            Bitmap target             = Bitmap.CreateBitmap(width, height, pixelFormat);

            int[] sourcePixels = new int[width * height];
            int[] targetPixels = new int[width * height];

            int targetR = color.Value.R;
            int targetG = color.Value.G;
            int targetB = color.Value.B;

            source.GetPixels(sourcePixels, 0, width, 0, 0, width, height);
            for (int i = 0; i < sourcePixels.Length; ++i)
            {
                int sourceColor = sourcePixels[i];
                int targetColor = sourceColor;
                if (sourceColor != 0)
                {
                    int sourceAlpha = Android.Graphics.Color.GetAlphaComponent(sourceColor);
                    targetColor = new Android.Graphics.Color(targetR, targetG, targetB, sourceAlpha);
                }
                targetPixels[i] = targetColor;
            }
            target.SetPixels(targetPixels, 0, width, 0, 0, width, height);

            return(target);
        }
コード例 #8
0
        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.");
            }
        }
コード例 #9
0
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            var view = convertView ?? _activity.LayoutInflater.Inflate(
                Resource.Layout.ContactListItem, parent, false);

            var txtContactName  = view.FindViewById <TextView> (Resource.Id.txtContactName);
            var txtPhoneNumber  = view.FindViewById <TextView> (Resource.Id.txtPhoneNumber);
            var imgContactThumb = view.FindViewById <ImageView> (Resource.Id.imgContactThumb);

            txtPhoneNumber.Text = _contactList [position].Number;

            // For each number but the first one
            if (position > 0)
            {
                // We are on the first contact's number
                if (_contactList [position - 1].Id != _contactList [position].Id)
                {
                    // Set contact name
                    txtContactName.Text = _contactList [position].DisplayName;

                    // Handle contact's image
                    if (_contactList [position].PhotoThumbnailId == null)
                    {
                        imgContactThumb = view.FindViewById <ImageView> (Resource.Id.imgContactThumb);

                        // TODO: corriger ca
                        Bitmap contactImageBmp = BitmapFactory.DecodeResource(parent.Context.Resources, 1);

                        if (contactImageBmp == null)
                        {
                            Bitmap.Config conf = Bitmap.Config.Argb8888;                             // see other conf types
                            Bitmap        bmp  = Bitmap.CreateBitmap(200, 200, conf);
                            bmp.EraseColor(Android.Graphics.Color.ParseColor("#0099CC"));

                            Drawable circleContactImage = new CircleDrawable(bmp);
                            imgContactThumb.SetImageDrawable(circleContactImage);
                        }
                    }
                    else
                    {
                        imgContactThumb.SetImageURI(_contactList[position].GetThumbnailUri());
                    }
                }
                // We are on additional contact's number
                else
                {
                    // Remove image
                    imgContactThumb.SetImageDrawable(null);

                    // Empty contact name
                    txtContactName.Text = null;
                }
            }


            view.SetTag(Resource.String.NormalizedPhone, _contactList [position].DisplayName);

            return(view);
        }
コード例 #10
0
ファイル: DrawFaces.cs プロジェクト: jobam/AgeMe
        public Stream DrawFaces(MediaFile image, ICollection <FaceDatas> faceDatas)
        {
            Bitmap bitmap = BitmapFactory.DecodeStream(image.GetStream());

            Bitmap.Config bitmapConfig =
                bitmap.GetConfig();

            // set default bitmap config if none
            if (bitmapConfig == null)
            {
                bitmapConfig = 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(PaintFlags.AntiAlias);

            paint.SetStyle(Paint.Style.Stroke);
            // rectangle color
            paint.Color = Color.Yellow;

            // pain for text
            Paint paintText = new Paint(PaintFlags.AntiAlias);

            // text color
            paintText.Color = Color.Yellow;

            // add rects into image
            foreach (var face in faceDatas)
            {
                //  create Rect left top right bottom
                Rect faceRect = new Rect(face.Rectangle.Left, face.Rectangle.Top,
                                         face.Rectangle.Left + face.Rectangle.Width,
                                         face.Rectangle.Top + face.Rectangle.Height);

                // Datas strings
                int x = (faceRect.Left + faceRect.Width() / 2);
                int y = (faceRect.Top + faceRect.Height()) + (int)(paintText.TextSize);

                //Set textSize, depends on rect size
                paintText.TextSize = face.Rectangle.Width / 6;

                // Draw on Image
                canvas.DrawRect(faceRect, paint);
                canvas.DrawText(face.Age.ToString(), x, y, paintText);
                canvas.DrawText(face.Gender, x, y + paintText.TextSize, paintText);
            }
            MemoryStream stream = new MemoryStream();

            bitmap.Compress(Bitmap.CompressFormat.Jpeg, 30, stream);
            stream.Seek(0, SeekOrigin.Begin);

            return(stream);
        }
コード例 #11
0
ファイル: Platform.cs プロジェクト: dhtdht020/GTTMCube
 public static bool Is32Bpp(Bitmap bmp)
 {
                 #if !ANDROID
     PixelFormat format = bmp.PixelFormat;
     return(format == PixelFormat.Format32bppRgb || format == PixelFormat.Format32bppArgb);
                 #else
     Bitmap.Config config = bmp.GetConfig();
     return(config != null && config == Bitmap.Config.Argb8888);
                 #endif
 }
コード例 #12
0
 private static Bitmap CreateBitmapSafelyWithGc(int width, int height, Bitmap.Config config, int retryCount)
 {
     if (retryCount > 0)
     {
         Java.Lang.JavaSystem.Gc();
         GC.Collect();
         return(CreateBitmapSafely(width, height, config, retryCount - 1));
     }
     return(null);
 }
コード例 #13
0
        /// <summary>
        /// Obtain a Bitmap from <see cref="RenderingBuffer"/>
        /// </summary>
        public static Bitmap GetBitmap(PixelsBuffer source, Bitmap.Config config = null)
        {
            var bmp = Bitmap.CreateBitmap(Array.ConvertAll <uint, int>(source.Data, new Converter <uint, int>(x => (int)x)), source.StartOffset, source.Stride, source.Width, source.Height, Bitmap.Config.Argb8888);

            if (config != null && !config.Equals(Bitmap.Config.Argb8888))
            {
                var tmp = ConvertConfig(bmp, config);
                bmp.Dispose();
                bmp = tmp;
            }
            return(bmp);
        }
コード例 #14
0
        public static Bitmap Base64ToBitmap(string base64String)
        {
            if (string.IsNullOrEmpty(base64String))
            {
                Bitmap.Config conf = Bitmap.Config.Argb8888;              // see other conf types
                Bitmap        bmp  = Bitmap.CreateBitmap(100, 100, conf); // this creates a MUTABLE bitmap
                return(bmp);
            }

            byte[] imageAsBytes = Base64.Decode(base64String, Base64Flags.Default);
            return(BitmapFactory.DecodeByteArray(imageAsBytes, 0, imageAsBytes.Length));
        }
コード例 #15
0
ファイル: AndroidGraphics.cs プロジェクト: Paludan/BeerRun
        public IImage NewImage(string fileName, Framework.ImageFormat format)
        {
            //Handles options and configuration for the desired image
            Bitmap.Config config = null;
            switch (format)
            {
            case Framework.ImageFormat.ARGB4444:
                config = Bitmap.Config.Argb4444;
                format = Framework.ImageFormat.ARGB4444;
                break;

            case Framework.ImageFormat.ARGB8888:
                config = Bitmap.Config.Argb8888;
                format = Framework.ImageFormat.ARGB8888;
                break;

            case Framework.ImageFormat.RGB565:
                config = Bitmap.Config.Rgb565;
                format = Framework.ImageFormat.RGB565;
                break;
            }

            BitmapFactory.Options o = new BitmapFactory.Options();
            o.InPreferredConfig = config;

            System.IO.Stream inStream = null;
            Bitmap           bitmap   = null;

            //Load the image from the assets folder
            try {
                inStream = assets.Open(fileName);
                bitmap   = BitmapFactory.DecodeStream(inStream, null, o);
                if (bitmap == null)
                {
                    throw new NullReferenceException("Couldn't load bitmap from assets '" + fileName + "'");
                }
            } catch {
                throw new NullReferenceException("Couldn't load bitmap from assets '" + fileName + "'");
            } finally {
                if (inStream != null)
                {
                    try {
                        inStream.Close();
                    }
                    catch {
                    }
                }
            }

            return(new AndroidImage(bitmap, format));
        }
コード例 #16
0
        public Bitmap drawTextToBitmap(Photo photo)
        {
            Android.Content.Res.Resources resources = context.Resources;
            BitmapFactory.Options         options   = new BitmapFactory.Options();
            options.InScaled = false;
            Bitmap bitmap = BitmapFactory.DecodeResource(resources, photo.mPhotoID, options);

            try
            {
                float         scale        = resources.DisplayMetrics.Density;
                Bitmap.Config bitmapConfig = bitmap.GetConfig();
                // set default bitmap config if none
                if (bitmapConfig == null)
                {
                    bitmapConfig = Bitmap.Config.Argb8888;
                }
                // resource bitmaps are imutable,
                // so we need to convert it to mutable one
                bitmap = bitmap.Copy(bitmapConfig, true);
                Console.WriteLine("Height:" + bitmap.Height + " Width:" + bitmap.Width);
                Canvas canvas = new Canvas(bitmap);
                Console.WriteLine("Canvas Height:" + canvas.Height + " Width:" + canvas.Width);
                TextPaint mTextPaint = new TextPaint(PaintFlags.AntiAlias);
                mTextPaint.Color = Color.ParseColor(photo.Hex);
                mTextPaint.SetTypeface(Typeface.Create(Typeface.DefaultBold, TypefaceStyle.Bold));
                int size = 30;
                mTextPaint.TextSize = ((int)(size * scale));
                StaticLayout mTextLayout = new StaticLayout(TrophyName, mTextPaint, (int)photo.W, Layout.Alignment.AlignCenter, 1.0f, 0.0f, false);
                while (true)
                {
                    if (mTextLayout.Height > photo.H)
                    {
                        mTextPaint.TextSize = (((size--) * scale));
                        mTextLayout         = new StaticLayout(TrophyName, mTextPaint, (int)photo.W, Layout.Alignment.AlignCenter, 1.0f, 0.0f, false);
                    }
                    else
                    {
                        break;
                    }
                }
                canvas.Save();
                canvas.Translate(photo.X, photo.Y + (photo.H - mTextLayout.Height) / 2);
                mTextLayout.Draw(canvas);
                canvas.Restore();
            }
            catch (Exception e)
            {
                Console.WriteLine("Bitmap Text Merge Exception:" + e.ToString());
            }
            return(bitmap);
        }
コード例 #17
0
        /// <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, config);

            ToBitmap(result);
            return(result);
        }
コード例 #18
0
        public static Bitmap ConvertConfig(Bitmap bitmap, Bitmap.Config config)
        {
            if (bitmap.GetConfig().Equals(config))
            {
                return(Bitmap.CreateBitmap(bitmap));
            }
            Bitmap convertedBitmap = Bitmap.CreateBitmap(bitmap.Width, bitmap.Height, config);
            Canvas canvas          = new Canvas(convertedBitmap);

            Android.Graphics.Paint paint = new Android.Graphics.Paint();
            paint.Color = Android.Graphics.Color.Black;
            canvas.DrawBitmap(bitmap, 0, 0, paint);
            return(convertedBitmap);
        }
コード例 #19
0
            Bitmap CreateBitmap(bool pressed, int width, int height)
            {
                Bitmap bitmap;

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

                using (var canvas = new ACanvas(bitmap))
                {
                    DrawCanvas(canvas, width, height, pressed);
                }

                return(bitmap);
            }
コード例 #20
0
        public static Bitmap Overlay(IEnumerable <Bitmap> bmps, Bitmap.Config config = null)
        {
            int    width     = bmps.Max <Bitmap>(x => x.Width);
            int    height    = bmps.Max <Bitmap>(x => x.Height);
            Bitmap bmOverlay = Bitmap.CreateBitmap(width, height, config == null ? Bitmap.Config.Argb8888 : config);
            Canvas canvas    = new Canvas(bmOverlay);

            foreach (var bmp in bmps)
            {
                canvas.DrawBitmap(bmp, 0, 0, null);
            }
            canvas.Dispose();
            return(bmOverlay);
        }
コード例 #21
0
 /// <summary>
 /// Creates the bitmap safely.
 /// </summary>
 /// <param name="width">The width.</param>
 /// <param name="height">The height.</param>
 /// <param name="config">The configuration.</param>
 /// <param name="retryCount">The retry count.</param>
 /// <returns>The bitmap.</returns>
 public static Bitmap CreateBitmapSafely(int width, int height, Bitmap.Config config, int retryCount)
 {
     try
     {
         return(Bitmap.CreateBitmap(width, height, config));
     }
     catch (OutOfMemoryException)
     {
         return(CreateBitmapSafelyWithGc(width, height, config, retryCount));
     }
     catch (Java.Lang.OutOfMemoryError)
     {
         return(CreateBitmapSafelyWithGc(width, height, config, retryCount));
     }
 }
コード例 #22
0
ファイル: BitmapImageCodec.cs プロジェクト: fossabot/jazz2
        public unsafe PixelData Read(Stream stream)
        {
            ColorRgba[] rawColorData;
            int         width, height;

            using (Bitmap bitmap = BitmapFactory.DecodeStream(stream, null, new BitmapFactory.Options {
                InPremultiplied = false,
                InScaled = false
            })) {
                Bitmap.Config config = bitmap.GetConfig();
                if (config != Bitmap.Config.Argb8888)
                {
                    throw new NotSupportedException();
                }

                width  = bitmap.Width;
                height = bitmap.Height;

                IntPtr ptr    = bitmap.LockPixels();
                int    stride = bitmap.RowBytes / sizeof(int);

                rawColorData = new ColorRgba[width * height];

                Parallel.ForEach(Partitioner.Create(0, height), range => {
                    for (int y = range.Item1; y < range.Item2; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            int argbValue = ((int *)ptr)[x + y * stride];

                            int i             = x + y * width;
                            rawColorData[i].A = (byte)((argbValue & 0xFF000000) >> 24);
                            rawColorData[i].B = (byte)((argbValue & 0x00FF0000) >> 16);
                            rawColorData[i].G = (byte)((argbValue & 0x0000FF00) >> 8);
                            rawColorData[i].R = (byte)((argbValue & 0x000000FF) >> 0);
                        }
                    }
                });

                bitmap.UnlockPixels();
            }

            PixelData pixelData = new PixelData();

            pixelData.SetData(rawColorData, width, height);
            pixelData.ColorTransparentPixels();
            return(pixelData);
        }
コード例 #23
0
ファイル: RouteViewHolder.cs プロジェクト: del77/BSc-Thesis
        private void AddCheckpointsToMap()
        {
            Bitmap.Config conf    = Bitmap.Config.Argb8888;
            var           bmp     = BitmapFactory.DecodeResource(_context.Resources, Resource.Drawable.checkpoint);
            Canvas        canvas1 = new Canvas();

            canvas1.DrawBitmap(bmp, 0, 0, null);


            foreach (var routeCheckpoint in Route.Checkpoints)
            {
                var marker = GoogleMap.AddMarker(new MarkerOptions()
                                                 .SetPosition(new LatLng(routeCheckpoint.Latitude, routeCheckpoint.Longitude))
                                                 .SetIcon(BitmapDescriptorFactory.FromBitmap(bmp))
                                                 .Anchor(0.5f, 0.5f));
            }
        }
コード例 #24
0
        /// <summary>
        /// Convert the Mat to Bitmap
        /// </summary>
        /// <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 void ToBitmap(Bitmap bitmap)
        {
            System.Drawing.Size size = Size;
            if (!(size.Width == bitmap.Width && size.Height == bitmap.Height))
            {
                throw new Exception("Bitmap size doesn't match the Mat size");
            }

            Bitmap.Config config = bitmap.GetConfig();
            if (config == Bitmap.Config.Argb8888)
            {
                int channels = NumberOfChannels;
                using (BitmapArgb8888Image bi = new BitmapArgb8888Image(bitmap))
                {
                    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);
                        }
                    }
                }
            }
            else if (config == Bitmap.Config.Rgb565)
            {
                using (BitmapRgb565Image bi = new BitmapRgb565Image(bitmap))
                    using (Image <Bgr, Byte> tmp = ToImage <Bgr, Byte>())
                        bi.ConvertFrom(tmp);
            }
            else
            {
                throw new NotImplementedException("Only Bitmap config of Argb888 or Rgb565 is supported.");
            }
        }
コード例 #25
0
        /// <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.");
            }
        }
コード例 #26
0
 public static Bitmap CreateBitmapSafely(int width, int height, Bitmap.Config config, int retryCount)
 {
     try
     {
         return(Bitmap.CreateBitmap(width, height, config));
     }
     catch (OutOfMemoryError e)
     {
         e.PrintStackTrace();
         if (retryCount > 0)
         {
             GC.Collect();
             return(CreateBitmapSafely(width, height, config, retryCount - 1));
         }
         return(null);
     }
 }
コード例 #27
0
        /// <summary>
        /// 获取一个bitmap,目的是用来承载drawable;
        /// 将这个bitmap放在canvas上面承载,并在其上面画一个椭圆(其实也是一个圆,因为width=height)来固定显示区域
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <returns></returns>
        public Bitmap CreateOvalBitmap(int width, int height)
        {
            Bitmap.Config localConfig = Bitmap.Config.Argb8888;
            Bitmap        localBitmap = Bitmap.CreateBitmap(width, height, localConfig);
            Canvas        localCanvas = new Canvas(localBitmap);
            Paint         localPaint  = new Paint();
            int           padding     = mBorderWidth - 3;

            /**
             * 设置椭圆的大小(因为椭圆的最外边会和border的最外边重合的,如果图片最外边的颜色很深,有看出有棱边的效果,所以为了让体验更加好,
             * 让其缩进padding px)
             */
            RectF localRectF = new RectF(padding, padding, width - padding, height - padding);

            localCanvas.DrawOval(localRectF, localPaint);
            return(localBitmap);
        }
コード例 #28
0
ファイル: Utils.cs プロジェクト: maximrub/xamarin-components
 public static Bitmap CreateBitmapSafely(int width, int height, Bitmap.Config config, int retryCount)
 {
     try
     {
         return(Bitmap.CreateBitmap(width, height, config));
     }
     catch (OutOfMemoryException e)
     {
         Console.WriteLine(e.ToString());
         Console.Write(e.StackTrace);
         if (retryCount > 0)
         {
             Java.Lang.JavaSystem.Gc();
             GC.Collect();
             return(CreateBitmapSafely(width, height, config, retryCount - 1));
         }
         return(null);
     }
 }
コード例 #29
0
 /// <summary>
 /// Return the byte usage per pixel of a bitmap based on its configuration.
 /// </summary>
 /// <param name="config">The bitmap configuration</param>
 /// <returns>The byte usage per pixel</returns>
 private int GetBytesPerPixel(Bitmap.Config config)
 {
     if (config == Bitmap.Config.Argb8888)
     {
         return(4);
     }
     else if (config == Bitmap.Config.Rgb565)
     {
         return(2);
     }
     else if (config == Bitmap.Config.Argb4444)
     {
         return(2);
     }
     else if (config == Bitmap.Config.Alpha8)
     {
         return(1);
     }
     return(1);
 }
コード例 #30
0
        private bool CanUseForInBitmap(Bitmap item, int width, int height, Bitmap.Config bitmapConfig, int inSampleSize)
        {
            if (!Utils.HasKitKat())
            {
                // On earlier versions, the dimensions must match exactly and the inSampleSize must be 1
                return(item.Width == width && item.Height == height && GetBytesPerPixel(item.GetConfig()) == GetBytesPerPixel(bitmapConfig) && inSampleSize == 1);
            }

            // From Android 4.4 (KitKat) onward we can re-use if the byte size of the new bitmap
            // is smaller than the reusable bitmap candidate allocation byte count.
            if (inSampleSize == 0)
            {
                // avoid division by zero
                inSampleSize = 1;
            }

            int newWidth  = (int)Math.Ceiling(width / (float)inSampleSize);
            int newHeight = (int)Math.Ceiling(height / (float)inSampleSize);

            if (inSampleSize > 1)
            {
                // Android docs: the decoder uses a final value based on powers of 2, any other value will be rounded down to the nearest power of 2.
                if (newWidth % 2 != 0)
                {
                    newWidth += 1;
                }

                if (newHeight % 2 != 0)
                {
                    newHeight += 1;
                }
            }

            int byteCount = newWidth * newHeight * GetBytesPerPixel(bitmapConfig);

            return(byteCount <= item.AllocationByteCount);
        }