Пример #1
0
        private static byte[] CallFunction(byte[] photoBytes, string textDiplay,
                                           string bubbleFigure, string bubblePos, string filter)
        {
            string assetsPath = ConfigurationManager.AppSettings["ASSETS_ROOT"] ?? @"C:\Temp\images\";

            bubbleFigure = bubbleFigure.ToUpper();
            bubblePos    = bubblePos.ToUpper();
            filter       = filter.ToUpper();

            var imgGen = new MemeImgGen.ImageProcessor(assetsPath);

            BubbleFigure   figure       = (BubbleFigure)Enum.Parse(typeof(BubbleFigure), bubbleFigure);
            BubblePosition position     = (BubblePosition)Enum.Parse(typeof(BubblePosition), bubblePos);
            BubbleFilter   bFilter      = (BubbleFilter)Enum.Parse(typeof(BubbleFilter), filter);
            IMatrixFilter  bubbleFilter = imgGen.GetFilterByName(bFilter);

            // 이미지 변환 메서드 호출
            byte[] outBytes = imgGen.ImageGenerate(photoBytes, textDiplay, figure, position, bubbleFilter);
            return(outBytes);
        }
Пример #2
0
        public static async void Run(
            [QueueTrigger("meme-que")] VisionInfo info,
            string BlobName,
            [Blob("meme-input/{BlobName}", FileAccess.Read)] byte[] inputBlob,
            [Blob("meme-output/{BlobName}", FileAccess.Write)] Stream outputBlob,
            TraceWriter log)
        {
            string assetsPath = ConfigurationManager.AppSettings["ASSETS_ROOT"] ?? @"C:\Temp\images\";

            string bubbleFigure = info.BubbleInfo.Figure.ToUpper();
            string bubblePos    = info.BubbleInfo.Position.ToUpper();
            string filter       = info.BubbleInfo.Filter.ToUpper();
            string textDisplay  = info.BubbleInfo.Text;
            var    imgGen       = new MemeImgGen.ImageProcessor(assetsPath);

            BubbleFigure   figure       = (BubbleFigure)Enum.Parse(typeof(BubbleFigure), bubbleFigure);
            BubblePosition position     = (BubblePosition)Enum.Parse(typeof(BubblePosition), bubblePos);
            BubbleFilter   bFilter      = (BubbleFilter)Enum.Parse(typeof(BubbleFilter), filter);
            IMatrixFilter  bubbleFilter = imgGen.GetFilterByName(bFilter);

            // 이미지 변환 메서드 호출
            byte[] outBytes = imgGen.ImageGenerate(inputBlob, textDisplay, figure, position, bubbleFilter);
            await outputBlob.WriteAsync(outBytes, 0, outBytes.Length);
        }
Пример #3
0
        private BubbleInfo GetBubbleInfo(BubblePosition position, BubbleFigure bubbleFigure)
        {
            BubbleInfo info = new BubbleInfo();

            // ie : bubble-TOPLEFT-STAR.png
            info.ImageName  = $"BUBBLE-{position.ToString()}-{bubbleFigure.ToString()}.png";
            info.StartPoint = new Point(0, 0); // Right position is calculated after automatically

            //Point p = new Point();
            switch (position)
            {
            case BubblePosition.TOPLEFT:
                switch (bubbleFigure)
                {
                case BubbleFigure.STAR:
                    info.TextArea = new Rectangle(10, 0, 200, 120);
                    break;

                case BubbleFigure.CLOUD:

                case BubbleFigure.NORMAL:
                default:
                    info.TextArea = new Rectangle(10, 10, 200, 110);
                    break;
                }
                return(info);

            case BubblePosition.TOPRIGHT:
                switch (bubbleFigure)
                {
                case BubbleFigure.STAR:
                    info.TextArea = new Rectangle(70, 0, 200, 140);
                    break;

                case BubbleFigure.CLOUD:

                case BubbleFigure.NORMAL:
                default:
                    info.TextArea = new Rectangle(10, 10, 200, 120);
                    break;
                }
                return(info);

            //info.STARtpoint = new Point(200, 0);
            //info.ImageName = "bubble-tr.png";
            //info.TextArea = new Rectangle(10, 10, 200, 120);
            //break;
            case BubblePosition.BOTTOMLEFT:
                switch (bubbleFigure)
                {
                case BubbleFigure.STAR:
                    info.TextArea = new Rectangle(5, 60, 200, 130);
                    break;

                case BubbleFigure.CLOUD:

                case BubbleFigure.NORMAL:
                default:
                    info.TextArea = new Rectangle(10, 60, 200, 120);
                    break;
                }
                return(info);

            //info.STARtpoint = new Point(0, 400);
            //info.ImageName = "bubble-bl.png";
            //info.TextArea = new Rectangle(10, 60, 200, 120);
            //break;
            case BubblePosition.BOTTOMRIGHT:
                switch (bubbleFigure)
                {
                case BubbleFigure.STAR:
                    info.TextArea = new Rectangle(70, 50, 200, 160);
                    break;

                case BubbleFigure.CLOUD:

                case BubbleFigure.NORMAL:
                default:
                    info.TextArea = new Rectangle(0, 50, 200, 140);
                    break;
                }
                return(info);

            //info.STARtpoint = new Point(200, 400);
            //info.ImageName = "bubble-STAR2br.png";
            //info.TextArea = new Rectangle(70, 60, 200, 140);
            //return info;
            default:
                return(null);
            }
        }
Пример #4
0
        /// <summary>
        /// Main method of this class for processing image
        /// provide filtering, image overlaying etc
        /// </summary>
        /// <param name="photoBytes"></param>
        /// <returns></returns>
        public byte[] ImageGenerate(byte[] photoBytes, string textDiplay,
                                    BubbleFigure bubbleFigure, BubblePosition bubblePosition, IMatrixFilter appliedFilter)
        {
            // Get width and height of original image
            // I know it's not good solution but now... remain
            int originImgWidth, originImgHeight;

            using (Image image = Image.FromStream(new MemoryStream(photoBytes)))
            {
                originImgWidth  = image.Width;
                originImgHeight = image.Height;
            }

            // 가정
            // 1. 이미지의 widht 는 400으로 한다. (resize 필요)
            // 2. 버블은 좌,우 및 상,하로 구분한다

            var bubbleInfo = GetBubbleInfo(bubblePosition, bubbleFigure);

            // Format is automatically detected though can be changed.
            ISupportedImageFormat format = new JpegFormat {
                Quality = 70
            };

            Bitmap bitmap = (Bitmap)Image.FromFile(Path.Combine(assetsPath, bubbleInfo.ImageName));

            #region 강제 텍스트 출력예
            //string firstText = "내 이름은";
            //string secondText = "빌 게이츠";

            //PointF firstLocation = new PointF(10f, 10f);
            //PointF secondLocation = new PointF(10f, 50f);

            //using (Graphics graphics = Graphics.FromImage(bitmap))
            //{
            //    using (Font arialFont = new Font("Malgun Gothic", 20, FontStyle.Bold))
            //    {
            //        graphics.DrawString(firstText, arialFont, Brushes.Black, firstLocation);
            //        graphics.DrawString(secondText, arialFont, Brushes.Black, secondLocation);
            //    }
            //}
            #endregion

            //Bitmap newBitmap = new Bitmap(bitmap.Width, bitmap.Height);
            // create bitmap for overlaying
            using (Graphics graphics = Graphics.FromImage(bitmap))
            {
                using (Font font1 = new Font(this.fontName, 120, FontStyle.Bold, GraphicsUnit.Pixel))
                {
                    Rectangle rect1 = bubbleInfo.TextArea;

                    StringFormat stringFormat = new StringFormat();
                    stringFormat.Alignment     = StringAlignment.Center;
                    stringFormat.LineAlignment = StringAlignment.Center;
                    graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;

                    Font goodFont = FindFont(graphics, textDiplay, rect1.Size, font1);
                    graphics.DrawString(textDiplay, goodFont, Brushes.Black, rect1, stringFormat);
                }
            }
            //Image img = (Image)bitmap;
            Image overlayImage = (Image)bitmap;

            //get width and height of layer for overlay
            int overlayWidth, overlayHeight;
            overlayWidth  = overlayImage.Width;
            overlayHeight = overlayImage.Height;

            // revise TextArea width and Height
            Rectangle rec = bubbleInfo.TextArea;
            if (bubbleFigure == BubbleFigure.NORMAL)
            {
                bubbleInfo.TextArea = new Rectangle(rec.X, rec.Y, rec.Width - figure_normal_revise_w, rec.Height - figure_normal_revise_h);
            }

            using (MemoryStream inStream = new MemoryStream(photoBytes))
            {
                // if bubblePosition is right side, start X is imgWidth - overlayWidth
                if (bubblePosition == BubblePosition.TOPRIGHT || bubblePosition == BubblePosition.BOTTOMRIGHT)
                {
                    bubbleInfo.StartPoint = new Point(originImgWidth - overlayWidth, bubbleInfo.StartPoint.Y);
                }

                // if bubblePosition is bottom side, start y is imgHeight - overlayHeight
                if (bubblePosition == BubblePosition.BOTTOMLEFT || bubblePosition == BubblePosition.BOTTOMRIGHT)
                {
                    bubbleInfo.StartPoint = new Point(bubbleInfo.StartPoint.X, originImgHeight - overlayHeight);
                }

                using (MemoryStream outStream = new MemoryStream())
                {
                    // Initialize the ImageFactory using the overload to preserve EXIF metadata.
                    using (ImageFactory imageFactory = new ImageFactory(preserveExifData: true))
                    {
                        //Image img = Image.FromFile(@"C:\Temp\images\bubble-tp.png");
                        ImageLayer layer = new ImageLayer();
                        layer.Image    = overlayImage;
                        layer.Opacity  = this.opacity;
                        layer.Position = bubbleInfo.StartPoint;

                        //필터 적용?
                        // Comic, Gotham, GreyScale, Lomograph, Polaroid, HiSatch, Sepia
                        var filter = appliedFilter;

                        // Load, resize, set the format and quality and save an image.
                        var factory = imageFactory.Load(inStream)
                                      //.Resize(size)
                                      .Format(format);

                        if (appliedFilter != null)
                        {
                            factory.Filter(filter);
                        }

                        factory
                        .Overlay(layer)
                        .Save(outStream);
                    }

                    return(outStream.ToArray());
                }
            }
        }