예제 #1
0
        /// <summary>
        /// Converting raw pixel data to jpeg stream
        /// </summary>
        /// <param name="rawPixel">The raw pixel data</param>
        /// <param name="width">The width of the image</param>
        /// <param name="height">The height of the image</param>
        /// <param name="channels">The number of channels</param>
        /// <returns>The jpeg stream</returns>
        public static JpegData PixelToJpeg(byte[] rawPixel, int width, int height, int channels)
        {
            if (channels != 4)
            {
                throw new NotImplementedException("Only 4 channel pixel input is supported.");
            }
            System.Drawing.Size sz = new System.Drawing.Size(width, height);

            using (CGColorSpace cspace = CGColorSpace.CreateDeviceRGB())
                using (CGBitmapContext context = new CGBitmapContext(
                           rawPixel,
                           sz.Width, sz.Height,
                           8,
                           sz.Width * 4,
                           cspace,
                           CGBitmapFlags.PremultipliedLast | CGBitmapFlags.ByteOrder32Big))
                    using (CGImage cgImage = context.ToImage())

                        using (NSBitmapImageRep newImg = new NSBitmapImageRep(cgImage))
                        {
                            JpegData result      = new JpegData();
                            var      rawJpegData = newImg.RepresentationUsingTypeProperties(NSBitmapImageFileType.Jpeg);

                            byte[] raw = new byte[rawJpegData.Length];
                            System.Runtime.InteropServices.Marshal.Copy(rawJpegData.Bytes, raw, 0,
                                                                        (int)rawJpegData.Length);
                            result.Raw    = raw;
                            result.Width  = sz.Width;
                            result.Height = sz.Height;
                            return(result);
                        }
        }
예제 #2
0
        /// <summary>
        /// Read the file and draw rectangles on it.
        /// </summary>
        /// <param name="fileName">The name of the file.</param>
        /// <param name="annotations">Annotations to be add to the image. Can consist of rectangles and labels</param>
        /// <returns>The image in Jpeg stream format</returns>
        public static JpegData ImageFileToJpeg(String fileName, Annotation[] annotations = null)
        {
            NSImage img;

            if (File.Exists(fileName))
            {
                img = new NSImage(fileName); //full path
            }
            else
            {
                img = NSImage.ImageNamed(fileName); //image included in the package
            }
            if (annotations != null && annotations.Length > 0)
            {
                DrawAnnotations(img, annotations);
            }

            var imageData = img.AsTiff();
            var imageRep  = NSBitmapImageRep.ImageRepsWithData(imageData)[0] as NSBitmapImageRep;
            var jpegData  = imageRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Jpeg, null);

            byte[] jpeg = new byte[jpegData.Length];
            System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, jpeg, 0, (int)jpegData.Length);

            JpegData result = new JpegData();

            result.Raw    = jpeg;
            result.Width  = (int)img.Size.Width;
            result.Height = (int)img.Size.Height;

            return(result);
        }
예제 #3
0
 /// <summary>
 /// Convert bitmap to jpeg
 /// </summary>
 /// <param name="bmp">The android bitmap</param>
 /// <returns>The jpeg representation</returns>
 public static Emgu.Models.JpegData ToJpeg(this Android.Graphics.Bitmap bmp)
 {
     using (MemoryStream ms = new MemoryStream())
     {
         bmp.Compress(Bitmap.CompressFormat.Jpeg, 90, ms);
         JpegData result = new JpegData();
         result.Raw    = ms.ToArray();
         result.Width  = bmp.Width;
         result.Height = bmp.Height;
         return(result);
     }
 }
예제 #4
0
        /// <summary>
        /// Read the file and draw rectangles on it.
        /// </summary>
        /// <param name="fileName">The name of the file.</param>
        /// <param name="annotations">Annotations to be add to the image. Can consist of rectangles and labels</param>
        /// <returns>The image in Jpeg stream format</returns>
        public static JpegData ImageFileToJpeg(String fileName, Annotation[] annotations = null)
        {
            UIImage uiimage = new UIImage(fileName);

            UIImage imgWithRect = DrawAnnotations(uiimage, annotations);
            var     jpegData    = imgWithRect.AsJPEG();

            byte[] jpeg = new byte[jpegData.Length];
            System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, jpeg, 0, (int)jpegData.Length);
            JpegData result = new JpegData();

            result.Raw    = jpeg;
            result.Width  = (int)uiimage.Size.Width;
            result.Height = (int)uiimage.Size.Height;
            return(result);
        }
예제 #5
0
        /// <summary>
        /// Read the file and draw rectangles on it.
        /// </summary>
        /// <param name="fileName">The name of the file.</param>
        /// <param name="annotations">Annotations to be add to the image. Can consist of rectangles and labels</param>
        /// <returns>The image in Jpeg stream format</returns>
        public static Emgu.Models.JpegData ImageFileToJpeg(String fileName, Annotation[] annotations = null)
        {
            using (BitmapFactory.Options options = new BitmapFactory.Options())
            {
                options.InMutable = true;
                using (Android.Graphics.Bitmap bmp = BitmapFactory.DecodeFile(fileName, options))
                {
                    if (annotations != null)
                    {
                        using (Android.Graphics.Paint p = new Android.Graphics.Paint())
                            using (Canvas c = new Canvas(bmp))
                            {
                                p.AntiAlias = true;
                                p.Color     = Android.Graphics.Color.Red;

                                p.TextSize = 20;
                                for (int i = 0; i < annotations.Length; i++)
                                {
                                    p.SetStyle(Paint.Style.Stroke);
                                    float[] rects           = ScaleLocation(annotations[i].Rectangle, bmp.Width, bmp.Height);
                                    Android.Graphics.Rect r = new Rect((int)rects[0], (int)rects[1], (int)rects[2],
                                                                       (int)rects[3]);
                                    c.DrawRect(r, p);

                                    p.SetStyle(Paint.Style.Fill);
                                    c.DrawText(annotations[i].Label, (int)rects[0], (int)rects[1], p);
                                }
                            }
                    }

                    using (MemoryStream ms = new MemoryStream())
                    {
                        bmp.Compress(Bitmap.CompressFormat.Jpeg, 90, ms);
                        JpegData result = new JpegData();
                        result.Raw    = ms.ToArray();
                        result.Width  = bmp.Width;
                        result.Height = bmp.Height;
                        return(result);
                    }
                }
            }
        }
예제 #6
0
        /// <summary>
        /// Read the file and draw rectangles on it.
        /// </summary>
        /// <param name="fileName">The name of the file.</param>
        /// <param name="annotations">Annotations to be add to the image. Can consist of rectangles and labels</param>
        /// <returns>The image in Jpeg stream format</returns>
        public static JpegData ImageFileToJpeg(String fileName, Annotation[] annotations = null)
        {
            Bitmap img = new Bitmap(fileName);

            if (annotations != null)
            {
                using (Graphics g = Graphics.FromImage(img))
                {
                    for (int i = 0; i < annotations.Length; i++)
                    {
                        if (annotations[i].Rectangle != null)
                        {
                            float[]    rects  = ScaleLocation(annotations[i].Rectangle, img.Width, img.Height);
                            PointF     origin = new PointF(rects[0], rects[1]);
                            RectangleF rect   = new RectangleF(origin,
                                                               new SizeF(rects[2] - rects[0], rects[3] - rects[1]));
                            Pen redPen = new Pen(Color.Red, 3);
                            g.DrawRectangle(redPen, Rectangle.Round(rect));

                            String label = annotations[i].Label;
                            if (label != null)
                            {
                                g.DrawString(label, new Font(FontFamily.GenericSansSerif, 20f), Brushes.Red, origin);
                            }
                        }
                    }

                    g.Save();
                }
            }

            using (MemoryStream ms = new MemoryStream())
            {
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                JpegData result = new JpegData();
                result.Raw    = ms.ToArray();
                result.Width  = img.Size.Width;
                result.Height = img.Size.Height;
                return(result);
            }
        }
예제 #7
0
        /// <summary>
        /// Read the file and draw rectangles on it.
        /// </summary>
        /// <param name="fileName">The name of the file.</param>
        /// <param name="annotations">Annotations to be add to the image. Can consist of rectangles and lables</param>
        /// <returns>The image in Jpeg stream format</returns>
        public static JpegData ImageFileToJpeg(String fileName, Annotation[] annotations = null)
        {
#if __ANDROID__
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.InMutable = true;
            Android.Graphics.Bitmap bmp = BitmapFactory.DecodeFile(fileName, options);

            Android.Graphics.Paint p = new Android.Graphics.Paint();
            p.SetStyle(Paint.Style.Stroke);
            p.AntiAlias = true;
            p.Color     = Android.Graphics.Color.Red;
            Canvas c = new Canvas(bmp);

            for (int i = 0; i < annotations.Length; i++)
            {
                float[] rects           = ScaleLocation(annotations[i].Rectangle, bmp.Width, bmp.Height);
                Android.Graphics.Rect r = new Rect((int)rects[0], (int)rects[1], (int)rects[2], (int)rects[3]);
                c.DrawRect(r, p);
            }

            using (MemoryStream ms = new MemoryStream())
            {
                bmp.Compress(Bitmap.CompressFormat.Jpeg, 90, ms);
                JpegData result = new JpegData();
                result.Raw    = ms.ToArray();
                result.Width  = bmp.Width;
                result.Height = bmp.Height;
                return(result);
            }
#elif __MACOS__
            NSImage img = NSImage.ImageNamed(fileName);

            if (annotations != null && annotations.Length > 0)
            {
                DrawAnnotations(img, annotations);
            }

            var    imageData = img.AsTiff();
            var    imageRep  = NSBitmapImageRep.ImageRepsWithData(imageData)[0] as NSBitmapImageRep;
            var    jpegData  = imageRep.RepresentationUsingTypeProperties(NSBitmapImageFileType.Jpeg, null);
            byte[] jpeg      = new byte[jpegData.Length];
            System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, jpeg, 0, (int)jpegData.Length);

            JpegData result = new JpegData();
            result.Raw    = jpeg;
            result.Width  = (int)img.Size.Width;
            result.Height = (int)img.Size.Height;

            return(result);
#elif __IOS__
            UIImage uiimage = new UIImage(fileName);

            UIGraphics.BeginImageContextWithOptions(uiimage.Size, false, 0);
            var context = UIGraphics.GetCurrentContext();

            uiimage.Draw(new CGPoint());
            context.SetStrokeColor(UIColor.Red.CGColor);
            context.SetLineWidth(2);

            for (int i = 0; i < annotations.Length; i++)
            {
                float[] rects = ScaleLocation(
                    annotations[i].Rectangle,
                    (int)uiimage.Size.Width,
                    (int)uiimage.Size.Height);
                CGRect cgRect = new CGRect(
                    (nfloat)rects[0],
                    (nfloat)rects[1],
                    (nfloat)(rects[2] - rects[0]),
                    (nfloat)(rects[3] - rects[1]));
                context.AddRect(cgRect);
                context.DrawPath(CGPathDrawingMode.Stroke);
            }
            context.ScaleCTM(1, -1);
            context.TranslateCTM(0, -uiimage.Size.Height);
            for (int i = 0; i < annotations.Length; i++)
            {
                float[] rects = ScaleLocation(
                    annotations[i].Rectangle,
                    (int)uiimage.Size.Width,
                    (int)uiimage.Size.Height);
                context.SelectFont("Helvetica", 18, CGTextEncoding.MacRoman);
                context.SetFillColor((nfloat)1.0, (nfloat)0.0, (nfloat)0.0, (nfloat)1.0);
                context.SetTextDrawingMode(CGTextDrawingMode.Fill);
                context.ShowTextAtPoint(rects[0], uiimage.Size.Height - rects[1], annotations[i].Label);
            }
            UIImage imgWithRect = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();

            var    jpegData = imgWithRect.AsJPEG();
            byte[] jpeg     = new byte[jpegData.Length];
            System.Runtime.InteropServices.Marshal.Copy(jpegData.Bytes, jpeg, 0, (int)jpegData.Length);
            JpegData result = new JpegData();
            result.Raw    = jpeg;
            result.Width  = (int)uiimage.Size.Width;
            result.Height = (int)uiimage.Size.Height;
            return(result);
#else
            Bitmap img = new Bitmap(fileName);

            if (annotations != null)
            {
                using (Graphics g = Graphics.FromImage(img))
                {
                    for (int i = 0; i < annotations.Length; i++)
                    {
                        if (annotations[i].Rectangle != null)
                        {
                            float[]    rects  = ScaleLocation(annotations[i].Rectangle, img.Width, img.Height);
                            PointF     origin = new PointF(rects[0], rects[1]);
                            RectangleF rect   = new RectangleF(origin, new SizeF(rects[2] - rects[0], rects[3] - rects[1]));
                            Pen        redPen = new Pen(Color.Red, 3);
                            g.DrawRectangle(redPen, Rectangle.Round(rect));

                            String label = annotations[i].Label;
                            if (label != null)
                            {
                                g.DrawString(label, new Font(FontFamily.GenericSansSerif, 20f), Brushes.Red, origin);
                            }
                        }
                    }
                    g.Save();
                }
            }

            using (MemoryStream ms = new MemoryStream())
            {
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                JpegData result = new JpegData();
                result.Raw    = ms.ToArray();
                result.Width  = img.Size.Width;
                result.Height = img.Size.Height;
                return(result);
            }
#endif
        }