Exemple #1
0
        /// <summary>
        /// Creates a windowed image.
        /// </summary>
        /// <param name="keys">The substrokes that are being considered.</param>
        /// <param name="uncropped">The substrokes in the neighborhood of key (key is included in this list)</param>
        /// <param name="cropMethod">An enum that specifies the cropping metric.</param>
        public void CreateImage(List <Substroke> keys, List <Substroke> uncropped, CropMethod cropMethod)
        {
            // Creates a wrapper for the list of substrokes.
            keysSub = new Substrokes(keys);

            // Finds the maximum distance from the weighted center of the keys to the points on the substrokes of keys.
            float maxDist = float.NegativeInfinity;

            foreach (Substroke sub in keysSub.SubStrokes)
            {
                foreach (Point point in sub.PointsL)
                {
                    maxDist = Math.Max(maxDist, (float)(new PointDistance(keysSub.WeightedCenter, point).distance(PointDistance.EUCLIDIAN)));
                }
            }

            // Determines the width and height of keys
            this.keysWidth  = keysSub.MaxX - keysSub.MinX;
            this.keysHeight = keysSub.MaxY - keysSub.MinY;

            // Find the appropriate span given the size of the bounding box or the distance threshold
            if (cropMethod == CropMethod.RECTANGLE || cropMethod == CropMethod.SQUARE)
            {
                this.span = Math.Max(2 * Math.Max((float)(keysWidth / 2 + keysWidth * BB_THRESHOLD),
                                                  (float)(keysHeight / 2 + keysHeight * BB_THRESHOLD)) + 1.0f, 2 * maxDist + 1.0f);
            }
            else
            {
                this.span = Math.Max(Math.Max(keysWidth, keysHeight) + 2 * DISTANCE_THRESHOLD + 1.0f,
                                     2 * maxDist + 2 * DISTANCE_THRESHOLD + 1.0f);
            }

            this.cropped = this.CropSubstrokesDist(uncropped);

            //Trick it so it does not rotate
            //List<DefinitionImage> matches = new List<DefinitionImage>(0);
            //di = new DefinitionImage(this.iWidth, this.iHeight, this.cropped, matches);

            //this.AddPixels(cropped);
        }
Exemple #2
0
        public override int GetHashCode()
        {
            int hash = 1;

            if (MinCropSize != 0)
            {
                hash ^= MinCropSize.GetHashCode();
            }
            if (CropMethod != 0)
            {
                hash ^= CropMethod.GetHashCode();
            }
            if (MeanB != 0F)
            {
                hash ^= MeanB.GetHashCode();
            }
            if (MeanG != 0F)
            {
                hash ^= MeanG.GetHashCode();
            }
            if (MeanR != 0F)
            {
                hash ^= MeanR.GetHashCode();
            }
            if (IsBgr != false)
            {
                hash ^= IsBgr.GetHashCode();
            }
            if (CropScale != 0F)
            {
                hash ^= CropScale.GetHashCode();
            }
            if (InputBlobName.Length != 0)
            {
                hash ^= InputBlobName.GetHashCode();
            }
            if (ImParamBlobName.Length != 0)
            {
                hash ^= ImParamBlobName.GetHashCode();
            }
            if (OutputBlobName.Length != 0)
            {
                hash ^= OutputBlobName.GetHashCode();
            }
            if (ModelName.Length != 0)
            {
                hash ^= ModelName.GetHashCode();
            }
            if (ModelType.Length != 0)
            {
                hash ^= ModelType.GetHashCode();
            }
            if (ProtoFile.Length != 0)
            {
                hash ^= ProtoFile.GetHashCode();
            }
            if (WeightFile.Length != 0)
            {
                hash ^= WeightFile.GetHashCode();
            }
            if (MaxBatchSize != 0)
            {
                hash ^= MaxBatchSize.GetHashCode();
            }
            return(hash);
        }
Exemple #3
0
        public static async Task <string> SetBackgroundImageAsync(Posts posts, uint postShuffle, CropMethod method, Size screenSize, bool isLockscreen)
        {
            var folderName = isLockscreen ? LOCKSCREEN_FOLDER_NAME : WALLPAPER_FOLDER_NAME;

            Random rnd     = new Random();
            int    pointer = rnd.Next((int)postShuffle);

            var post = await GetPostAtAsync(posts, pointer);

            var previewBuffer = await(new Windows.Web.Http.HttpClient()).GetBufferAsync(new Uri(post.PreviewUrl));
            var jpegBuffer    = await(new Windows.Web.Http.HttpClient()).GetBufferAsync(new Uri(post.JpegUrl));


            var imageFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(folderName, CreationCollisionOption.OpenIfExists);

            var imageFile = await imageFolder.CreateFileAsync($"{post.Id}.jpg", CreationCollisionOption.ReplaceExisting);

            await FileIO.WriteBufferAsync(imageFile, jpegBuffer);

            var jpegFile = await imageFolder.CreateFileAsync($"{post.Id}-original.jpg", CreationCollisionOption.ReplaceExisting);

            await FileIO.WriteBufferAsync(jpegFile, jpegBuffer);



            // Crop the image
            var imageSize = new Size(post.JpegWidth, post.JpegHeight);

            await CropImageFile(imageFile, imageSize, method, screenSize, previewBuffer);

            if (UserProfilePersonalizationSettings.IsSupported())
            {
                if (isLockscreen)
                {
                    var b = await UserProfilePersonalizationSettings.Current.TrySetLockScreenImageAsync(imageFile);

                    if (b)
                    {
                        return(post.Id.ToString());
                    }
                }
                else
                {
                    var b = await UserProfilePersonalizationSettings.Current.TrySetWallpaperImageAsync(imageFile);

                    if (b)
                    {
                        return(post.Id.ToString());
                    }
                }
            }
            return("");
        }
Exemple #4
0
        private static async Task CropImageFile(StorageFile imageFile, Size imageSize, CropMethod method, Size screenSize, IBuffer previewBuffer)
        {
            switch (method)
            {
            case CropMethod.None:
                break;

            case CropMethod.Center:
            {
                var cropRect = CropBitmap.GetCenterCropRect(imageSize, screenSize);
                await CropBitmap.GetCroppedBitmapAsync(imageFile, cropRect, 1.0);
            }
            break;

            case CropMethod.TopMiddle:
            {
                var cropRect = CropBitmap.GetTopMiddleCropRect(imageSize, screenSize);
                await CropBitmap.GetCroppedBitmapAsync(imageFile, cropRect, 1.0);
            }
            break;

            case CropMethod.MostFaces:
            case CropMethod.BiggestFace:
                var result = await DetechFromBufferAsync(previewBuffer, 3, 25);

                var faces = ImageCropper.ScaleFaces(result.Faces, imageSize, result.FrameSize);
                if (faces.Count() > 0)
                {
                    if (method == CropMethod.BiggestFace)
                    {
                        faces = faces.OrderByDescending(o => o.Width).Take(1);
                    }
                    var cropRect = CropBitmap.GetGreedyCropRect(imageSize, screenSize, faces);
                    await CropBitmap.GetCroppedBitmapAsync(imageFile, cropRect, 1.0);
                }
                else
                {
                    // No face detected, fall back crop
                }
                break;

            default:
                break;
            }
        }
        public static async Task <string> SetBackgroundImageAsync(FilteredCollection <Post, Posts> posts, uint postShuffle, CropMethod method, Size screenSize, bool isLockscreen, int qualityLevel)
        {
            var folderName = isLockscreen ? LOCKSCREEN_FOLDER_NAME : WALLPAPER_FOLDER_NAME;

            Random rnd     = new Random();
            int    pointer = rnd.Next((int)postShuffle);

            // Load as many post as possible until reaching the pointer
            while ((pointer >= posts.Count) && (posts.HasMoreItems))
            {
                await posts.LoadMoreItemsAsync(1);
            }

            if (posts.Count == 0)
            {
                // No result at all, cannot update background
                return("");
            }

            if (posts.Count - 1 < pointer)
            {
                // Not enough posts to shuffle, shuffle for a lower amount of posts
                pointer = rnd.Next((int)posts.Count - 1);
            }

            // Select a post
            var post = posts[pointer];

            var previewBuffer  = await(new Windows.Web.Http.HttpClient()).GetBufferAsync(new Uri(post.PreviewUrl));
            var largeBufferUrl = "";
            int largeWidth     = 0;
            int largeHeight    = 0;

            switch (qualityLevel)
            {
            case 2:
                if (!String.IsNullOrEmpty(post.FileUrl))
                {
                    largeBufferUrl = post.FileUrl;
                    largeWidth     = post.Width;
                    largeHeight    = post.Height;
                }
                else
                {
                    largeBufferUrl = post.JpegUrl;
                    largeWidth     = post.JpegWidth;
                    largeHeight    = post.JpegHeight;
                }
                break;

            case 1:
                largeBufferUrl = post.JpegUrl;
                largeWidth     = post.JpegWidth;
                largeHeight    = post.JpegHeight;
                break;

            default:
                largeBufferUrl = post.SampleUrl;
                largeWidth     = post.SampleWidth;
                largeHeight    = post.SampleHeight;
                break;
            }

            // Try to delete all files of previous images
            var imageFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(folderName, CreationCollisionOption.OpenIfExists);

            var imageFiles = await imageFolder.GetFilesAsync();

            foreach (var oldImageFile in imageFiles)
            {
                try
                {
                    await oldImageFile.DeleteAsync(StorageDeleteOption.PermanentDelete);
                }
                catch (Exception ex)
                {
                }
            }


            // Downlaod the image and create a copy ready to crop
            var largeBuffer = await(new Windows.Web.Http.HttpClient()).GetBufferAsync(new Uri(largeBufferUrl));
            var imageFile   = await imageFolder.CreateFileAsync($"{post.Id}.jpg", CreationCollisionOption.ReplaceExisting);

            await FileIO.WriteBufferAsync(imageFile, largeBuffer);

            var jpegFile = await imageFolder.CreateFileAsync($"{post.Id}-original.jpg", CreationCollisionOption.ReplaceExisting);

            await FileIO.WriteBufferAsync(jpegFile, largeBuffer);


            // Crop the image
            var imageSize = new Size(largeWidth, largeHeight);

            await CropImageFile(imageFile, imageSize, method, screenSize, previewBuffer);


            var id = "";

            if (UserProfilePersonalizationSettings.IsSupported())
            {
                if (isLockscreen)
                {
                    var b = await UserProfilePersonalizationSettings.Current.TrySetLockScreenImageAsync(imageFile);

                    if (b)
                    {
                        using (var db = new AppDbContext())
                        {
                            db.LockScreenRecords.Add(LockScreenRecord.Create(post));
                            db.SaveChanges();
                        }
                        id = post.Id.ToString();
                    }
                }
                else
                {
                    var b = await UserProfilePersonalizationSettings.Current.TrySetWallpaperImageAsync(imageFile);

                    if (b)
                    {
                        using (var db = new AppDbContext())
                        {
                            db.WallpaperRecords.Add(WallpaperRecord.Create(post));
                            db.SaveChanges();
                        }
                        id = post.Id.ToString();
                    }
                }
            }


            // Return the latest ID if there is an update
            return(id);
        }
        public static async Task <string> SetBackgroundImageAsync(FilteredCollection <Post, Posts> posts, uint postShuffle, CropMethod method, Size screenSize, bool isLockscreen, int qualityLevel)
        {
            var folderName = isLockscreen ? LOCKSCREEN_FOLDER_NAME : WALLPAPER_FOLDER_NAME;

            Random rnd     = new Random();
            int    pointer = rnd.Next((int)postShuffle);

            var post = await GetPostAtAsync(posts, pointer);

            var previewBuffer  = await(new Windows.Web.Http.HttpClient()).GetBufferAsync(new Uri(post.PreviewUrl));
            var largeBufferUrl = "";
            int largeWidth     = 0;
            int largeHeight    = 0;

            switch (qualityLevel)
            {
            case 2:
                if (!String.IsNullOrEmpty(post.FileUrl))
                {
                    largeBufferUrl = post.FileUrl;
                    largeWidth     = post.Width;
                    largeHeight    = post.Height;
                }
                else
                {
                    largeBufferUrl = post.JpegUrl;
                    largeWidth     = post.JpegWidth;
                    largeHeight    = post.JpegHeight;
                }
                break;

            case 1:
                largeBufferUrl = post.JpegUrl;
                largeWidth     = post.JpegWidth;
                largeHeight    = post.JpegHeight;
                break;

            default:
                largeBufferUrl = post.SampleUrl;
                largeWidth     = post.SampleWidth;
                largeHeight    = post.SampleHeight;
                break;
            }
            var largeBuffer = await(new Windows.Web.Http.HttpClient()).GetBufferAsync(new Uri(largeBufferUrl));


            var imageFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync(folderName, CreationCollisionOption.OpenIfExists);

            var imageFile = await imageFolder.CreateFileAsync($"{post.Id}.jpg", CreationCollisionOption.ReplaceExisting);

            await FileIO.WriteBufferAsync(imageFile, largeBuffer);

            var jpegFile = await imageFolder.CreateFileAsync($"{post.Id}-original.jpg", CreationCollisionOption.ReplaceExisting);

            await FileIO.WriteBufferAsync(jpegFile, largeBuffer);



            // Crop the image
            var imageSize = new Size(largeWidth, largeHeight);

            await CropImageFile(imageFile, imageSize, method, screenSize, previewBuffer);

            if (UserProfilePersonalizationSettings.IsSupported())
            {
                if (isLockscreen)
                {
                    var b = await UserProfilePersonalizationSettings.Current.TrySetLockScreenImageAsync(imageFile);

                    if (b)
                    {
                        return(post.Id.ToString());
                    }
                }
                else
                {
                    var b = await UserProfilePersonalizationSettings.Current.TrySetWallpaperImageAsync(imageFile);

                    if (b)
                    {
                        return(post.Id.ToString());
                    }
                }
            }
            return("");
        }