Ejemplo n.º 1
0
        public byte[] GetImage(string referenceName, int zoomLevel, int chunk)
        {
            var refer  = _service._references.First(r => r.Name == referenceName);
            var height = (_service._histograms[refer.Name].Max() + 1) * 15;

            using var surface     = SKSurface.Create(new SKImageInfo(WIDTH, height));
            using SKCanvas canvas = surface.Canvas;
            canvas.Clear();

            using SKPaint paint = new SKPaint
                  {
                      Style       = SKPaintStyle.Stroke,
                      Color       = SKColors.Black,
                      StrokeWidth = 1
                  };

            using SKPaint fillPaint = new SKPaint
                  {
                      Style       = SKPaintStyle.Fill,
                      Color       = SKColors.Blue,
                      StrokeWidth = 1
                  };

            using SKPaint reverseFillPaint = new SKPaint
                  {
                      Style       = SKPaintStyle.Fill,
                      Color       = SKColors.Red,
                      StrokeWidth = 1
                  };

            var startY = 7;
            var list   = new List <long> {
                0
            };

            using var reader = new StreamReader($"{_service._references.IndexOf(refer)}.tmp");
            string line;

            while ((line = reader.ReadLine()) != null)
            {
                var splitLine = line.Split('\t');

                var read = new SamFile
                {
                    QueryName       = splitLine[0],
                    ReferenceName   = splitLine[1],
                    StartingPos     = int.Parse(splitLine[2]),
                    DrawExpressions = DataService.GetDrawExpressions(splitLine[3]),
                    Reverse         = bool.Parse(splitLine[4])
                };

                var realStartingX = zoomLevel * read.StartingPos;

                if (realStartingX >= (chunk + 1) * WIDTH)
                {
                    break;
                }

                var index      = list.FindIndex(i => realStartingX > i + 1);
                var y          = startY * (index != -1 ? index : list.Count);
                var lineLength = read.DrawExpressions.Sum(de => de.Length) * zoomLevel;

                if (index == -1)
                {
                    list.Add(realStartingX + lineLength);
                }
                else
                {
                    list[index] = realStartingX + lineLength;
                }

                realStartingX -= chunk * WIDTH;

                if (realStartingX < 0 && realStartingX + lineLength < 0)
                {
                    continue;
                }

                canvas.DrawLine(new SKPoint(realStartingX, y + 3), new SKPoint(realStartingX + lineLength, y + 3), paint);

                var currentX = realStartingX;
                foreach (var draw in read.DrawExpressions)
                {
                    if (draw.Type == DrawType.Rectangle)
                    {
                        canvas.DrawRect(currentX, y, draw.Length * zoomLevel, 5, read.Reverse ? reverseFillPaint : fillPaint);
                        canvas.DrawRect(currentX, y, draw.Length * zoomLevel, 5, paint);
                    }
                    currentX += draw.Length * zoomLevel;
                }

                canvas.Flush();
            }

            var      width      = (chunk + 1) * WIDTH * zoomLevel > refer.Length * zoomLevel ? refer.Length * zoomLevel % WIDTH : WIDTH;
            var      realHeight = 7 * (list.Count + 1);
            SKPixmap pixmap     = surface.Snapshot().Subset(SKRectI.Create(0, 0, width, realHeight)).PeekPixels();

            SKData data;

            if (realHeight > 15000)
            {
                data = pixmap.Encode(SKPngEncoderOptions.Default);
            }
            else
            {
                var options = new SKWebpEncoderOptions(SKWebpEncoderCompression.Lossless, 100);
                data = pixmap.Encode(options);
            }

            return(data.ToArray());
        }
Ejemplo n.º 2
0
        private SKBitmap CropWhiteSpace(SKBitmap bitmap)
        {
            var topmost = 0;

            for (int row = 0; row < bitmap.Height; ++row)
            {
                if (IsAllWhiteRow(bitmap, row))
                {
                    topmost = row + 1;
                }
                else
                {
                    break;
                }
            }

            int bottommost = bitmap.Height;

            for (int row = bitmap.Height - 1; row >= 0; --row)
            {
                if (IsAllWhiteRow(bitmap, row))
                {
                    bottommost = row;
                }
                else
                {
                    break;
                }
            }

            int leftmost = 0, rightmost = bitmap.Width;

            for (int col = 0; col < bitmap.Width; ++col)
            {
                if (IsAllWhiteColumn(bitmap, col))
                {
                    leftmost = col + 1;
                }
                else
                {
                    break;
                }
            }

            for (int col = bitmap.Width - 1; col >= 0; --col)
            {
                if (IsAllWhiteColumn(bitmap, col))
                {
                    rightmost = col;
                }
                else
                {
                    break;
                }
            }

            var newRect = SKRectI.Create(leftmost, topmost, rightmost - leftmost, bottommost - topmost);

            using (var image = SKImage.FromBitmap(bitmap))
            {
                using (var subset = image.Subset(newRect))
                {
                    return(SKBitmap.FromImage(subset));
                }
            }
        }
Ejemplo n.º 3
0
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            if (string.IsNullOrEmpty(imagePath))
            {
                return;
            }

            canvas.Clear(SKColors.White);

            byte[] imageBytes = null;
            try
            {
                imageBytes = File.ReadAllBytes(imagePath);
            }
            catch (Exception)
            {
                // a dirty hack because its late
                return;
            }

            // decode the bitmap
            using (var bitmap = SKBitmap.Decode(imageBytes))
            {
                if (identResults != null)
                {
                    // draw directly on the bitmap
                    using (var annotationCanvas = new SKCanvas(bitmap))
                        using (var textPaint = new SKPaint())
                            using (var boxPain = new SKPaint())
                            {
                                boxPain.StrokeWidth = 3;
                                boxPain.Style       = SKPaintStyle.Stroke;
                                textPaint.TextSize  = 25;

                                foreach (var result in identResults)
                                {
                                    SKColor drawColor  = SKColors.Red;
                                    var     readyCount = result.Orders.Where(o => o.Status == OrderStatus.Ready).Count();

                                    // All ready
                                    if (readyCount == result.Orders.Length)
                                    {
                                        drawColor = SKColors.GreenYellow;
                                    }

                                    // Part Ready
                                    else if (readyCount > 0 && readyCount != result.Orders.Length)
                                    {
                                        drawColor = SKColors.Orange;
                                    }

                                    // Set the paint color
                                    boxPain.Color   = drawColor;
                                    textPaint.Color = drawColor;

                                    // Draw the rectagle
                                    var rect = result.Rectangle;
                                    var face = SKRectI.Create(
                                        rect.X,
                                        rect.Y,
                                        rect.Width,
                                        rect.Height);

                                    annotationCanvas.DrawRect(face, boxPain);

                                    var message = result.Customer.FirstName + " #" + String.Join(",", result.Orders.Select(o => o.OrderNumber.ToString()));
                                    annotationCanvas.DrawText(message, rect.X + 10, rect.Y - textPaint.TextSize, textPaint);
                                }
                            }
                }

                // Resizing
                var pictureFrame = canvasView.Bounds.ToSKRect();
                var imageSize    = new SKSize(bitmap.Width, bitmap.Height);
                var dest         = pictureFrame.AspectFill(imageSize);

                // draw the image
                var paint = new SKPaint
                {
                    FilterQuality = SKFilterQuality.High // high quality scaling
                };

                // draw the modified bitmap to the screen
                canvas.DrawBitmap(bitmap, dest, paint);
            }
        }
        /// <summary>
        /// 剪裁图片
        /// </summary>
        /// <param name="bytes">图片内容</param>
        /// <returns>为空:剪裁失败或者不需要剪裁。</returns>
        private async Task <HttpResponseInfo> CutImage(byte[] bytes)
        {
            logger?.Info($"{nameof(CutImage)}: staring...");
            try
            {
                using (var ms = new MemoryStream(bytes))
                {
                    ms.Position = 0;
                    using (var inputStream = new SKManagedStream(ms))
                    {
                        using (var bitmap = SKBitmap.Decode(inputStream))
                        {
                            var h  = bitmap.Height;
                            var w  = bitmap.Width;
                            var w2 = h * 2 / 3; //封面宽度

                            if (w2 < w)         //需要剪裁
                            {
                                var x = await GetBaiduBodyAnalysisResult(bytes);

                                var start_w = w - w2;   //默认右边

                                if (x > 0)              //百度人体识别,中心点位置
                                {
                                    if (x + w2 / 2 > w) //右边
                                    {
                                        start_w = w - w2;
                                    }
                                    else if (x - w2 / 2 < 0)//左边
                                    {
                                        start_w = 0;
                                    }
                                    else //居中
                                    {
                                        start_w = (int)x - w2 / 2;
                                    }
                                }

                                var image = SKImage.FromBitmap(bitmap);

                                var subset      = image.Subset(SKRectI.Create(start_w, 0, w2, h));
                                var encodedData = subset.Encode(SKEncodedImageFormat.Jpeg, 90);
                                logger?.Info($"{nameof(CutImage)}: Already cut {w}x{h} --> start_w: {start_w}");
                                return(new HttpResponseInfo()
                                {
                                    Content = encodedData.AsStream(),
                                    ContentLength = encodedData.Size,
                                    ContentType = "image/jpeg",
                                    StatusCode = HttpStatusCode.OK,
                                });
                            }

                            logger?.Info($"{nameof(CutImage)}: not need to cut. {w}x{h}");
                            return(null);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger?.Warn($"{nameof(CutImage)}: cut image failed. {ex.Message}");
            }
            logger?.Warn($"{nameof(CutImage)}: cut image failed.");
            return(null);
        }
Ejemplo n.º 5
0
        void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
        {
            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;
            SKCanvas    canvas  = surface.Canvas;

            canvas.Clear(SKColors.Transparent);

            using (var bitmap = masterBitmap.Copy())
            {
                float scale = Math.Min((float)info.Width / bitmap.Width,
                                       (float)info.Height / bitmap.Height);
                float  x        = (info.Width - scale * bitmap.Width) / 2;
                float  y        = (info.Height - scale * bitmap.Height) / 2;
                SKRect destRect = new SKRect(x, y, x + scale * bitmap.Width,
                                             y + scale * bitmap.Height);

                if (predictions != null)
                {
                    // draw directly on the bitmap
                    using (var annotationCanvas = new SKCanvas(bitmap))
                        using (var textPaint = new SKPaint())
                            using (var boxPaint = new SKPaint())
                            {
                                boxPaint.StrokeWidth = 3 / scale;
                                textPaint.TextSize   = 25 / scale;
                                boxPaint.Style       = SKPaintStyle.Stroke;


                                foreach (var result in predictions)
                                {
                                    // Draw the bounding box
                                    var rect = result.BoundingBox;
                                    var zone = SKRectI.Create(
                                        (int)(rect.Left * bitmap.Width),
                                        (int)(rect.Top * bitmap.Height),
                                        (int)(rect.Width * bitmap.Width),
                                        (int)(rect.Height * bitmap.Height));

                                    boxPaint.Color = SKColors.Red;
                                    annotationCanvas.DrawRect(zone, boxPaint);

                                    // Draw the textbox
                                    textPaint.Color        = SKColors.Red;
                                    textPaint.FakeBoldText = true;
                                    //var textBox = SKRectI.Create(zone.Left, zone.Top - textPaint.TextSize, );

                                    var message = result.TagName + ":" + Math.Round(result.Probability, 2);
                                    annotationCanvas.DrawText(message, zone.Left + 10, zone.Top - textPaint.TextSize, textPaint);
                                }
                            }
                }

                // Resizing
                //var pictureFrame = canvasView.Bounds.ToSKRect();
                //var imageSize = new SKSize(bitmap.Width, bitmap.Height);
                //var dest = pictureFrame.AspectFill(imageSize);

                // draw the image
                //var paint = new SKPaint
                //{
                //	FilterQuality = SKFilterQuality.High // high quality scaling
                //};

                // draw the modified bitmap to the screen
                canvas.DrawBitmap(bitmap, destRect);
            }
        }
Ejemplo n.º 6
0
        private void DrawMarker(SKCanvas canvas, Marker marker)
        {
            SKRectI bounds     = new SKRectI();
            double  topLeftLat = tiles[0].Tile_lat;
            double  topLeftLon = tiles[0].Tile_lon;

            bool gotBounds = canvas.GetDeviceClipBounds(out bounds);

            double distanceFromCentreLat = marker.Position.Latitude - App.CurrentCentre.Latitude;
            double pixelsFromCentreLat   = distanceFromCentreLat / App.CurrentLatDegreesPerPixel;
            double yOff = (pixelsFromCentreLat * -1) + (bounds.MidY / App.ZoomScale);

            double distanceFromCentreLon = marker.Position.Longitude - App.CurrentCentre.Longitude;
            double pixelsFromCentreLon   = distanceFromCentreLon / App.CurrentLonDegreesPerPixel;
            double xOff = pixelsFromCentreLon + (bounds.MidX / App.ZoomScale);

            float x = Convert.ToSingle(xOff);
            float y = Convert.ToSingle(yOff);

            SKColor pointerColor = SKColors.White;

            marker.Point = new SKPoint(x, y);
            SKPaint circlePaint = new SKPaint();

            switch (marker.Type)
            {
            case MarkerType.Step:
                // Create circle fill


                circlePaint.Style       = SKPaintStyle.Fill;
                circlePaint.Color       = SKColors.White;
                circlePaint.IsAntialias = true;

                canvas.DrawCircle(x, y - 30, 18, circlePaint);



                // circle stroke
                circlePaint.Style       = SKPaintStyle.Stroke;
                circlePaint.StrokeWidth = 5.0f;
                circlePaint.Color       = SKColors.Red;
                circlePaint.IsAntialias = true;
                canvas.DrawCircle(x, y - 30, 18, circlePaint);

                // Add Text
                SKPaint textPaint = new SKPaint()
                {
                    TextSize    = 16.0f,
                    IsAntialias = true,
                    Color       = SKColors.Black,
                    Style       = SKPaintStyle.StrokeAndFill,
                    StrokeWidth = 1,
                    TextAlign   = SKTextAlign.Center
                };
                canvas.DrawText(marker.Number, x, y - 30 + (textPaint.TextSize / 2) - 2, textPaint);

                pointerColor = SKColors.Red;
                break;

            case MarkerType.Search:
                // Create circle fill


                circlePaint.Style       = SKPaintStyle.Fill;
                circlePaint.Color       = SKColors.Blue;
                circlePaint.IsAntialias = true;

                canvas.DrawCircle(x, y - 30, 18, circlePaint);



                // circle stroke
                circlePaint.Style       = SKPaintStyle.Stroke;
                circlePaint.StrokeWidth = 5.0f;
                circlePaint.Color       = SKColors.Blue;
                circlePaint.IsAntialias = true;
                canvas.DrawCircle(x, y - 30, 18, circlePaint);
                pointerColor = SKColors.Blue;
                break;

            case MarkerType.Gardens:
                // Create circle fill
                circlePaint.Style       = SKPaintStyle.Fill;
                circlePaint.Color       = SKColors.LightBlue;
                circlePaint.IsAntialias = true;
                canvas.DrawCircle(x, y - 30, 18, circlePaint);
                // circle stroke
                circlePaint.Style       = SKPaintStyle.Stroke;
                circlePaint.StrokeWidth = 5.0f;
                circlePaint.Color       = SKColors.Green;
                circlePaint.IsAntialias = true;
                canvas.DrawCircle(x, y - 30, 18, circlePaint);
                // draw flower
                circlePaint.Style = SKPaintStyle.Fill;
                //stalk
                circlePaint.Color = SKColors.Green;
                canvas.DrawRect(x - 1, y - 28, 2, 12, circlePaint);
                //petals
                circlePaint.Color = SKColors.Yellow;
                canvas.DrawCircle(x - 4, y - 34, 6, circlePaint);
                canvas.DrawCircle(x + 4, y - 34, 6, circlePaint);
                canvas.DrawCircle(x - 4, y - 30, 6, circlePaint);
                canvas.DrawCircle(x + 4, y - 30, 6, circlePaint);
                //centre
                circlePaint.Color = SKColors.Orange;
                canvas.DrawCircle(x, y - 32, 4, circlePaint);

                pointerColor = SKColors.Green;
                break;

            case MarkerType.Landscape:
                float x1 = x - 18;
                float y1 = y - 23;
                float x2 = x - 12;
                float y2 = y - 29;
                float x3 = x - 8;
                float y3 = y - 34;
                float x4 = x - 5;
                float y4 = y - 30;
                float x5 = x + 5;
                float y5 = y - 26;
                float x6 = x + 18;
                float y6 = y - 22;

                // Create circle fill
                circlePaint.Style       = SKPaintStyle.Fill;
                circlePaint.Color       = SKColors.LightBlue;
                circlePaint.IsAntialias = true;
                canvas.DrawCircle(x, y - 30, 18, circlePaint);
                circlePaint.Color       = SKColors.SandyBrown;
                circlePaint.Style       = SKPaintStyle.Stroke;
                circlePaint.StrokeWidth = 5;
                SKPath landPath1 = new SKPath();
                landPath1.MoveTo(x1, y1);
                landPath1.LineTo(x2, y2);
                landPath1.LineTo(x3, y3);
                landPath1.LineTo(x4, y4);
                landPath1.LineTo(x5, y5);
                landPath1.LineTo(x6, y6);
                canvas.DrawPath(landPath1, circlePaint);
                circlePaint.Color = SKColors.Green;
                SKPath landPath2 = new SKPath();
                landPath2.MoveTo(x1 + 3, y1 + 5);
                landPath2.LineTo(x2, y2 + 5);
                landPath2.LineTo(x3, y3 + 5);
                landPath2.LineTo(x4, y4 + 5);
                landPath2.LineTo(x5, y5 + 5);
                landPath2.LineTo(x6 - 2, y6 + 4);
                canvas.DrawPath(landPath2, circlePaint);
                circlePaint.Color = SKColors.DarkGreen;
                circlePaint.Style = SKPaintStyle.StrokeAndFill;
                SKPath landPath3 = new SKPath();
                landPath3.MoveTo(x1 + 8, y1 + 9);
                landPath3.LineTo(x2, y2 + 8);
                landPath3.LineTo(x3, y3 + 10);
                landPath3.LineTo(x4, y4 + 10);
                landPath3.LineTo(x5, y5 + 10);
                landPath3.LineTo(x6 - 6, y6 + 7);
                canvas.DrawPath(landPath3, circlePaint);
                // circle stroke
                circlePaint.Style       = SKPaintStyle.Stroke;
                circlePaint.StrokeWidth = 5.0f;
                circlePaint.Color       = SKColors.Green;
                circlePaint.IsAntialias = true;
                canvas.DrawCircle(x, y - 30, 18, circlePaint);


                pointerColor = SKColors.Green;
                break;

            case MarkerType.House:
                // create house
                // roof
                SKPath roofPath = new SKPath();
                roofPath.MoveTo(x - 13, y - 30);
                roofPath.LineTo(x - 11, y - 41);
                roofPath.LineTo(x + 10, y - 41);
                roofPath.LineTo(x + 12, y - 30);
                roofPath.LineTo(x - 13, y - 30);


                SKPaint roofPaint = new SKPaint
                {
                    Style       = SKPaintStyle.StrokeAndFill,
                    Color       = SKColors.SlateGray,
                    IsAntialias = true,
                    StrokeWidth = 1
                };
                canvas.DrawPath(roofPath, roofPaint);

                // building
                SKPaint rectPaint = new SKPaint
                {
                    Style       = SKPaintStyle.Fill,
                    Color       = SKColors.Firebrick,
                    IsAntialias = true
                };
                canvas.DrawRect(x - 11, y - 30, 21, 21, rectPaint);
                pointerColor = SKColors.Firebrick;

                // door
                SKPaint doorPaint = new SKPaint
                {
                    Style = SKPaintStyle.Fill,
                    Color = SKColors.DarkBlue
                };
                canvas.DrawRect(x - 2, y - 16, 4, 12, doorPaint);
                // window
                SKPaint windowPaint = new SKPaint
                {
                    Style = SKPaintStyle.Fill,
                    Color = SKColors.LightCyan
                };
                canvas.DrawRect(x - 8, y - 24, 5, 4, windowPaint);
                canvas.DrawRect(x + 2, y - 24, 5, 4, windowPaint);
                break;
            }
            //Add pointer

            // Create the path
            SKPath pointerPath = new SKPath();

            pointerPath.MoveTo(x, y);
            pointerPath.LineTo(x - 4, y - 9);
            pointerPath.LineTo(x + 4, y - 9);
            pointerPath.LineTo(x, y);
            // Create two SKPaint objects
            SKPaint pointerPaint = new SKPaint
            {
                Style       = SKPaintStyle.StrokeAndFill,
                Color       = pointerColor,
                IsAntialias = true,
                StrokeWidth = 1
            };

            canvas.DrawPath(pointerPath, pointerPaint);
        }
Ejemplo n.º 7
0
 public SpriteData(SKRectI textureBounds)
 {
     TextureBounds    = textureBounds;
     OriginNormalized = new SKPoint(.5f, .5f);
 }
Ejemplo n.º 8
0
 public static Int32Rect ToInt32Rect(this SKRectI rect)
 {
     return(new Int32Rect(rect.Left, rect.Top, rect.Width, rect.Height));
 }
Ejemplo n.º 9
0
 public static CGRect ToCGRect(this SKRectI rect) => new CGRect(rect.Left, rect.Top, rect.Width, rect.Height);
Ejemplo n.º 10
0
 /// <summary>
 /// Pixelates a given image area defined by extractRect of the original image and draws it to the canvas.
 /// </summary>
 /// <param name="canvas"></param>
 /// <param name="extractRect"></param>
 /// <param name="original"></param>
 /// <param name="outline"></param>
 /// <param name="pixelSizeFunction"></param>
 private static void Pixelate(SKCanvas canvas, SKRectI extractRect, SKBitmap original, SKPaint outline,
                              Func <int, int, int, int, int> pixelSizeFunction)
 {
     Pixelate(canvas, extractRect, original, pixelSizeFunction);
     canvas.DrawRect(extractRect, outline);
 }
Ejemplo n.º 11
0
 public static Rect ToRect(this SKRectI rect)
 {
     return(new Rect(rect.Location.ToPoint(), rect.Size.ToSize()));
 }
Ejemplo n.º 12
0
        private async Task <Stream> Pixelate(SKBitmap bitmap, SKEncodedOrigin origin, IFeatureExtractor featureExtractor)
        {
            // fix orientation if encoded origin is not TopLeft/Default
            bitmap = FixOrientation(bitmap, origin);

            // a surface is something like a table we need to actually draw stuff
            var surface = SKSurface.Create(new SKImageInfo(bitmap.Width, bitmap.Height));
            // get the canvas where we actually draw onto
            var canvas = surface.Canvas;

            canvas.Clear(SKColors.Transparent);
            // apply the original image to the canvas
            canvas.DrawBitmap(bitmap, 0, 0);

            // extract the image features
            switch (FaceProcessing)
            {
            case FaceProcessing.PixelateFaces:
            {
                var faces = (await featureExtractor.ExtractFacesAsync()).ToArray();
                Logger.OnExtractedFaces(_imagePath, faces);
                foreach (var face in faces)
                {
                    if (FaceOutline != null)
                    {
                        Pixelate(canvas, SKRectI.Create(face.X, face.Y, face.Width, face.Height), bitmap, FaceOutline,
                                 PixelSizeFunction);
                    }
                    else
                    {
                        Pixelate(canvas, SKRectI.Create(face.X, face.Y, face.Width, face.Height), bitmap,
                                 PixelSizeFunction);
                    }

                    Logger.OnPixelatedFace(_imagePath, face);
                }

                break;
            }

            case FaceProcessing.PixelatePersons:
            {
                var persons = (await featureExtractor.ExtractPersonsAsync()).ToArray();
                Logger.OnExtractedPersons(_imagePath, persons);
                foreach (var person in persons)
                {
                    if (FaceOutline != null)
                    {
                        Pixelate(canvas, SKRectI.Create(person.X, person.Y, person.Width, person.Height), bitmap,
                                 FaceOutline, PixelSizeFunction);
                    }
                    else
                    {
                        Pixelate(canvas, SKRectI.Create(person.X, person.Y, person.Width, person.Height), bitmap,
                                 PixelSizeFunction);
                    }

                    Logger.OnPixelatedPerson(_imagePath, person);
                }

                break;
            }

            case FaceProcessing.Skip:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            switch (CarProcessing)
            {
            case CarProcessing.PixelatePlatesAndTextOnCars:
            {
                var text = (await featureExtractor.ExtractTextAsync()).ToArray();
                Logger.OnExtractedText(_imagePath, text);
                var cars = (await featureExtractor.ExtractCarsAsync()).ToArray();
                Logger.OnExtractedCars(_imagePath, cars);
                var licensePlates = (await featureExtractor.ExtractLicensePlatesAsync()).ToArray();
                Logger.OnExtractedLicensePlates(_imagePath, licensePlates);

                var mergeDistance = (int)(bitmap.Width * MergeFactor);
                var merged        = ImagePatchClusterizer.Clusterize(text, mergeDistance).ToArray();

                foreach (var car in cars)
                {
                    foreach (var patch in merged)
                    {
                        // If patch is inside of the car borders
                        if (patch.Y >= car.Y &&
                            patch.X >= car.X &&
                            patch.Y <= car.Y + car.Height &&
                            patch.X <= car.X + car.Width &&
                            patch.Width <= car.Width + car.X - patch.X &&
                            patch.Height <= car.Height + car.Y - patch.Y)
                        {
                            if (PlateOutline != null)
                            {
                                Pixelate(canvas, SKRectI.Create(patch.X, patch.Y, patch.Width, patch.Height),
                                         bitmap, PlateOutline, PixelSizeFunction);
                            }
                            else
                            {
                                Pixelate(canvas, SKRectI.Create(patch.X, patch.Y, patch.Width, patch.Height),
                                         bitmap, PixelSizeFunction);
                            }

                            Logger.OnPixelatedText(_imagePath, patch);
                        }
                    }
                }

                foreach (var plate in licensePlates)
                {
                    if (PlateOutline != null)
                    {
                        Pixelate(canvas, SKRectI.Create(plate.X, plate.Y, plate.Width, plate.Height), bitmap,
                                 PlateOutline, PixelSizeFunction);
                    }
                    else
                    {
                        Pixelate(canvas, SKRectI.Create(plate.X, plate.Y, plate.Width, plate.Height), bitmap,
                                 PixelSizeFunction);
                    }

                    Logger.OnPixelatedLicensePlate(_imagePath, plate);
                }

                break;
            }

            case CarProcessing.PixelateCars:
            {
                var cars = (await featureExtractor.ExtractCarsAsync()).ToArray();
                Logger.OnExtractedCars(_imagePath, cars);
                foreach (var car in cars)
                {
                    if (PlateOutline != null)
                    {
                        Pixelate(canvas, SKRectI.Create(car.X, car.Y, car.Width, car.Height), bitmap, PlateOutline,
                                 PixelSizeFunction);
                    }
                    else
                    {
                        Pixelate(canvas, SKRectI.Create(car.X, car.Y, car.Width, car.Height), bitmap,
                                 PixelSizeFunction);
                    }

                    Logger.OnPixelatedCar(_imagePath, car);
                }

                break;
            }

            case CarProcessing.Skip:
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(surface.Snapshot().Encode(OutputFormat, OutputQuality).AsStream());
        }
Ejemplo n.º 13
0
        /// <summary>
        /// Resize & Crop an image to 512x512
        /// </summary>
        /// <param name="file">The stream that contains the image</param>
        /// <returns>The resized and cropped image</returns>
        public static (byte[] file, string mime) ResizeIcon(Stream file, string mime)
        {
            var img = SKBitmap.Decode(file);

            file.Position = 0;
            byte[] newFile = null;
            if (img.Width == 512 && img.Height == 512)
            {
                return(StreamToByteA(file), mime);
            }
            if ((img.Width == img.Height))
            {
                var newImg = img.Resize(new SKImageInfo(512, 512), SKFilterQuality.High);
                var skimg  = SKImage.FromBitmap(newImg);
                var data   = skimg.Encode();
                newFile = data.ToArray();
                //newFile.Position = 0;
                data.Dispose();
                skimg.Dispose();
            }
            else
            {
                int   tH, tW;
                float r;
                if (img.Height > img.Width)
                {
                    r = img.Width / (float)512;
                }
                else
                {
                    r = img.Height / (float)512;
                }
                tW = (int)(img.Width / r);
                tH = (int)(img.Height / r);
                var downImg = img.Resize(new SKImageInfo(tW, tH), SKFilterQuality.High);
                var skimg = SKImage.FromBitmap(downImg);
                int h, w, t, l;
                if (skimg.Height > skimg.Width)
                {
                    h = Math.Min(512, skimg.Width);
                    w = h;
                    t = skimg.Height - (w / 2) - (skimg.Height / 2);
                    l = 0;
                }
                else
                {
                    w = Math.Min(512, skimg.Height);
                    h = w;
                    l = skimg.Width - (h / 2) - (skimg.Width / 2);
                    t = 0;
                }
                var cropRect = new SKRectI(l, t, l + w, t + h);

                var crop = skimg.Subset(cropRect);
                var data = crop.Encode();
                newFile = data.ToArray();
                data.Dispose();
                crop.Dispose();
                skimg.Dispose();
                downImg.Dispose();
            }
            img.Dispose();
            file.Dispose();
            return(newFile, "image/png");
        }
Ejemplo n.º 14
0
        public static SKBitmap ResizeImage(SKBitmap original, int w, int h, string mode)
        {
            SKBitmap bitmap = null;

            if (w == 0 && h != 0)
            {
                w = original.Width * h / original.Height;

                var imageInfo = new SKImageInfo(w, h, SKImageInfo.PlatformColorType, original.AlphaType);

                bitmap = original.Resize(imageInfo, SKFilterQuality.High);
            }
            else if (h == 0 && w != 0)
            {
                h = original.Height * w / original.Width;

                var imageInfo = new SKImageInfo(w, h, SKImageInfo.PlatformColorType, original.AlphaType);

                bitmap = original.Resize(imageInfo, SKFilterQuality.High);
            }
            else if (w != 0 && h != 0)
            {
                float originalRatio = ((float)original.Height) / ((float)original.Width);
                float ratio         = ((float)h) / ((float)w);

                switch (mode)
                {
                case "pad":
                {
                    SKRectI drawRect;
                    if (originalRatio < ratio)
                    {
                        var newW = w;
                        var newH = original.Height * w / original.Width;
                        var pad  = (h - newH) / 2;

                        drawRect = new SKRectI
                        {
                            Left   = 0,
                            Top    = pad,
                            Right  = newW,
                            Bottom = newH + pad
                        };
                    }
                    else
                    {
                        var newW = original.Width * h / original.Height;
                        var newH = h;
                        var pad  = (w - newW) / 2;

                        drawRect = new SKRectI
                        {
                            Left   = pad,
                            Top    = 0,
                            Right  = newW + pad,
                            Bottom = newH
                        };
                    }

                    bitmap = new SKBitmap(w, h, true);
                    var canvas = new SKCanvas(bitmap);
                    canvas.Clear(new SKColor(255, 255, 255));

                    var imageInfo = new SKImageInfo(drawRect.Width, drawRect.Height, SKImageInfo.PlatformColorType,
                                                    original.AlphaType);
                    original = original.Resize(imageInfo, SKFilterQuality.High);

                    canvas.DrawBitmap(original, drawRect, new SKPaint());

                    canvas.Flush();
                    canvas.Dispose();

                    break;
                }

                case "crop":
                {
                    SKRectI drawRect;

                    if (originalRatio < ratio)
                    {
                        var newW = original.Width * h / original.Height;

                        var pad = (newW - w) / 2;

                        var imageInfo = new SKImageInfo(newW, h, SKImageInfo.PlatformColorType, original.AlphaType);

                        var resizedBitmap = original.Resize(imageInfo, SKFilterQuality.High);

                        drawRect = new SKRectI
                        {
                            Left   = pad,
                            Top    = 0,
                            Right  = w + pad,
                            Bottom = h
                        };

                        bitmap = new SKBitmap(drawRect.Width, drawRect.Height, true);

                        resizedBitmap.ExtractSubset(bitmap, drawRect);
                    }
                    else
                    {
                        var newH = original.Height * w / original.Width;

                        var pad = (newH - h) / 2;

                        var imageInfo = new SKImageInfo(w, newH, SKImageInfo.PlatformColorType, original.AlphaType);

                        var resizedBitmap = original.Resize(imageInfo, SKFilterQuality.High);

                        drawRect = new SKRectI
                        {
                            Left   = 0,
                            Top    = pad,
                            Right  = w,
                            Bottom = h + pad
                        };

                        bitmap = new SKBitmap(drawRect.Width, drawRect.Height);

                        resizedBitmap.ExtractSubset(bitmap, drawRect);
                    }

                    break;
                }

                default:
                    break;
                }
            }
            else
            {
                bitmap = original.Copy();
            }

            original.Dispose();

            return(bitmap);
        }
Ejemplo n.º 15
0
        protected override void OnPaintSurface(SKPaintSurfaceEventArgs e)
        {
            Trace.WriteLine($"{GetType().Name} OnPaintSurface");
            SKImageInfo info    = e.Info;
            SKSurface   surface = e.Surface;
            SKCanvas    canvas  = surface.Canvas;

            RadiusDefault = Math.Max(info.Width, info.Height) / 2;
            if (Radius == 0)
            {
                Radius = RadiusDefault;
            }

            // 押下状態では影を大きくする
            var seed        = Math.Min(info.Rect.Width, info.Rect.Height);
            int bodyDelta   = seed / 10;
            var bodyRect    = new SKRectI(info.Rect.Left + bodyDelta, info.Rect.Top + bodyDelta, info.Rect.Right - bodyDelta, info.Rect.Bottom - bodyDelta);
            int shadowDelta = IsPressed ? 0 : seed / 20;
            var shadowRect  = new SKRectI(info.Rect.Left + shadowDelta, info.Rect.Top + shadowDelta, info.Rect.Right - shadowDelta, info.Rect.Bottom - shadowDelta);
            var shadowColor = SKColors.DarkGray;
            var sunnyColor  = SKColors.Transparent;

            canvas.Clear();

            // 上下の影
            using (var paint = new SKPaint())
            {
                float start = (float)(bodyRect.Top - shadowRect.Top) / shadowRect.Height;
                float end   = 1 - start;
                var   rect  = new SKRectI(bodyRect.Left, shadowRect.Top, bodyRect.Right, shadowRect.Bottom);

                paint.Shader = SKShader.CreateLinearGradient(
                    new SKPoint(rect.MidX, rect.Top),
                    new SKPoint(rect.MidX, rect.Bottom),
                    new SKColor[] { sunnyColor, shadowColor, shadowColor, sunnyColor },
                    new float[] { 0, start, end, 1 },
                    SKShaderTileMode.Clamp);
                canvas.DrawRect(rect, paint);
            }

            // 左右の影
            using (var paint = new SKPaint())
            {
                float start = (float)(bodyRect.Left - shadowRect.Left) / shadowRect.Width;
                float end   = 1 - start;
                var   rect  = new SKRectI(shadowRect.Left, bodyRect.Top, shadowRect.Right, bodyRect.Bottom);

                paint.Shader = SKShader.CreateLinearGradient(
                    new SKPoint(rect.Left, rect.MidY),
                    new SKPoint(rect.Right, rect.MidY),
                    new SKColor[] { sunnyColor, shadowColor, shadowColor, sunnyColor },
                    new float[] { 0, start, end, 1 },
                    SKShaderTileMode.Clamp);
                canvas.DrawRect(rect, paint);
            }

            // 四隅 左上
            using (var paint = new SKPaint())
            {
                var rect = new SKRectI(shadowRect.Left, shadowRect.Top, bodyRect.Left, bodyRect.Top);

                paint.Shader = SKShader.CreateRadialGradient(
                    new SKPoint(rect.Right, rect.Bottom),
                    rect.Width,
                    new SKColor[] { shadowColor, sunnyColor },
                    null,
                    SKShaderTileMode.Clamp);
                canvas.DrawRect(rect, paint);
            }
            // 四隅 右上
            using (var paint = new SKPaint())
            {
                var rect = new SKRectI(bodyRect.Right, shadowRect.Top, shadowRect.Right, bodyRect.Top);

                paint.Shader = SKShader.CreateRadialGradient(
                    new SKPoint(rect.Left, rect.Bottom),
                    rect.Width,
                    new SKColor[] { shadowColor, sunnyColor },
                    null,
                    SKShaderTileMode.Clamp);
                canvas.DrawRect(rect, paint);
            }
            // 四隅 左下
            using (var paint = new SKPaint())
            {
                var rect = new SKRectI(shadowRect.Left, bodyRect.Bottom, bodyRect.Left, shadowRect.Bottom);

                paint.Shader = SKShader.CreateRadialGradient(
                    new SKPoint(rect.Right, rect.Top),
                    rect.Width,
                    new SKColor[] { shadowColor, sunnyColor },
                    null,
                    SKShaderTileMode.Clamp);
                canvas.DrawRect(rect, paint);
            }
            // 四隅 右下
            using (var paint = new SKPaint())
            {
                var rect = new SKRectI(bodyRect.Right, bodyRect.Bottom, shadowRect.Right, shadowRect.Bottom);

                paint.Shader = SKShader.CreateRadialGradient(
                    new SKPoint(rect.Left, rect.Top),
                    rect.Width,
                    new SKColor[] { shadowColor, sunnyColor },
                    null,
                    SKShaderTileMode.Clamp);
                canvas.DrawRect(rect, paint);
            }

            // 本体
            using (var paint = new SKPaint())
            {
                // 非活性状態ではグレー
                var centerColor = IsEnabled ? CenterColor.ToSKColor() : SKColors.WhiteSmoke;
                var edgeColor   = IsEnabled ? EdgeColor.ToSKColor() : SKColors.LightGray;

                // プラットフォームにより影のサイズが異なるため切れてしまう
                //var sigma = IsPressed ? 4 : 1;
                //paint.ImageFilter = SKImageFilter.CreateDropShadow(0, 0, sigma, sigma, SKColors.Black, SKDropShadowImageFilterShadowMode.DrawShadowAndForeground);

                paint.Shader = SKShader.CreateRadialGradient(
                    new SKPoint(info.Rect.MidX, info.Rect.MidY),
                    Radius,
                    new SKColor[] { centerColor, edgeColor },
                    null,
                    SKShaderTileMode.Clamp);
                canvas.DrawRect(bodyRect, paint);
            }

            // 文字
            using (var paint = new SKPaint())
            {
                paint.Color       = IsEnabled ? SKColors.Black : SKColors.Gray;
                paint.Typeface    = SKTypeface.FromFamilyName("Arial", SKTypefaceStyle.Bold);
                paint.IsAntialias = true;

                // 見やすくならない
                //paint.ImageFilter = SKImageFilter.CreateDropShadow(2, 2, 2, 2, SKColors.Black, SKDropShadowImageFilterShadowMode.DrawShadowAndForeground);

                var textBounds = new SKRect();
                paint.MeasureText(Text, ref textBounds);

                float height = textBounds.Height;
                paint.TextSize = 0.4f * bodyRect.Height * paint.TextSize / height;
                paint.MeasureText(Text, ref textBounds);

                float xText = bodyRect.Width / 2 - textBounds.MidX + bodyDelta;
                float yText = bodyRect.Height / 2 - textBounds.MidY + bodyDelta;

                canvas.DrawText(Text, xText, yText, paint);
            }
        }
Ejemplo n.º 16
0
 internal ClipIterator(SKRegion region, SKRectI clip)
     : base(SkiaApi.sk_region_cliperator_new(region.Handle, &clip), true)
 {
     this.region = region;
     this.clip   = clip;
 }
Ejemplo n.º 17
0
        private void OnCanvasViewPaintSurface(object sender, SkiaSharp.Views.Forms.SKPaintSurfaceEventArgs args)
        {
            Debug.WriteLine("*************PaintSUrface map");

            bitmaps = new List <SKBitmap>();

            GetWalkMarkers();


            var newTiles = GetTiles();

            if (newTiles != null)
            {
                tiles = newTiles;
                Debug.WriteLine("No. of Tiles:" + tiles.Count.ToString());
            }


            if (tiles.Count != 49)
            {
                lastTileColRow.Column       = 0;
                lastTileColRow.Row          = 0;
                App.CurrentCentre.Latitude  = App.PreviousCentre.Latitude;
                App.CurrentCentre.Longitude = App.PreviousCentre.Longitude;
                tiles = GetTiles();
            }
            for (int i = 0; i < tiles.Count; i++)
            {
                MemoryStream stream = new MemoryStream(tiles[i].Tile_data);
                bitmaps.Add(SKBitmap.Decode(stream));
            }


            SKImageInfo info    = args.Info;
            SKSurface   surface = args.Surface;

            canvas = surface.Canvas;
            SKRect  deviceClipBounds = canvas.DeviceClipBounds;
            SKRectI bounds           = new SKRectI();
            bool    gotBounds        = canvas.GetDeviceClipBounds(out bounds);



            canvas.Clear();
            canvas.Scale(App.ZoomScale);



            canvas.DrawColor(SKColors.White);
            int b = 0;

            double topLeftLat = tiles[0].Tile_lat;
            double topLeftLon = tiles[0].Tile_lon;


            double distanceToCentreLat = topLeftLat - App.CurrentCentre.Latitude;
            double pixelsToCentreLat   = distanceToCentreLat / App.CurrentLatDegreesPerPixel;
            double yOff = (pixelsToCentreLat) - (bounds.MidY / App.ZoomScale);



            double distanceToCentreLon = Math.Abs(topLeftLon - App.CurrentCentre.Longitude);
            double pixelsToCentreLon   = distanceToCentreLon / App.CurrentLonDegreesPerPixel;
            double xOff = (pixelsToCentreLon) - (bounds.MidX / App.ZoomScale);



            xOffset = Convert.ToSingle(xOff);
            yOffset = Convert.ToSingle(yOff);



            SKPaint bitmapPaint = new SKPaint();

            bitmapPaint.FilterQuality = SKFilterQuality.High;


            for (int x = 0; x < tiles.Count / 7; x++)     //
            {
                for (int y = 0; y < tiles.Count / 7; y++) //
                {
                    float fx = Convert.ToSingle((x * 256) - (xOffset));
                    float fy = Convert.ToSingle((y * 256) - (yOffset));

                    SKImage image = SKImage.FromBitmap(bitmaps[b++]);
                    canvas.DrawImage(image, fx, fy, bitmapPaint);
                }
            }

            //canvas.Translate(-xOffset, -yOffset);

            foreach (Marker marker in markers)
            {
                switch (marker.Type)
                {
                case MarkerType.Facility:
                    if (App.LayerFacilities)
                    {
                        DrawMarker(canvas, marker);
                    }
                    break;

                case MarkerType.Gardens:
                    if (App.LayerGardens)
                    {
                        DrawMarker(canvas, marker);
                    }
                    break;

                case MarkerType.House:
                    if (App.LayerHouses)
                    {
                        DrawMarker(canvas, marker);
                    }
                    break;

                case MarkerType.Landscape:
                    if (App.LayerLandscape)
                    {
                        DrawMarker(canvas, marker);
                    }
                    break;

                case MarkerType.Poi:
                    if (App.LayerPois)
                    {
                        DrawMarker(canvas, marker);
                    }
                    break;

                case MarkerType.Step:
                    if (App.LayerSteps)
                    {
                        DrawMarker(canvas, marker);
                    }
                    break;

                case MarkerType.Search:

                    DrawMarker(canvas, marker);

                    break;
                }
            }
        }
Ejemplo n.º 18
0
 private RRect ConvertRectI(SKRectI rect)
 {
     return(new RRect(rect.Left, rect.Top, rect.Width, rect.Height));
 }
Ejemplo n.º 19
0
        private SKBitmap CropWhiteSpace(SKBitmap bitmap)
        {
            var topmost = 0;

            for (int row = 0; row < bitmap.Height; ++row)
            {
                if (IsAllWhiteRow(bitmap, row))
                {
                    topmost = row;
                }
                else
                {
                    break;
                }
            }

            int bottommost = 0;

            for (int row = bitmap.Height - 1; row >= 0; --row)
            {
                if (IsAllWhiteRow(bitmap, row))
                {
                    bottommost = row;
                }
                else
                {
                    break;
                }
            }

            int leftmost = 0, rightmost = 0;

            for (int col = 0; col < bitmap.Width; ++col)
            {
                if (IsAllWhiteColumn(bitmap, col))
                {
                    leftmost = col;
                }
                else
                {
                    break;
                }
            }

            for (int col = bitmap.Width - 1; col >= 0; --col)
            {
                if (IsAllWhiteColumn(bitmap, col))
                {
                    rightmost = col;
                }
                else
                {
                    break;
                }
            }

            var newRect = SKRectI.Create(leftmost, topmost, rightmost - leftmost, bottommost - topmost);

            using (var image = SKImage.FromBitmap(bitmap))
            {
                using (var subset = image.Subset(newRect))
                {
                    return(SKBitmap.FromImage(subset));
                    //using (var data = subset.Encode(StripCollageBuilder.GetEncodedFormat(outputPath), 90))
                    //{
                    //    using (var fileStream = _fileSystem.GetFileStream(outputPath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read))
                    //    {
                    //        data.AsStream().CopyTo(fileStream);
                    //    }
                    //}
                }
            }
        }
Ejemplo n.º 20
0
        private void DrawView(RootObject rootObject, Image img)
        {
            Stream streamD = null;
            var    width   = img.Width;
            var    height  = img.Height;

            if (rootObject != null && rootObject.predictions.Count > 0)
            {
                //  beerRecognizedList = new List<RootObject>();
                //processedImage.Image = pcbOriginal.Image;
                //processedImage.SizeMode = PictureBoxSizeMode.StretchImage;
                int i = 0;
                foreach (var item in rootObject.predictions)
                {
                    if (item.probability > .50)
                    {
                        var bitmap = SKBitmap.Decode(App.CroppedImage);

                        // create a canvas for drawing
                        var canvas = new SKCanvas(bitmap);

                        // draw a rectangle with a red border
                        var paint = new SKPaint
                        {
                            Style       = SKPaintStyle.Stroke,
                            Color       = SKColors.Red,
                            StrokeWidth = 5
                        };

                        canvas.DrawRect(SKRect.Create(10, 10, 10, 10), paint);

                        // get an image subset to save
                        var im     = SKImage.FromBitmap(bitmap);
                        var subset = im.Subset(SKRectI.Create(20, 20, 90, 90));

                        // encode the image
                        var encodedData = im.Encode(SKEncodedImageFormat.Png, 75);

                        // get a stream that can be saved to disk/memory/etc
                        var stream = encodedData.AsStream();

                        image.Source = ImageSource.FromStream(() => streamD);

                        Content = image;
                    }
                }



                //foreach (var item in rootObject.predictions)
                //{
                //    if (item.probability > .50)
                //    {
                //        var left = item.boundingBox.left * width;
                //        var top = item.boundingBox.top * height;
                //        var widthX = item.boundingBox.width * width;
                //        var heightY = item.boundingBox.height * height;

                //        canvas.DrawRect(SKRect.Create((float)left, (float)top, (float)widthX, (float)heightY), paint);

                //        // get an image subset to save
                //        //var image = SKImage.FromBitmap(bitmap);
                //        //var subset = image.Subset(SKRectI.Create(Convert.ToInt32(left), Convert.ToInt32(top), Convert.ToInt32(widthX), Convert.ToInt32(heightY)));

                //        // encode the image
                //        //var encodedData = subset.Encode(SKEncodedImageFormat.Png, 100);

                //        // get a stream that can be saved to disk/memory/etc
                //        //streamD= encodedData.AsStream();
                //    }
                //}
                //image.Source = ImageSource.FromStream(() => streamD);

                //Content = image;
            }
        }
Ejemplo n.º 21
0
 public static System.Drawing.Rectangle ToDrawingRect(this SKRectI rect)
 {
     return(System.Drawing.Rectangle.FromLTRB(rect.Left, rect.Top, rect.Right, rect.Bottom));
 }
Ejemplo n.º 22
0
        // Xamarin.Forms.Size

        public static Rectangle ToFormsRect(this SKRectI rect)
        {
            return(new Rectangle(rect.Left, rect.Top, rect.Width, rect.Height));
        }
Ejemplo n.º 23
0
 public void SKRectIRoundWorksAsExpected()
 {
     Assert.AreEqual(new SKRectI(6, 6, 21, 21), SKRectI.Round(new SKRect(5.51f, 5.51f, 20.51f, 20.51f)));
     Assert.AreEqual(new SKRectI(5, 6, 20, 21), SKRectI.Round(new SKRect(5.41f, 5.61f, 20.41f, 20.61f)));
     Assert.AreEqual(new SKRectI(20, 21, 5, 6), SKRectI.Round(new SKRect(20.41f, 20.61f, 5.41f, 5.61f)));
 }
Ejemplo n.º 24
0
 public LayerChunk(Layer layer, SKRectI absoluteChunkRect)
 {
     Layer             = layer;
     AbsoluteChunkRect = absoluteChunkRect;
 }
Ejemplo n.º 25
0
 public static Rectangle ToRect(this SKRectI rect)
 {
     return(new Rectangle(rect.Left, rect.Top, rect.Right, rect.Bottom));
 }
Ejemplo n.º 26
0
        private SKBitmap BuildThumbCollageBitmap(string[] paths, int width, int height)
        {
            var bitmap = new SKBitmap(width, height);

            using (var canvas = new SKCanvas(bitmap))
            {
                canvas.Clear(SKColors.Black);

                // determine sizes for each image that will composited into the final image
                var iSlice  = Convert.ToInt32(width * 0.23475);
                int iTrans  = Convert.ToInt32(height * .25);
                int iHeight = Convert.ToInt32(height * .70);
                var horizontalImagePadding = Convert.ToInt32(width * 0.0125);
                var verticalSpacing        = Convert.ToInt32(height * 0.01111111111111111111111111111111);
                int imageIndex             = 0;

                for (int i = 0; i < 4; i++)
                {
                    using (var currentBitmap = SKBitmap.Decode(paths[imageIndex]))
                    {
                        // resize to the same aspect as the original
                        int iWidth = (int)Math.Abs(iHeight * currentBitmap.Width / currentBitmap.Height);
                        using (var resizeBitmap = new SKBitmap(iWidth, iHeight, currentBitmap.ColorType, currentBitmap.AlphaType))
                        {
                            currentBitmap.Resize(resizeBitmap, SKBitmapResizeMethod.Lanczos3);
                            // determine how much to crop
                            int ix = (int)Math.Abs((iWidth - iSlice) / 2);
                            using (var image = SKImage.FromBitmap(resizeBitmap))
                            {
                                // crop image
                                using (var subset = image.Subset(SKRectI.Create(ix, 0, iSlice, iHeight)))
                                {
                                    // draw image onto canvas
                                    canvas.DrawImage(subset, (horizontalImagePadding * (i + 1)) + (iSlice * i), verticalSpacing);

                                    using (var croppedBitmap = SKBitmap.FromImage(subset))
                                    {
                                        // create reflection of image below the drawn image
                                        using (var reflectionBitmap = new SKBitmap(croppedBitmap.Width, croppedBitmap.Height / 2, croppedBitmap.ColorType, croppedBitmap.AlphaType))
                                        {
                                            // resize to half height
                                            croppedBitmap.Resize(reflectionBitmap, SKBitmapResizeMethod.Lanczos3);

                                            using (var flippedBitmap = new SKBitmap(reflectionBitmap.Width, reflectionBitmap.Height, reflectionBitmap.ColorType, reflectionBitmap.AlphaType))
                                            {
                                                using (var flippedCanvas = new SKCanvas(flippedBitmap))
                                                {
                                                    // flip image vertically
                                                    var matrix = SKMatrix.MakeScale(1, -1);
                                                    matrix.SetScaleTranslate(1, -1, 0, flippedBitmap.Height);
                                                    flippedCanvas.SetMatrix(matrix);
                                                    flippedCanvas.DrawBitmap(reflectionBitmap, 0, 0);
                                                    flippedCanvas.ResetMatrix();

                                                    // create gradient to make image appear as a reflection
                                                    var remainingHeight = height - (iHeight + (2 * verticalSpacing));
                                                    flippedCanvas.ClipRect(SKRect.Create(reflectionBitmap.Width, remainingHeight));
                                                    using (var gradient = new SKPaint())
                                                    {
                                                        gradient.IsAntialias = true;
                                                        gradient.BlendMode   = SKBlendMode.SrcOver;
                                                        gradient.Shader      = SKShader.CreateLinearGradient(new SKPoint(0, 0), new SKPoint(0, remainingHeight), new[] { new SKColor(0, 0, 0, 128), new SKColor(0, 0, 0, 208), new SKColor(0, 0, 0, 240), new SKColor(0, 0, 0, 255) }, null, SKShaderTileMode.Clamp);
                                                        flippedCanvas.DrawPaint(gradient);
                                                    }

                                                    // finally draw reflection onto canvas
                                                    canvas.DrawBitmap(flippedBitmap, (horizontalImagePadding * (i + 1)) + (iSlice * i), iHeight + (2 * verticalSpacing));
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                    imageIndex++;

                    if (imageIndex >= paths.Length)
                    {
                        imageIndex = 0;
                    }
                }
            }

            return(bitmap);
        }
Ejemplo n.º 27
0
        public SearchFaces(byte[] photoArray, int[] additionalRotateAngles = null, int photoJpegQuality = 95, double faceClippingRatio = 1.2)
        {
            if (photoArray != null)
            {
                try
                {
                    using (Stream input = new MemoryStream(photoArray))
                    {
                        photoArray = null;
                        using (var inputStream = new SKManagedStream(input))
                            using (var codec = SKCodec.Create(inputStream))
                                using (var original = SKBitmap.Decode(codec))
                                {
                                    // ЧИТАЕМ EXIF-ИНФОРМАЦИЮ
                                    input.Position = 0;
                                    try
                                    {
                                        using (ExifReader reader = new ExifReader(input))
                                        {
                                            reader.GetTagValue(ExifTags.DateTime, out photoDateTime);
                                            reader.GetTagValue(ExifTags.BodySerialNumber, out cameraSerialNumber);
                                        }
                                    }
                                    catch (Exception exc) { }

                                    // НОРМАЛИЗУЕМ ИЗОБРАЖЕНИE ПО ВРАЩЕНИЮ
                                    SKBitmap normalized  = AdjustOrientation(original, codec.EncodedOrigin);
                                    double   scaleFactor = 2;

                                    // ПОЛУЧАЕМ ДЕТЕКТИРУЕМОЕ НА ЛИЦА ИЗОБРАЖЕНИЕ
                                    using (var scanned = normalized.Resize(new SKImageInfo((int)Math.Round((double)normalized.Width / scaleFactor), (int)Math.Round((double)normalized.Height / scaleFactor)), SKFilterQuality.High))
                                    {
                                        if (scanned == null)
                                        {
                                            return;
                                        }

                                        int additionalFacesCounter = 0;

                                        List <FaceLocation> faceLocationList = new List <FaceLocation>();

                                        using (var fd = Dlib.GetFrontalFaceDetector())
                                        {
                                            DlibDotNet.Rectangle[] faces = null;
                                            using (var array2D = Dlib.LoadImageData <RgbPixel>(ImagePixelFormat.Rgba, scanned.Bytes, (uint)scanned.Height, (uint)scanned.Width, (uint)(scanned.Bytes.Length / scanned.Height)))
                                                faces = fd.Operator(array2D);

                                            if (faces != null && faces.Length > 0)
                                            {
                                                for (int f = 0; f < faces.Length; f++)
                                                {
                                                    #region обрезаем лицо до квадрата
                                                    Point center = faces[f].Center;
                                                    int   radius = 0;
                                                    if (faces[f].Width < faces[f].Height)
                                                    {
                                                        radius = (int)faces[f].Width / 2;
                                                    }
                                                    else
                                                    {
                                                        radius = (int)faces[f].Height / 2;
                                                    }
                                                    faces[f].Left   = center.X - radius;
                                                    faces[f].Right  = center.X + radius;
                                                    faces[f].Top    = center.Y - radius;
                                                    faces[f].Bottom = center.Y + radius;
                                                    #endregion обрезаем лицо до квадрата
                                                    FaceLocation faceLocation = CalculateNormalFaceLocation(faces[f], normalized.Width, normalized.Height, scaleFactor, faceClippingRatio);
                                                    faceLocationList.Add(faceLocation);
                                                }
                                            }

                                            if (additionalRotateAngles != null && additionalRotateAngles.Length > 0)
                                            {
                                                for (int r = 0; r < additionalRotateAngles.Length; r++)
                                                {
                                                    if (additionalRotateAngles[r] != 0)
                                                    {
                                                        DlibDotNet.Rectangle[] addFaces = null;
                                                        SKBitmap rotatedScanned         = Rotate(scanned, additionalRotateAngles[r]);
                                                        using (var array2D = Dlib.LoadImageData <RgbPixel>(ImagePixelFormat.Rgba, rotatedScanned.Bytes, (uint)rotatedScanned.Height, (uint)rotatedScanned.Width, (uint)(rotatedScanned.Bytes.Length / rotatedScanned.Height)))
                                                            addFaces = fd.Operator(array2D);

                                                        if (addFaces != null && addFaces.Length > 0)
                                                        {
                                                            for (int i = 0; i < addFaces.Length; i++)
                                                            {
                                                                #region обрезаем лицо до квадрата
                                                                Point center = addFaces[i].Center;
                                                                int   radius = 0;
                                                                if (addFaces[i].Width < addFaces[i].Height)
                                                                {
                                                                    radius = (int)addFaces[i].Width / 2;
                                                                }
                                                                else
                                                                {
                                                                    radius = (int)addFaces[i].Height / 2;
                                                                }
                                                                addFaces[i].Left   = center.X - radius;
                                                                addFaces[i].Right  = center.X + radius;
                                                                addFaces[i].Top    = center.Y - radius;
                                                                addFaces[i].Bottom = center.Y + radius;
                                                                #endregion обрезаем лицо до квадрата
                                                                FaceLocation faceLocation = CalculateRotatedFaceLocation((double)rotatedScanned.Width / 2, (double)rotatedScanned.Height / 2, addFaces[i], -additionalRotateAngles[r], normalized.Width, normalized.Height, scaleFactor, faceClippingRatio);
                                                                additionalFacesCounter++;
                                                                faceLocationList.Add(faceLocation);
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }

                                        if (faceLocationList.Count > 0)
                                        {
                                            List <FaceLocation> correlatedFaceList = GetCorrelatedFaceList(faceLocationList, additionalFacesCounter); // пропускаем через коррелятор лиц для избавления от дублей и уменьшения бокового наклона

                                            if (correlatedFaceList != null && correlatedFaceList.Count > 0)
                                            {
                                                for (int i = 0; i < correlatedFaceList.Count; i++)
                                                {
                                                    //var cropRect = new SKRectI { Left = correlatedFaceList[i].Left, Top = correlatedFaceList[i].Top, Right = correlatedFaceList[i].Right, Bottom = correlatedFaceList[i].Bottom };
                                                    var cropRect = new SKRectI();
                                                    int w        = correlatedFaceList[i].Right - correlatedFaceList[i].Left;
                                                    int h        = correlatedFaceList[i].Bottom - correlatedFaceList[i].Top;
                                                    int centerX  = correlatedFaceList[i].Left + w / 2;
                                                    int centerY  = correlatedFaceList[i].Top + h / 2;

                                                    if (w > h)
                                                    {
                                                        cropRect.Left   = centerX - h / 2;
                                                        cropRect.Right  = centerX + h / 2;
                                                        cropRect.Top    = centerY - h / 2;
                                                        cropRect.Bottom = centerY + h / 2;
                                                    }
                                                    else if (w < h)
                                                    {
                                                        cropRect.Left   = centerX - w / 2;
                                                        cropRect.Right  = centerX + w / 2;
                                                        cropRect.Top    = centerY - w / 2;
                                                        cropRect.Bottom = centerY + w / 2;
                                                    }
                                                    else
                                                    {
                                                        cropRect.Left   = correlatedFaceList[i].Left;
                                                        cropRect.Top    = correlatedFaceList[i].Top;
                                                        cropRect.Right  = correlatedFaceList[i].Right;
                                                        cropRect.Bottom = correlatedFaceList[i].Bottom;
                                                    }

                                                    var faceBitmap = new SKBitmap(cropRect.Width, cropRect.Height);
                                                    normalized.ExtractSubset(faceBitmap, cropRect);

                                                    //// ТЕПЕРЬ БУДЕМ ПОВОРАЧИВАТЬ
                                                    SKBitmap rotated = Rotate(faceBitmap, -correlatedFaceList[i].Angle);

                                                    // ТЕПЕРЬ НАКЛАДЫВАЕМ МАСКУ НА ЛИЦО В ВИДЕ КРУГА
                                                    double radius = 0;
                                                    if (cropRect.Width < cropRect.Height)
                                                    {
                                                        radius = (double)cropRect.Width / 2 * (1 + 0.5 / 2);
                                                    }
                                                    else
                                                    {
                                                        radius = (double)cropRect.Height / 2 * (1 + 0.5 / 2);
                                                    }

                                                    using (SKCanvas canvas = new SKCanvas(rotated))
                                                    {
                                                        canvas.DrawBitmap(rotated, 0, 0);
                                                        SKPaint paint = new SKPaint();
                                                        paint.Color       = maskColor;
                                                        paint.Style       = SKPaintStyle.Stroke;
                                                        paint.StrokeWidth = (float)(radius * 0.4);

                                                        canvas.DrawCircle((float)rotated.Width / 2, (float)rotated.Height / 2, (float)radius, paint);
                                                        canvas.Flush();
                                                    }

                                                    // ВЫРЕЗАЕМ ИТОГ
                                                    double x             = (double)rotated.Width / 2;
                                                    double y             = (double)rotated.Height / 2;
                                                    var    finalCropRect = new SKRectI {
                                                        Left = (int)(x - (double)faceBitmap.Width / 2), Top = (int)(y - (double)faceBitmap.Height / 2), Right = (int)(x + (double)faceBitmap.Width / 2), Bottom = (int)(y + (double)faceBitmap.Height / 2)
                                                    };
                                                    faceBitmap.Dispose();
                                                    using (SKBitmap face = new SKBitmap(finalCropRect.Width, finalCropRect.Height))
                                                    {
                                                        rotated.ExtractSubset(face, finalCropRect);
                                                        try
                                                        {
                                                            if (face.Width > 600 * scaleFactor)
                                                            {
                                                                using (var scaled = face.Resize(new SKImageInfo((int)Math.Round(400 * scaleFactor), (int)Math.Round(400 * scaleFactor)), SKFilterQuality.High))
                                                                {
                                                                    if (scaled != null)
                                                                    {
                                                                        using (var image = SKImage.FromBitmap(scaled))
                                                                            using (var data = image.Encode(SKEncodedImageFormat.Jpeg, 90))
                                                                            {
                                                                                if (facesByteArrays == null)
                                                                                {
                                                                                    facesByteArrays = new List <byte[]>();
                                                                                }
                                                                                facesByteArrays.Add(data.ToArray());
                                                                            }
                                                                    }
                                                                }
                                                            }
                                                            else
                                                            {
                                                                using (var image = SKImage.FromBitmap(face))
                                                                    using (var data = image.Encode(SKEncodedImageFormat.Jpeg, 90))
                                                                    {
                                                                        if (facesByteArrays == null)
                                                                        {
                                                                            facesByteArrays = new List <byte[]>();
                                                                        }
                                                                        facesByteArrays.Add(data.ToArray());
                                                                    }
                                                            }
                                                        }
                                                        catch (Exception exc) { };
                                                    }
                                                }
                                                normalized.Dispose();
                                                correlatedFaceList = null;
                                            }
                                            faceLocationList = null;
                                        }
                                    }
                                }
                        isSuccess = true;
                    }
                }
                catch (Exception exc)
                {
                    try
                    {
                        isSuccess = false;
                        if (exc.StackTrace != null && exc.StackTrace != "")
                        {
                            log.Debug(String.Format("SearchFaces. Exception: {0}", exc.StackTrace));
                        }
                        else if (exc.Message != null && exc.Message != "")
                        {
                            log.Debug(String.Format("SearchFaces. Exception: {0}", exc.Message));
                        }
                    }
                    catch (Exception ex) { }
                }
            }
            else
            {
                log.Debug("SearchFaces. Null request.");
            }
        }
        public async Task <HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
        {
            logger?.Info($"{nameof(ImageProxyService)}-{nameof(GetImageResponse)}-{url}");
            int type = 0;
            var i    = url.IndexOf(image_type_param_name, StringComparison.OrdinalIgnoreCase);

            if (i > 0)
            {
                try
                {
                    var p = url.Substring(i + image_type_param_name.Length).Trim('=').Trim();
                    url = url.Substring(0, i - 1);//减去一个连接符
                    int.TryParse(p, out type);
                }
                catch (Exception ex)
                {
                    logger?.Error(ex.ToString());
                }
            }

            try
            {
                var resp = await client.GetAsync(url, cancellationToken);

                if (resp.IsSuccessStatusCode == false)
                {
                    return(await Parse(resp));
                }

                if (type == 1)
                {
                    try
                    {
                        using (var inputStream = new SKManagedStream(await resp.Content.ReadAsStreamAsync()))
                        {
                            using (var bitmap = SKBitmap.Decode(inputStream))
                            {
                                var h  = bitmap.Height;
                                var w  = bitmap.Width;
                                var w2 = h * 2 / 3; //封面宽度

                                if (w2 < w)         //需要剪裁
                                {
                                    var x = await GetBaiduBodyAnalysisResult(resp);

                                    var start_w = w - w2;   //默认右边

                                    if (x > 0)              //百度人体识别,中心点位置
                                    {
                                        if (x + w2 / 2 > w) //右边
                                        {
                                            start_w = w - w2;
                                        }
                                        else if (x - w2 / 2 < 0)//左边
                                        {
                                            start_w = 0;
                                        }
                                        else //居中
                                        {
                                            start_w = (int)x - w2 / 2;
                                        }
                                    }

                                    var image = SKImage.FromBitmap(bitmap);

                                    var subset      = image.Subset(SKRectI.Create(start_w, 0, w2, h));
                                    var encodedData = subset.Encode(SKEncodedImageFormat.Png, 75);

                                    return(new HttpResponseInfo()
                                    {
                                        Content = encodedData.AsStream(),
                                        ContentLength = encodedData.Size,
                                        ContentType = "image/png",
                                        StatusCode = HttpStatusCode.OK,
                                    });
                                }
                            }
                        }
                    }
                    catch { }
                }

                return(await Parse(resp));
            }
            catch (Exception ex)
            {
                logger?.Error(ex.ToString());
            }
            return(new HttpResponseInfo());
        }
Ejemplo n.º 29
0
        // Rect

        public static Rectangle ToMauiRectangle(this SKRectI rect) =>
        new Rectangle(rect.Left, rect.Top, rect.Width, rect.Height);
Ejemplo n.º 30
0
        public SKData DrawHistogram(string referenceName, int zoom, int chunk)
        {
            var refer = _service._references.FirstOrDefault(r => r.Name == referenceName);

            if (chunk == 0 && zoom == 10)
            {
                LoadReference(referenceName);
            }

            using var surface     = SKSurface.Create(new SKImageInfo(refer.Length * zoom, 220));
            using SKCanvas canvas = surface.Canvas;

            canvas.Clear();

            using SKPaint paint = new SKPaint
                  {
                      Style       = SKPaintStyle.StrokeAndFill,
                      Color       = SKColors.Blue,
                      StrokeWidth = 1
                  };

            using SKPaint linePaint = new SKPaint
                  {
                      Style       = SKPaintStyle.Stroke,
                      Color       = SKColors.Black,
                      StrokeWidth = 1
                  };

            using SKPaint dashPaint = new SKPaint
                  {
                      Style       = SKPaintStyle.Stroke,
                      Color       = SKColors.Black,
                      StrokeWidth = 1,
                      PathEffect  = SKPathEffect.CreateDash(new[] { 20f, 5f }, 0)
                  };

            var histData = _service._histograms[refer.Name];

            var scale = (float)(100 / histData.Average());

            for (int i = 0; i < refer.Length; i++)
            {
                canvas.DrawRect(i * zoom + 5, 200 - (histData[i] * scale), zoom, histData[i] * scale, paint);
            }

            canvas.DrawLine(5, 0, 5, 200, linePaint);
            canvas.DrawLine(5, 199, refer.Length * zoom, 199, linePaint);

            var average = (int)(Math.Round(histData.Average()));

            canvas.DrawLine(5, average * scale, refer.Length * zoom, average * scale, dashPaint);

            using SKPaint textPaint = new SKPaint
                  {
                      Style        = SKPaintStyle.StrokeAndFill,
                      Color        = SKColors.Black,
                      StrokeWidth  = 1,
                      TextSize     = 20,
                      Typeface     = SKTypeface.FromFamilyName("Courier New"),
                      SubpixelText = true
                  };
            canvas.DrawText(average.ToString(), 6, average * scale - 2, textPaint);

            var cnt = textPaint.MeasureText("ACGT");

            if (zoom == 10)
            {
                textPaint.TextSize = textPaint.TextSize * 40 / cnt;
                var shaper = new SKShaper(SKTypeface.FromFamilyName("Courier New"));

                canvas.DrawShapedText(shaper, reference, 5, 215, textPaint);
            }

            var      width  = (chunk + 1) * WIDTH * zoom > refer.Length * zoom ? refer.Length * zoom % WIDTH : WIDTH;
            SKPixmap pixmap = surface.Snapshot().Subset(SKRectI.Create(chunk * WIDTH, 0, width, canvas.DeviceClipBounds.Height)).PeekPixels();

            var options = new SKWebpEncoderOptions(SKWebpEncoderCompression.Lossless, 100);

            return(pixmap.Encode(options));
        }