コード例 #1
0
        private void DrawFrame(PredictionModel prediction, Canvas overlayCanvas)
        {
            _overlayCanvasActualWidth  = (uint)CameraPreview.ActualWidth;
            _overlayCanvasActualHeight = (uint)CameraPreview.ActualHeight;

            var x = (uint)Math.Max(prediction.Box.Left, 0);
            var y = (uint)Math.Max(prediction.Box.Top, 0);
            var w = (uint)Math.Min(_overlayCanvasActualWidth - x, prediction.Box.Width);
            var h = (uint)Math.Min(_overlayCanvasActualHeight - y, prediction.Box.Height);

            Debug.WriteLine($"\tOverLay Canvas \tActual Width: {_overlayCanvasActualWidth}, Actual Height: {_overlayCanvasActualHeight}");
            Debug.WriteLine(prediction.GetExtendedDescription());
            Debug.WriteLine($"\tOriginal\tY: {y}, x: {x}, Height: {h}, Width: {w}");

            // fit to current size
            var factorSize = 100u;

            x = _overlayCanvasActualWidth * x / factorSize;
            y = _overlayCanvasActualHeight * y / factorSize;
            w = _overlayCanvasActualWidth * w / factorSize;
            h = _overlayCanvasActualHeight * h / factorSize;

            Debug.WriteLine($"\tScaled\tY: {y}, x: {x}, Height: {h}, Width: {w}");

            var rectStroke = _lineBrushGreen;

            switch (prediction.TagName)
            {
            case "arch":
                rectStroke = _lineBrushGray;
                break;

            case "triangle":
                rectStroke = _lineBrushGreen;
                break;

            case "square":
                rectStroke = _lineBrushGreen;
                break;
            }

            var r = new Windows.UI.Xaml.Shapes.Rectangle
            {
                Tag             = prediction,
                Width           = w,
                Height          = h,
                Fill            = _fillBrush,
                Stroke          = rectStroke,
                StrokeThickness = _lineThickness,
                Margin          = new Thickness(x, y, 0, 0)
            };

            var tb = new TextBlock
            {
                Margin     = new Thickness(x + 4, y + 4, 0, 0),
                Text       = $"{prediction}",
                FontWeight = FontWeights.Bold,
                Width      = 126,
                Height     = 21,
                HorizontalTextAlignment = TextAlignment.Center
            };

            var textBack = new Windows.UI.Xaml.Shapes.Rectangle
            {
                Width  = 150,
                Height = 29,
                Fill   = rectStroke,
                Margin = new Thickness(x, y, 0, 0)
            };

            overlayCanvas.Children.Add(textBack);
            overlayCanvas.Children.Add(tb);
            overlayCanvas.Children.Add(r);
        }