示例#1
1
        /// <summary>
        /// Save the primitive image and thumbnail image and return the Photo model with local url
        /// </summary>
        /// <param name="imageBuffer"></param>
        /// <returns></returns>
        public static async Task<Photo> SaveImage(IBuffer imageBuffer)
        {
            try
            {
                Photo photo = new Photo
                {
                    CreatedTime = DateTime.UtcNow
                };

                AutoResizeConfiguration tbRC = null;
                AutoResizeConfiguration pvRC = null;
                Rect thumbnailRect;
                Size pvSize;
                
                using (var source = new BufferImageSource(imageBuffer))
                {
                    var info = await source.GetInfoAsync();
                    //var thumbnailSize = ImageHelper.CalculateSize(info.ImageSize);
                    
                    thumbnailRect = CalculateRect(info.ImageSize);
                    pvSize = CalculateSize(info.ImageSize);
                    tbRC = new AutoResizeConfiguration(ImageHelper.thumbnailMaxBytes, thumbnailMaxSize, new Size(0, 0), AutoResizeMode.Automatic, 0, ColorSpace.Yuv420);
                    pvRC = new AutoResizeConfiguration(ImageHelper.pvMaxBytes, pvSize, new Size(0, 0), AutoResizeMode.Automatic, 0, ColorSpace.Yuv420);
                }

                using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    //Get LocalPath
                    var localPath = AppResources.LocalImagePath;

                    if (!store.DirectoryExists(localPath))
                    {
                        store.CreateDirectory(localPath);
                    }

                    //Save the primitive bitmap file
                    using (var file = store.CreateFile(photo.LocalUri = localPath + @"\" + photo.Tag + @"_Raw"))
                    {
                        using (var localImage = imageBuffer.AsStream())
                        {
                            localImage.CopyTo(file);
                            file.Flush();
                            localImage.Close();
                        }
                    }

                    //Save Preview Image
                    if (pvRC != null)
                    {
                        //Resize the Image to priview image

                        var pvBuffer = await Nokia.Graphics.Imaging.JpegTools.AutoResizeAsync(imageBuffer, pvRC);

                        //Save Preview Image
                        using (var file = store.CreateFile(localPath + @"\" + photo.Tag + @"_Preview.jpg"))
                        {
                            photo.PreviewDetailUri = file.Name;
                            ;
                            using (var localImage = pvBuffer.AsStream())
                            {
                                localImage.CopyTo(file);
                                file.Flush();
                                localImage.Close();
                            }
                        }
                    }

                    //Save thumbnail Image
                    if(tbRC != null)
                    {
                        //Crop to the square
                        var rb = await Reframe(imageBuffer, thumbnailRect);

                        //Resize the Image to thumbnail
                        var tb = await Nokia.Graphics.Imaging.JpegTools.AutoResizeAsync(rb, tbRC);

                        //Save thumbnail
                        using (var file = store.CreateFile(photo.LocalThumbnailUri = localPath + @"\" + photo.Tag + @"_Thumbnail.jpg"))
                        {
                            photo.ThumbnailDetailUri = file.Name;

                            using (var localImage = tb.AsStream())
                            {
                                localImage.CopyTo(file);
                                file.Flush();
                                localImage.Close();
                            }

                        }
                    }
                }

            return photo;

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
示例#2
0
 public async Task <Windows.Foundation.Size> GetImageSizeAsync()
 {
     using (var source = new BufferImageSource(_buffer))
     {
         return((await source.GetInfoAsync()).ImageSize);
     }
 }
示例#3
0
        private async Task <string> SaveToCameraRoll(Stream cameraImage, string filenameBase)
        {
            string                  filename            = filenameBase + ".jpg";
            StorageFolder           storageFolder       = KnownFolders.CameraRoll;
            AutoResizeConfiguration resizeConfiguration = null;
            var buffer = StreamToBuffer(cameraImage);

            // Store low resolution image
            using (var source = new BufferImageSource(buffer))
            {
                var info = await source.GetInfoAsync();

                if (info.ImageSize.Width * info.ImageSize.Height > LibraryMaxArea)
                {
                    var compactedSize = CalculateSize(info.ImageSize, LibraryMaxSize, LibraryMaxArea);
                    resizeConfiguration = new AutoResizeConfiguration(LibraryMaxBytes, compactedSize,
                                                                      new Size(0, 0), AutoResizeMode.Automatic, 0, ColorSpace.Yuv420);
                    buffer = await Nokia.Graphics.Imaging.JpegTools.AutoResizeAsync(buffer, resizeConfiguration);
                }

                using (var library = new MediaLibrary())
                {
                    library.SavePictureToCameraRoll(filename, buffer.AsStream());
                }
            }

            // Store high resolution image
            if (resizeConfiguration != null)
            {
                filename             = filenameBase + HIGH_RESOLUTION_PHOTO_SUFFIX + @".jpg";
                cameraImage.Position = 0;

                using (var stream = await storageFolder.OpenStreamForWriteAsync(filename, CreationCollisionOption.GenerateUniqueName))
                {
                    await cameraImage.CopyToAsync(stream);
                }
            }
            _saved = true;

            return(storageFolder.Path + "\\" + filename);
        }
示例#4
0
        /// <summary>
        /// Asynchronously saves a low resolution version of the photo to MediaLibrary and if the photo is too large to be saved
        /// to MediaLibrary as is also saves the original high resolution photo to application's local storage so that the
        /// high resolution version is not lost.
        /// </summary>
        public async Task SaveAsync()
        {
            if (_image != null && _image.Length > 0 && LibraryPath == null)
            {
                var buffer = StreamToBuffer(_image);

                AutoResizeConfiguration resizeConfiguration = null;

                using (var source = new BufferImageSource(buffer))
                {
                    var info = await source.GetInfoAsync();

                    if (info.ImageSize.Width * info.ImageSize.Height > LibraryMaxArea)
                    {
                        var compactedSize = CalculateSize(info.ImageSize, LibraryMaxSize, LibraryMaxArea);

                        resizeConfiguration = new AutoResizeConfiguration(LibraryMaxBytes, compactedSize,
                            new Size(0, 0), AutoResizeMode.Automatic, 0, ColorSpace.Yuv420);
                    }
                }

                var filenameBase = "photoinspector";

                if (_original != null)
                {
                    if (_originalPath != null)
                    {
                        var originalPathParts = _originalPath.Split(new char[] { '_', '.' }, StringSplitOptions.RemoveEmptyEntries);

                        filenameBase += '_' + originalPathParts[1];
                    }
                    else
                    {
                        filenameBase += '_' + DateTime.UtcNow.Ticks.ToString();

                        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                        {
                            if (!store.DirectoryExists(Mapping.ORIGINALS_PATH))
                            {
                                store.CreateDirectory(Mapping.ORIGINALS_PATH);
                            }

                            var originalPath = Mapping.ORIGINALS_PATH + @"\" + filenameBase + @".jpg";

                            using (var file = store.CreateFile(originalPath))
                            {
                                _original.Position = 0;
                                _original.CopyTo(file);

                                file.Flush();

                                OriginalPath = originalPath;
                            }
                        }
                    }
                }

                filenameBase += '_' + DateTime.UtcNow.Ticks.ToString();

                if (resizeConfiguration != null)
                {
                    // Store high resolution original to application local storage

                    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (!store.DirectoryExists(Mapping.LOCALS_PATH))
                        {
                            store.CreateDirectory(Mapping.LOCALS_PATH);
                        }

                        var localPath = Mapping.LOCALS_PATH + @"\" + filenameBase + @".jpg";

                        using (var file = store.CreateFile(localPath))
                        {
                            _image.Position = 0;
                            _image.CopyTo(file);

                            file.Flush();

                            LocalPath = localPath;
                        }
                    }

                    // Compact the buffer for saving to the library

                    buffer = await Nokia.Graphics.Imaging.JpegTools.AutoResizeAsync(buffer, resizeConfiguration);
                }

                using (var libraryImage = buffer.AsStream())
                {
                    libraryImage.Position = 0;

                    using (var library = new MediaLibrary())
                    {
                        using (var picture = library.SavePictureToCameraRoll(filenameBase, libraryImage))
                        {
                            LibraryPath = picture.GetPath();
                        }
                    }
                }
            }
        }
示例#5
0
        /// <summary>
        /// Asynchronously saves a low resolution version of the photo to MediaLibrary and if the photo is too large to be saved
        /// to MediaLibrary as is also saves the original high resolution photo to application's local storage so that the
        /// high resolution version is not lost.
        /// </summary>
        public async Task SaveAsync()
        {
            if (_image != null && _image.Length > 0 && LibraryPath == null)
            {
                var buffer = StreamToBuffer(_image);

                AutoResizeConfiguration resizeConfiguration = null;

                using (var source = new BufferImageSource(buffer))
                {
                    var info = await source.GetInfoAsync();

                    if (info.ImageSize.Width * info.ImageSize.Height > LibraryMaxArea)
                    {
                        var compactedSize = CalculateSize(info.ImageSize, LibraryMaxSize, LibraryMaxArea);

                        resizeConfiguration = new AutoResizeConfiguration(LibraryMaxBytes, compactedSize,
                                                                          new Size(0, 0), AutoResizeMode.Automatic, 0, ColorSpace.Yuv420);
                    }
                }

                var filenameBase = "photoinspector";

                if (_original != null)
                {
                    if (_originalPath != null)
                    {
                        var originalPathParts = _originalPath.Split(new char[] { '_', '.' }, StringSplitOptions.RemoveEmptyEntries);

                        filenameBase += '_' + originalPathParts[1];
                    }
                    else
                    {
                        filenameBase += '_' + DateTime.UtcNow.Ticks.ToString();

                        using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                        {
                            if (!store.DirectoryExists(Mapping.ORIGINALS_PATH))
                            {
                                store.CreateDirectory(Mapping.ORIGINALS_PATH);
                            }

                            var originalPath = Mapping.ORIGINALS_PATH + @"\" + filenameBase + @".jpg";

                            using (var file = store.CreateFile(originalPath))
                            {
                                _original.Position = 0;
                                _original.CopyTo(file);

                                file.Flush();

                                OriginalPath = originalPath;
                            }
                        }
                    }
                }

                filenameBase += '_' + DateTime.UtcNow.Ticks.ToString();

                if (resizeConfiguration != null)
                {
                    // Store high resolution original to application local storage

                    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        if (!store.DirectoryExists(Mapping.LOCALS_PATH))
                        {
                            store.CreateDirectory(Mapping.LOCALS_PATH);
                        }

                        var localPath = Mapping.LOCALS_PATH + @"\" + filenameBase + @".jpg";

                        using (var file = store.CreateFile(localPath))
                        {
                            _image.Position = 0;
                            _image.CopyTo(file);

                            file.Flush();

                            LocalPath = localPath;
                        }
                    }

                    // Compact the buffer for saving to the library

                    buffer = await Nokia.Graphics.Imaging.JpegTools.AutoResizeAsync(buffer, resizeConfiguration);
                }

                using (var libraryImage = buffer.AsStream())
                {
                    libraryImage.Position = 0;

                    using (var library = new MediaLibrary())
                    {
                        using (var picture = library.SavePictureToCameraRoll(filenameBase, libraryImage))
                        {
                            LibraryPath = picture.GetPath();
                        }
                    }
                }
            }
        }
示例#6
0
        private async Task<string> SaveToCameraRoll(Stream cameraImage, string filenameBase)
        {
            string filename = filenameBase + ".jpg";
            StorageFolder storageFolder = KnownFolders.CameraRoll;
            AutoResizeConfiguration resizeConfiguration = null;
            var buffer = StreamToBuffer(cameraImage);

            // Store low resolution image
            using (var source = new BufferImageSource(buffer))
            {
                var info = await source.GetInfoAsync();

                if (info.ImageSize.Width * info.ImageSize.Height > LibraryMaxArea)
                {
                    var compactedSize = CalculateSize(info.ImageSize, LibraryMaxSize, LibraryMaxArea);
                    resizeConfiguration = new AutoResizeConfiguration(LibraryMaxBytes, compactedSize,
                        new Size(0, 0), AutoResizeMode.Automatic, 0, ColorSpace.Yuv420);
                    buffer = await Nokia.Graphics.Imaging.JpegTools.AutoResizeAsync(buffer, resizeConfiguration);
                }

                using (var library = new MediaLibrary())
                {
                    library.SavePictureToCameraRoll(filename, buffer.AsStream());
                }
            }

            // Store high resolution image
            if (resizeConfiguration != null)
            {
                filename = filenameBase + HIGH_RESOLUTION_PHOTO_SUFFIX + @".jpg";
                cameraImage.Position = 0;

                using (var stream = await storageFolder.OpenStreamForWriteAsync(filename, CreationCollisionOption.GenerateUniqueName))
                {
                    await cameraImage.CopyToAsync(stream);
                }
            }
            _saved = true;

            return storageFolder.Path + "\\" + filename;
        }
        async void setInput(Stream s)
        {

            //Dispose old session ressources.
            if (source != null)
            {
                source.Dispose(); source = null;
                filter.Dispose(); filter = null;
                reframing = null;
                rendererLR.Dispose(); rendererLR = null;
                rendererHR.Dispose(); rendererHR = null;
            }
            //reset stream position
            s.Seek(0, SeekOrigin.Begin);

            MemoryStream mstream = new MemoryStream();
            s.CopyTo(mstream);
            var tmpsource = new BufferImageSource(mstream.GetBuffer().AsBuffer());
           // var tmpsource = new StreamImageSource(s);
            {

                var info = await tmpsource.GetInfoAsync();

                filter = new FilterEffect(tmpsource);

                reframing = new ReframingFilter();
                filter.Filters = new IFilter[] { reframing };
                rendererLR = new WriteableBitmapRenderer(filter, outputBitmapLRTmp);
                rendererHR = new WriteableBitmapRenderer(filter, outputBitmapHRTmp);

                inputSize = new Size() { Width = info.ImageSize.Width, Height = info.ImageSize.Height };
                currentPos = new Point(info.ImageSize.Width / 2, info.ImageSize.Height / 2);
                if (info.ImageSize.Width > info.ImageSize.Height)
                {
                    currentScale = output.Height / info.ImageSize.Height;
                }
                else
                {
                    currentScale = output.Width / info.ImageSize.Width;
                }
                currentAngle = 0.0;
                saveLastPossaveLastPositionData();
                GC.Collect();
            }
            source = tmpsource;
            requestProcessing();
        }
示例#8
0
 public async Task<Windows.Foundation.Size> GetImageSizeAsync()
 {
     using (BufferImageSource source = new BufferImageSource(_buffer))
     {
         return (await source.GetInfoAsync()).ImageSize;
     }
 }