예제 #1
0
        LoadImageAsync(Windows.Storage.StorageFile file)
        {
            Windows.Storage.FileProperties.ImageProperties imgProp = await file.Properties.GetImagePropertiesAsync();

            using (var imgStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
            {
                var bitmap = new Windows.UI.Xaml.Media.Imaging.WriteableBitmap((int)imgProp.Width, (int)imgProp.Height);
                bitmap.SetSource(imgStream);
                return(bitmap);
            }
        }
예제 #2
0
        private async void CreateAndSaveVideo()
        {
            if (m_files != null && m_files.Count > 0)
            {
                if (_isDateNeeded)
                {
                    SortPhoto();
                }
                StorageFolder myVideoLibrary;
                //если папка существует открываем ,если нет создаем
                try
                {
                    myVideoLibrary = await KnownFolders.PicturesLibrary.GetFolderAsync("ImageConvertor");
                }
                catch
                {
                    myVideoLibrary = await KnownFolders.PicturesLibrary.CreateFolderAsync("ImageConvertor");
                }

                StorageFile videoFile = await myVideoLibrary.CreateFileAsync(_videoName + ".mp4", CreationCollisionOption.GenerateUniqueName);

                if (videoFile != null)
                {
                    PickPhotoBtn.IsEnabled = false;
                    Encode.IsEnabled       = false;
                    dateCheckBox.IsChecked = false;
                    durationList.IsEnabled = false;
                    nameBox.IsEnabled      = false;
                    BackBar.IsEnabled      = false;
                    progressRing.IsActive  = true;
                    titleBlock.Text        = "Конвертирование . . .";
                    try
                    {
                        using (IRandomAccessStream videoStream = await videoFile.OpenAsync(FileAccessMode.ReadWrite))
                        {
                            m_picture = new PictureWriter(videoStream, m_videoWidth, m_videoHeight);
                            foreach (StorageFile file in m_files)
                            {
                                Windows.Storage.FileProperties.ImageProperties properties = await file.Properties.GetImagePropertiesAsync();

                                var   longitude     = properties.Longitude;
                                float scaleOfWidth  = (float)m_videoWidth / properties.Width;
                                float scaleOfHeight = (float)m_videoHeight / properties.Height;
                                float scale         = scaleOfHeight > scaleOfWidth ?
                                                      scaleOfWidth : scaleOfHeight;
                                uint width  = (uint)(properties.Width * scale);
                                uint height = (uint)(properties.Height * scale);

                                using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
                                {
                                    for (int i = 0; i < 10 * _durationSec; ++i)
                                    {
                                        BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

                                        PixelDataProvider dataProvider = await decoder.GetPixelDataAsync(
                                            BitmapPixelFormat.Bgra8,
                                            BitmapAlphaMode.Straight,
                                            new BitmapTransform { ScaledWidth = width, ScaledHeight = height },
                                            ExifOrientationMode.RespectExifOrientation,
                                            ColorManagementMode.ColorManageToSRgb);

                                        m_picture.AddFrame(dataProvider.DetachPixelData(), (int)width, (int)height);
                                    }
                                }
                            }
                            m_picture.Finalize();
                            m_picture = null;
                        }
                    }
                    catch (Exception ex)
                    {
                        CreateAndSaveVideo();
                    }
                    progressRing.IsActive = false;

                    await CreateVideoModelAndWrite(videoFile); //сохраняем данные о файле в локальную папку

                    Frame.Navigate(typeof(MainPage));
                }
            }
        }
        /// <summary>
        /// Handle the returned file from file picker
        /// This method is triggered by ContinuationManager based on ActivationKind
        /// </summary>
        /// <param name="args">File save picker continuation activation argment. It cantains the file user selected with file save picker </param>
        public async void ContinueFileSavePicker(FileSavePickerContinuationEventArgs args)
        {
            StorageFile videoFile = args.File;

            if (videoFile != null)
            {
                IRandomAccessStream videoStream = await videoFile.OpenAsync(FileAccessMode.ReadWrite);

                m_picture = new PictureWriter(videoStream, m_videoWidth, m_videoHeight);

                // Add frames to the video.
                ProcessVideoRing.IsActive = true;
                VideoElement.AreTransportControlsEnabled = false;
                ImageBtn.IsEnabled  = false;
                EncodeBtn.IsEnabled = false;
                statusText.Text     = "Encoding...";

                foreach (StorageFile file in m_files)
                {
                    Windows.Storage.FileProperties.ImageProperties properties = await file.Properties.GetImagePropertiesAsync();

                    float scaleOfWidth  = (float)m_videoWidth / properties.Width;
                    float scaleOfHeight = (float)m_videoHeight / properties.Height;
                    float scale         = scaleOfHeight > scaleOfWidth ?
                                          scaleOfWidth : scaleOfHeight;
                    uint width  = (uint)(properties.Width * scale);
                    uint height = (uint)(properties.Height * scale);

                    using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
                    {
                        for (int i = 0; i < 10; ++i)
                        {
                            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

                            PixelDataProvider dataProvider = await decoder.GetPixelDataAsync(
                                BitmapPixelFormat.Bgra8,
                                BitmapAlphaMode.Straight,
                                new BitmapTransform { ScaledWidth = width, ScaledHeight = height },
                                ExifOrientationMode.RespectExifOrientation,
                                ColorManagementMode.ColorManageToSRgb);

                            m_picture.AddFrame(dataProvider.DetachPixelData(), (int)width, (int)height);
                        }
                    }
                }
                m_picture.Finalize();
                m_picture = null;

                VideoElement.AreTransportControlsEnabled = true;
                ImageBtn.IsEnabled        = true;
                EncodeBtn.IsEnabled       = true;
                statusText.Text           = "The image files are encoded successfully. You can review the video.";
                ProcessVideoRing.IsActive = false;

                videoStream.Dispose();
                videoStream = null;
                videoStream = await videoFile.OpenAsync(FileAccessMode.Read);

                VideoElement.SetSource(videoStream, videoFile.ContentType);
            }
        }
        private async void EncodeBtn_Click(object sender, RoutedEventArgs e)
        {
            if (m_files.Count == 0)
            {
                statusText.Text = "You must select one image at least.";
                return;
            }

            // Create the video file via file picker.
            FileSavePicker savePicker = new FileSavePicker();

            savePicker.SuggestedStartLocation = PickerLocationId.VideosLibrary;
            savePicker.FileTypeChoices.Add("MP4 File", new List <string>()
            {
                ".mp4"
            });
            savePicker.SuggestedFileName = "output";
            StorageFile videoFile = await savePicker.PickSaveFileAsync();

            if (videoFile != null)
            {
                IRandomAccessStream videoStream = await videoFile.OpenAsync(FileAccessMode.ReadWrite);

                m_picture = new PictureWriter(videoStream, m_videoWidth, m_videoHeight);

                // Add frames to the video.
                ProcessVideoRing.IsActive = true;
                statusText.Text           = "Encoding...";

                foreach (StorageFile file in m_files)
                {
                    Windows.Storage.FileProperties.ImageProperties properties = await file.Properties.GetImagePropertiesAsync();

                    float scaleOfWidth  = (float)m_videoWidth / properties.Width;
                    float scaleOfHeight = (float)m_videoHeight / properties.Height;
                    float scale         = scaleOfHeight > scaleOfWidth ?
                                          scaleOfWidth : scaleOfHeight;
                    uint width  = (uint)(properties.Width * scale);
                    uint height = (uint)(properties.Height * scale);

                    using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
                    {
                        for (int i = 0; i < 10; ++i)
                        {
                            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

                            PixelDataProvider dataProvider = await decoder.GetPixelDataAsync(
                                BitmapPixelFormat.Bgra8,
                                BitmapAlphaMode.Straight,
                                new BitmapTransform { ScaledWidth = width, ScaledHeight = height },
                                ExifOrientationMode.RespectExifOrientation,
                                ColorManagementMode.ColorManageToSRgb);

                            m_picture.AddFrame(dataProvider.DetachPixelData(), (int)width, (int)height);
                        }
                    }
                }
                m_picture.Finalize();
                m_picture = null;

                statusText.Text           = "The image files are encoded successfully. You can review the video.";
                ProcessVideoRing.IsActive = false;

                videoStream.Dispose();
                videoStream = null;
                videoStream = await videoFile.OpenAsync(FileAccessMode.Read);

                VideoElement.SetSource(videoStream, videoFile.ContentType);
            }
        }