Ejemplo n.º 1
0
        public async Task CreateNewCompoundImage(Guid mapId, IList <Guid> imageIds)
        {
            var nextImageId = await _mapsAnalyser.SelectNextImageId(mapId, imageIds);

            if (await CompoundImageAlreadyExists(mapId, imageIds))
            {
                return;
            }
            if (nextImageId == Guid.Empty)
            {
                return;
            }

            imageIds.Add(nextImageId);

            var compoundImage = new CompoundImage {
                MapId = mapId
            };
            await _compoundImagesRepository.InsertCompoundImage(compoundImage);

            foreach (var imageId in imageIds)
            {
                await _compoundImageMappingsRepository.InsertCompoundImageMapping(imageId, compoundImage.Id);
            }

            var queueCompoundImage = new KeyedCompoundImage
            {
                CompoundImageId = compoundImage.Id,
                ImageKey        = await GetCompoundKeyFromImageIds(imageIds),
                MapId           = mapId,
                Images          = await GetCompoundImagePartsFromIds(imageIds)
            };

            await _queueAdapter.SendMessage(queueCompoundImage, "compound-image");
        }
Ejemplo n.º 2
0
        public Bitmap StitchImages(KeyedCompoundImage keyedCompoundImage)
        {
            // Set width and height assumming that every image has equal dimensions.
            int        width         = 1000;
            int        height        = 1000;
            List <int> coordsX       = new List <int>();
            List <int> coordsY       = new List <int>();
            Bitmap     compoundImage = null; // Final, stitched image

            // Store coordinates of each image.
            foreach (var image in keyedCompoundImage.Images)
            {
                coordsX.Add(image.Coordinate.X);
                coordsY.Add(image.Coordinate.Y);
            }
            coordsX.Sort();
            coordsY.Sort();

            // Calculate canvas dimensions using the images count on each axis.
            width        *= coordsX[coordsX.Count - 1] - coordsX[0] + 1;
            height       *= coordsY[coordsY.Count - 1] - coordsY[0] + 1;
            compoundImage = new Bitmap(width, height); // Initialise canvas with the dimensions.

            // Get a graphics object from the image so we can draw on it.
            using (Graphics g = Graphics.FromImage(compoundImage))
            {
                g.Clear(Color.Transparent); // Set the background color.
                Bitmap bitmap;
                var    offsetX = 0;
                var    offsetY = 0;

                // Go through each image and draw it on the _compoundImage.
                foreach (var image in keyedCompoundImage.Images)
                {
                    bitmap  = CreateBitmapFromUrl(image.Image.Location);
                    offsetX = bitmap.Width * image.Coordinate.X - bitmap.Width;
                    offsetY = height - (bitmap.Height * image.Coordinate.Y - bitmap.Height) - bitmap.Height;

                    g.DrawImage(bitmap, new Rectangle(offsetX, offsetY, bitmap.Width, bitmap.Height));

                    bitmap.Dispose();
                }
            }

            Stream compoundImageStream = new MemoryStream();

            compoundImage.Save(compoundImageStream, ImageFormat.Jpeg);

            // If stitched image is greater than 4MB in size...
            if (compoundImageStream.Length > 400000)
            {
                // ...gradually decrease the image dimensions until it is less than 4MB in size (Azure Vision's limit).
                do
                {
                    compoundImageStream.Position = 0; // Rewind the memory stream.
                    compoundImage = ResizeImage(new Bitmap(compoundImageStream), Convert.ToInt32(compoundImage.Width * 0.9), Convert.ToInt32(compoundImage.Height * 0.9));
                    compoundImageStream.Position = 0;
                    compoundImage.Save(compoundImageStream, ImageFormat.Jpeg);
                }while (compoundImageStream.Position > 400000);

                return(compoundImage);
            }

            return(compoundImage);
        }