コード例 #1
0
ファイル: MainPage.xaml.cs プロジェクト: vadimas/events
 /// <summary>
 /// Render ObjectDetector skill results
 /// </summary>
 /// <param name="frame"></param>
 /// <param name="objectDetections"></param>
 /// <returns></returns>
 private async Task RenderResultsAsync(VideoFrame frame, IReadOnlyList <ObjectDetectorResult> objectDetections)
 {
     await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
     {
         try
         {
             if (frame.SoftwareBitmap != null)
             {
                 await m_processedBitmapSource.SetBitmapAsync(frame.SoftwareBitmap);
             }
             else
             {
                 var bitmap = await SoftwareBitmap.CreateCopyFromSurfaceAsync(frame.Direct3DSurface, BitmapAlphaMode.Ignore);
                 await m_processedBitmapSource.SetBitmapAsync(bitmap);
             }
             m_bboxRenderer.Render(objectDetections);
         }
         catch (TaskCanceledException)
         {
             // no-op: we expect this exception when we change media sources
             // and can safely ignore/continue
         }
         catch (Exception ex)
         {
             NotifyUser($"Exception while rendering results: {ex.Message}");
         }
     });
 }
コード例 #2
0
        private async Task SetImageAsync(StorageFile file)
        {
            ShowProgressBar("FlyoutLoading");
            using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
            {
                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

                SelectedBitmap = await decoder.GetSoftwareBitmapAsync();
            }

            if (SelectedBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 || SelectedBitmap.BitmapAlphaMode != BitmapAlphaMode.Premultiplied)
            {
                SelectedBitmap = SoftwareBitmap.Convert(SelectedBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
            }
            // Output is a copy of Selected and the source is the same for the two views
            var bitmapSource = new SoftwareBitmapSource();

            await bitmapSource.SetBitmapAsync(SelectedBitmap);

            OutputBitmap         = SoftwareBitmap.Copy(SelectedBitmap);
            EditingImageFile     = file;
            SelectedImage.Source = bitmapSource;

            await bitmapSource.SetBitmapAsync(OutputBitmap);

            ResultImage.Source           = bitmapSource;
            SaveButton.IsEnabled         = true;
            ResetButton.IsEnabled        = false;
            EffectActionsGrid.Visibility = Visibility.Visible;
            HideProgressBar();
        }
コード例 #3
0
        /// <summary>
        /// Run the skill against the frame passed as parameter
        /// </summary>
        /// <param name="frame"></param>
        /// <returns></returns>
        private async Task RunSkillAsync(VideoFrame frame)
        {
            // Update input image and run the skill against it
            await m_paramsSkillObj.m_binding.SetInputImageAsync(frame);

            // Evaluate skill
            await m_paramsSkillObj.m_skill.EvaluateAsync(m_paramsSkillObj.m_binding);

            VideoFrame superResOutput = null;

            await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
            {
                // Retrieve result and display
                superResOutput = await m_paramsSkillObj.m_binding.GetOutputImageAsync();
                if (superResOutput.SoftwareBitmap == null)
                {
                    SoftwareBitmap softwareBitmapOut = await SoftwareBitmap.CreateCopyFromSurfaceAsync(superResOutput.Direct3DSurface);
                    softwareBitmapOut = SoftwareBitmap.Convert(softwareBitmapOut, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);

                    await m_bitmapSource.SetBitmapAsync(softwareBitmapOut);
                    UIOutputViewer.Source = m_bitmapSource;
                }
                else
                {
                    await m_bitmapSource.SetBitmapAsync(superResOutput.SoftwareBitmap);
                    UIOutputViewer.Source = m_bitmapSource;
                }
            });
        }
コード例 #4
0
        /// <summary>
        /// Triggered when a new frame is available from the camera stream.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void FrameSource_FrameArrived(object sender, VideoFrame frame)
        {
            try
            {
                // Use a lock to process frames one at a time and bypass processing if busy
                if (m_lock.Wait(0))
                {
                    // Render incoming frame
                    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() =>
                    {
                        await m_renderLock.WaitAsync();
                        if (frame.SoftwareBitmap != null)
                        {
                            await m_bitmapSource.SetBitmapAsync(frame.SoftwareBitmap);
                        }
                        else
                        {
                            var bitmap = await SoftwareBitmap.CreateCopyFromSurfaceAsync(frame.Direct3DSurface, BitmapAlphaMode.Ignore);
                            await m_bitmapSource.SetBitmapAsync(bitmap);
                        }
                        m_renderLock.Release();
                    });

                    // Allign overlay canvas and camera preview so that the rendered rectangle looks right
                    if (!m_isCameraFrameDimensionInitialized || m_frameSource.FrameWidth != m_cameraFrameWidth || m_frameSource.FrameHeight != m_cameraFrameHeight)
                    {
                        m_cameraFrameWidth  = m_frameSource.FrameWidth;
                        m_cameraFrameHeight = m_frameSource.FrameHeight;

                        await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
                        {
                            UICameraPreview_SizeChanged(null, null);
                        });

                        m_isCameraFrameDimensionInitialized = true;
                    }

                    // Run the skill
                    await RunSkillAsync(frame);

                    m_lock.Release();
                }
            }
            catch (Exception ex)
            {
                // Show the error
                await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => NotifyUser(ex.Message, NotifyType.ErrorMessage));

                m_lock.Release();
            }
        }
コード例 #5
0
        private async static Task <SoftwareBitmapSource> ConvertSoftwareBitmap(SoftwareBitmap sb)
        {
            var sbs = new SoftwareBitmapSource();
            await sbs.SetBitmapAsync(sb);

            return(sbs);
        }
コード例 #6
0
        public async Task SwapActiveImageAsync()
        {
            if (SwappingActiveImage)
            {
                return;
            }

            SwappingActiveImage = true;

            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => {
                SoftwareBitmap latestBitmap;

                // Keep draining frames from the backbuffer until the backbuffer is empty.
                while ((latestBitmap = Interlocked.Exchange(ref BackBuffer, null)) != null)
                {
                    try {
                        await ImageSource.SetBitmapAsync(latestBitmap);
                    }
                    catch (TaskCanceledException) { }

                    latestBitmap.Dispose();
                }
            });

            SwappingActiveImage = false;
        }
コード例 #7
0
        private async void button_Click(object sender, RoutedEventArgs e)
        {
            Saved.Visibility = Visibility.Collapsed;

            /*
             *          if (Tops == null)
             *              Tops = await folder.CreateFolderAsync("Tops", CreationCollisionOption.OpenIfExists);
             *
             *          if (Bottoms == null)
             *              Bottoms = await folder.CreateFolderAsync("Bottoms", CreationCollisionOption.OpenIfExists);
             */

            CameraCaptureUI camera = new CameraCaptureUI();

            camera.PhotoSettings.CroppedAspectRatio = new Size(200, 200);
            photo = await camera.
                    CaptureFileAsync(CameraCaptureUIMode.Photo);


            //######
            IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);

            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

            SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();

            SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap,
                                                                       BitmapPixelFormat.Bgra8,
                                                                       BitmapAlphaMode.Premultiplied);

            SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
            await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);

            image.Source = bitmapSource;
        }
コード例 #8
0
        private void ProcessImage()
        {
            var task = localImgRenderer.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
            {
                if (taskRunning)
                {
                    return;
                }
                taskRunning = true;

                SoftwareBitmap latestBitmap;
                while ((latestBitmap = Interlocked.Exchange(ref backBuffer, null)) != null)
                {
                    SoftwareBitmapSource src = new SoftwareBitmapSource();
                    await src.SetBitmapAsync(latestBitmap);
                    localImgRenderer.Source = src;

                    Client.bmpToSend.Enqueue(src);

                    latestBitmap.Dispose();
                }

                taskRunning = false;
            });
        }
コード例 #9
0
        private static async Task <SoftwareBitmapSource> CropPhoto(StorageFile photo, FaceRectangle rectangle)
        {
            using (var imageStream = await photo.OpenReadAsync())
            {
                var decoder = await BitmapDecoder.CreateAsync(imageStream);

                if (decoder.PixelWidth >= rectangle.Left + rectangle.Width || decoder.PixelHeight >= rectangle.Top + rectangle.Height)
                {
                    var transform = new BitmapTransform
                    {
                        Bounds = new BitmapBounds
                        {
                            X      = (uint)rectangle.Left,
                            Y      = (uint)rectangle.Top,
                            Height = (uint)rectangle.Height,
                            Width  = (uint)rectangle.Width
                        }
                    };

                    var softwareBitmapBGR8 = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage);

                    SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
                    await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);

                    return(bitmapSource);
                }
                return(null);
            }
        }
コード例 #10
0
        //BitmapProcess比特图处理(异步)
        public static async Task <SoftwareBitmapSource> BitmapProcess(StorageFile inputFile)
        {
            var            source = new SoftwareBitmapSource();
            SoftwareBitmap softwareBitmap;

            using (IRandomAccessStream stream = await inputFile.OpenAsync(FileAccessMode.Read))
            {
                // Create the decoder from the stream
                var decoder = await BitmapDecoder.CreateAsync(stream);

                // Get the SoftwareBitmap representation of the file
                softwareBitmap = await decoder.GetSoftwareBitmapAsync();

                decoder = null;
            }
            if (softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 ||
                softwareBitmap.BitmapAlphaMode == BitmapAlphaMode.Straight)
            {
                softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
            }

            await source.SetBitmapAsync(softwareBitmap);

            // softwareBitmap.Dispose();//如果有机器慢的话就删除这句
            return(source);
        }
コード例 #11
0
        // A method to convert and bind the input image.
        private async Task imageBind()
        {
            UIPreviewImage.Source = null;

            try
            {
                SoftwareBitmap softwareBitmap;
                using (IRandomAccessStream stream = await selectedStorageFile.OpenAsync(FileAccessMode.Read))
                {
                    // Create the decoder from the stream
                    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

                    // Get the SoftwareBitmap representation of the file in BGRA8 format
                    softwareBitmap = await decoder.GetSoftwareBitmapAsync();

                    softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
                }
                // Display the image
                SoftwareBitmapSource imageSource = new SoftwareBitmapSource();
                await imageSource.SetBitmapAsync(softwareBitmap);

                UIPreviewImage.Source = imageSource;

                // Encapsulate the image within a VideoFrame to be bound and evaluated
                VideoFrame inputImage = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap);

                // bind the input image
                ImageFeatureValue imageTensor = ImageFeatureValue.CreateFromVideoFrame(inputImage);
                input.data = imageTensor;
            }
            catch (Exception e)
            {
            }
        }
コード例 #12
0
        private async void Launch_Camera(object sender, RoutedEventArgs e)
        {
            CameraCaptureUI captureUI = new CameraCaptureUI();

            captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
            captureUI.PhotoSettings.CroppedSizeInPixels = new Size(200, 200);

            StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);

            if (photo == null)
            {
                // User cancelled photo capture
                return;
            }

            IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);

            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

            SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();

            SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap,
                                                                       BitmapPixelFormat.Bgra8,
                                                                       BitmapAlphaMode.Premultiplied);

            SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
            await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);

            Img.Source = bitmapSource;
        }
コード例 #13
0
        public async void OtvoriKameru(Object slika)
        {
            CameraCaptureUI captureUI = new CameraCaptureUI();

            captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
            captureUI.PhotoSettings.CroppedSizeInPixels = new Size(200, 200);
            StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);

            if (photo == null)
            {
                return;
            }

            IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);

            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

            SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();

            SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap,
                                                                       BitmapPixelFormat.Bgra8,
                                                                       BitmapAlphaMode.Premultiplied);

            SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
            await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);

            ((Image)slika).Source = bitmapSource;



            /* await(new MessageDialog("Vanjski uredjaj jos nije implementiran!")).ShowAsync();*/
            // Pokrece se web kamera da bi se korisnik uslikao
            // Vanjski uredjaj jos nije implementiran
        }
コード例 #14
0
ファイル: CameraAPI.cs プロジェクト: jeremy091/Digi-ID
        public async Task <SoftwareBitmapSource> ConvertToSoftwareBitmapSource(SoftwareBitmap bitmap)
        {
            SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
            await bitmapSource.SetBitmapAsync(bitmap);

            return(bitmapSource);
        }
コード例 #15
0
        public async Task PauseVideoCaptureWithResult()
        {
            //<SnippetPauseCaptureWithResult>
            MediaCapturePauseResult result =
                await _mediaRecording.PauseWithResultAsync(Windows.Media.Devices.MediaCapturePauseBehavior.RetainHardwareResources);

            var pausedFrame = result.LastFrame.SoftwareBitmap;

            if (pausedFrame.BitmapPixelFormat != BitmapPixelFormat.Bgra8 || pausedFrame.BitmapAlphaMode != BitmapAlphaMode.Ignore)
            {
                pausedFrame = SoftwareBitmap.Convert(pausedFrame, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore);
            }

            var source = new SoftwareBitmapSource();
            await source.SetBitmapAsync(pausedFrame);

            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                PauseImage.Source     = source;
                PauseImage.Visibility = Visibility.Visible;
            });

            _totalRecordedTime += result.RecordDuration;
            //</SnippetPauseCaptureWithResult>
        }
コード例 #16
0
ファイル: CameraAPI.cs プロジェクト: jeremy091/Digi-ID
        public async Task<SoftwareBitmapSource> ConvertToSoftwareBitmapSource(SoftwareBitmap bitmap)
        {
            SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
            await bitmapSource.SetBitmapAsync(bitmap);

            return bitmapSource;
        }
コード例 #17
0
ファイル: SlateView.cs プロジェクト: samoteph/Sugoi
        public async Task <SoftwareBitmapSource> GetSoftwareBitmapSourceAsync()
        {
            var source = new SoftwareBitmapSource();
            await source.SetBitmapAsync(this.GetSoftwareBitmap());

            return(source);
        }
コード例 #18
0
        private async void file2SoftwareBitmapSource(StorageFile file, int index = 0, bool isAddImage = false)
        {
            if (!isAddImage)
            {
                IStorageFolder applicationFolder = ApplicationData.Current.TemporaryFolder;
                await file.CopyAndReplaceAsync(await applicationFolder.CreateFileAsync(file.Name, CreationCollisionOption.ReplaceExisting));
            }

            SoftwareBitmapSource source = new SoftwareBitmapSource();
            SoftwareBitmap       sb     = null;

            using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
            {
                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

                SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);

                sb = softwareBitmap;
            }
            await source.SetBitmapAsync(sb);

            imageList.Insert(index, new CommunityImageList {
                imgUri = source, imgName = file.Name, imgPath = file.Path, imgAppPath = "ms-appdata:///Temp/" + file.Name
            });
        }
コード例 #19
0
ファイル: MainPage.xaml.cs プロジェクト: we4690/WinIotVideo
        async private void Timer_Tick(object sender, object e)
        {
            if (timer_tick_complete_flag == 1)
            {
                return;
            }
            timer_tick_complete_flag = 1;
            /*  stream server  */
            if (streamSocketSrv.receive_buf_flag == 1)
            {
                receviebitMap.CopyFromBuffer(streamSocketSrv.receiverbuf);
                await sbSource.SetBitmapAsync(receviebitMap);

                streamSocketSrv.receive_buf_flag = 0;
                client_send_serverip_flag        = 0;
            }

            if (streamSocketClient.flag_client_start == 0)
            {
                await streamSocketClient.start(clientip, "22333");

                client_send_serverip_flag = 1;
            }
            else
            {
                if (client_send_serverip_flag == 1)
                {
                    await streamSocketClient.sendmsgString(serverip);
                }
            }
            timer_tick_complete_flag = 0;
        }
コード例 #20
0
        private void UpdateImages()
        {
            if (_pvCameraImageContext.ImageSourceNeedsUpdate)
            {
                var setPvImageTask = this._pvImage.Dispatcher.RunAsync(
                    CoreDispatcherPriority.Normal,
                    async() =>
                {
                    if (_pvCameraImageContext.ImageDataUpdateInProgress)
                    {
                        return;
                    }

                    _pvCameraImageContext.ImageDataUpdateInProgress = true;

                    _pvCameraImageContext.ConvertedImage = _pvCameraImageContext.RawImage;

                    _pvCameraImageContext.ImageSourceNeedsUpdate = false;

                    var imageSource = new SoftwareBitmapSource();

                    await imageSource.SetBitmapAsync(
                        _pvCameraImageContext.ConvertedImage);

                    this._pvImage.Source = imageSource;

                    _pvCameraImageContext.ImageDataUpdateInProgress = false;
                });
            }
        }
コード例 #21
0
ファイル: ImageHelper.cs プロジェクト: nazzi88ua/Unigram
        public static async Task <ImageSource> CropAndPreviewAsync(IRandomAccessStream imageStream, Rect cropRectangle)
        {
            var decoder = await BitmapDecoder.CreateAsync(imageStream);

            var bounds = new BitmapBounds();

            bounds.X      = (uint)cropRectangle.X;
            bounds.Y      = (uint)cropRectangle.Y;
            bounds.Width  = (uint)cropRectangle.Width;
            bounds.Height = (uint)cropRectangle.Height;

            var transform = ComputeScalingTransformForSourceImage(decoder);

            transform.Bounds = bounds;

            var pixelData = await decoder.GetSoftwareBitmapAsync(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage);

            var propertySet  = new BitmapPropertySet();
            var qualityValue = new BitmapTypedValue(0.77, PropertyType.Single);

            propertySet.Add("ImageQuality", qualityValue);

            var bitmap = await decoder.GetSoftwareBitmapAsync(decoder.BitmapPixelFormat, BitmapAlphaMode.Premultiplied, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage);

            var bitmapImage = new SoftwareBitmapSource();
            await bitmapImage.SetBitmapAsync(bitmap);

            return(bitmapImage);
        }
コード例 #22
0
        // Open a Exist Image
        private async void OnOpen(object sender, RoutedEventArgs e)
        {
            FileOpen.IsEnabled   = false;
            FileSaving.IsEnabled = false;

            await mLock.WaitAsync();

            try
            {
                cacheFrame = await LoadVideoFrameFromFilePickedAsync();

                if (cacheFrame != null)
                {
                    SoftwareBitmapSource source = new SoftwareBitmapSource();
                    await source.SetBitmapAsync(cacheFrame.SoftwareBitmap);

                    PreviewImage.Source = source;
                    await ProcessWithOpenCV(cacheFrame);
                }
            }
            catch (Exception ex)
            {
                await(new MessageDialog(ex.Message)).ShowAsync();
            }

            mLock.Release();

            FileOpen.IsEnabled   = true;
            FileSaving.IsEnabled = true;
        }
コード例 #23
0
        private async void TakePhotoAsync()
        {
            mediaCapture = new MediaCapture();
            await mediaCapture.InitializeAsync();

            //mediaCapture.Failed += MediaCapture_Failed;

            // Prepare and capture photo
            // 캡쳐한 사진을 바로 XAML에서 보여줌
            var lowLagCapture = await mediaCapture.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8));

            var capturedPhoto = await lowLagCapture.CaptureAsync();

            var softwareBitmap = capturedPhoto.Frame.SoftwareBitmap;

            await lowLagCapture.FinishAsync();

            //임시로 저장된 거를 XAML에서 표시
            if (softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 ||
                softwareBitmap.BitmapAlphaMode == BitmapAlphaMode.Straight)
            {
                softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
            }

            var source = new SoftwareBitmapSource();
            await source.SetBitmapAsync(softwareBitmap);

            // Set the source of the Image control
            imageControl.Source = source;
        }
コード例 #24
0
ファイル: MainPage.xaml.cs プロジェクト: vertogaurav/Vision-
        private async void takePhoto_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);

                if (photo == null)
                {
                    //User cancelled photo
                    return;
                }
                else
                {
                    imageStream = await photo.OpenAsync(FileAccessMode.Read);

                    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(imageStream);

                    SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();

                    SoftwareBitmap       softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
                    SoftwareBitmapSource bitmapSource       = new SoftwareBitmapSource();
                    await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);

                    image.Source = bitmapSource;
                }
            }
            catch
            {
                output.Text = "Error taking photo";
            }
        }
コード例 #25
0
        private void UpdateImage(CameraImageContext context, Image image)
        {
            if (!context.ImageSourceNeedsUpdate)
            {
                return;
            }

            var setImageTask = image.Dispatcher.RunAsync(
                CoreDispatcherPriority.Normal,
                async() =>
            {
                if (context.ImageDataUpdateInProgress)
                {
                    return;
                }

                context.ImageDataUpdateInProgress = true;

                context.ConvertedImage = SoftwareBitmap.Convert(
                    context.RawImage,
                    BitmapPixelFormat.Bgra8,
                    BitmapAlphaMode.Premultiplied);

                context.ImageSourceNeedsUpdate = false;

                var imageSource = new SoftwareBitmapSource();

                await imageSource.SetBitmapAsync(
                    context.ConvertedImage);

                image.Source = imageSource;

                context.ImageDataUpdateInProgress = false;
            });
        }
コード例 #26
0
        private async void showImage(StorageFile pic)
        {
            if (pic == null)
            {
                // User Cancelled the Photo
                return;
            }
            else
            {
                imageStream = await pic.OpenAsync(FileAccessMode.Read);

                // Everything after this was done just to Show the Image correctly
                // Convert Stream to Bitmap
                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(imageStream);

                // Store the Bitmap
                SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();

                // For better Display : Alpha - Transperancy
                SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
                // Bitmap Source
                SoftwareBitmapSource softwareBitmapSource = new SoftwareBitmapSource();
                await softwareBitmapSource.SetBitmapAsync(softwareBitmapBGR8);

                // Setting the ImageBox's Source to our formatted BitmapSource
                imgFace.Source = softwareBitmapSource;

                btnCheckEmotion.IsEnabled = true;
            }
        }
コード例 #27
0
        private async void image_Tapped(object sender, TappedRoutedEventArgs e)
        {
            try
            {
                CameraCaptureUI captureUI = new CameraCaptureUI();
                captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;

                captureUI.PhotoSettings.CroppedSizeInPixels = new Size(1280, 720);

                photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);

                stream = await photo.OpenAsync(FileAccessMode.Read);

                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

                SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();

                SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap,
                                                                           BitmapPixelFormat.Bgra8,
                                                                           BitmapAlphaMode.Premultiplied);

                SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
                await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);

                imgPhoto.Visibility = Visibility.Visible;
                imgPhoto.Source     = bitmapSource;
            }catch { }
        }
コード例 #28
0
        private async Task <StorageFile> Select()
        {
            FileOpenPicker openPicker = new FileOpenPicker();

            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            openPicker.FileTypeFilter.Add(".jpg");
            openPicker.FileTypeFilter.Add(".jpeg");
            openPicker.FileTypeFilter.Add(".png");

            StorageFile file = await openPicker.PickSingleFileAsync();

            if (file != null)
            {
                SoftwareBitmap      softwareBitmap;
                IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);

                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);

                softwareBitmap = await decoder.GetSoftwareBitmapAsync();

                if (softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 ||
                    softwareBitmap.BitmapAlphaMode == BitmapAlphaMode.Straight)
                {
                    softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
                }
                var source = new SoftwareBitmapSource();
                await source.SetBitmapAsync(softwareBitmap);

                image.Source = source;
            }
            return(file);
        }  //更改图片
コード例 #29
0
ファイル: Utilities.cs プロジェクト: matech96/UWPFaceDetector
        public static async Task <SoftwareBitmapSource> ToSoftwareBitmapSourceAsync(StorageFile input)
        {
            using (IRandomAccessStream fileStream = await input.OpenAsync(FileAccessMode.Read))
            {
                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);

                BitmapTransform transform = new BitmapTransform();
                const float     sourceImageHeightLimit = 1280;

                if (decoder.PixelHeight > sourceImageHeightLimit)
                {
                    float scalingFactor = (float)sourceImageHeightLimit / (float)decoder.PixelHeight;
                    transform.ScaledWidth  = (uint)Math.Floor(decoder.PixelWidth * scalingFactor);
                    transform.ScaledHeight = (uint)Math.Floor(decoder.PixelHeight * scalingFactor);
                }

                SoftwareBitmap sourceBitmap = await decoder.GetSoftwareBitmapAsync(decoder.BitmapPixelFormat,
                                                                                   BitmapAlphaMode.Premultiplied,
                                                                                   transform,
                                                                                   ExifOrientationMode.IgnoreExifOrientation,
                                                                                   ColorManagementMode.DoNotColorManage);

                SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
                await bitmapSource.SetBitmapAsync(sourceBitmap);

                return(bitmapSource);
            }
        }
コード例 #30
0
        public static async Task <SoftwareBitmapSource> ThumbnailProcess(StorageFile inputFile)
        {
            const uint             requestedSize    = 300;
            const ThumbnailMode    thumbnailMode    = ThumbnailMode.SingleItem;
            const ThumbnailOptions thumbnailOptions = ThumbnailOptions.ResizeThumbnail;
            var thumbnail = await inputFile.GetThumbnailAsync(thumbnailMode, requestedSize, thumbnailOptions);

            var            source = new SoftwareBitmapSource();
            SoftwareBitmap softwareBitmap;

            using (IRandomAccessStream stream = thumbnail)
            {
                // Create the decoder from the stream
                var decoder = await BitmapDecoder.CreateAsync(stream);

                // Get the SoftwareBitmap representation of the file
                softwareBitmap = await decoder.GetSoftwareBitmapAsync();

                decoder = null;
                stream.Dispose();
            }
            if (softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 ||
                softwareBitmap.BitmapAlphaMode == BitmapAlphaMode.Straight)
            {
                softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
            }

            await source.SetBitmapAsync(softwareBitmap);

            thumbnail.Dispose();//如果有机器慢的话就删除这句
            // softwareBitmap.Dispose();//造成不能实现UI虚拟化的元凶,代价是增加内存
            return(source);
        }
コード例 #31
0
        private async Task ShowImage(SoftwareBitmap softwareBitmap)
        {
            var imageSource = new SoftwareBitmapSource();
            await imageSource.SetBitmapAsync(softwareBitmap);

            Image.Source = imageSource;
        }
コード例 #32
0
        private async void button_Click(object sender, RoutedEventArgs e)
        {
            CameraCaptureUI captureUI = new CameraCaptureUI();
            captureUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
            captureUI.PhotoSettings.CroppedSizeInPixels = new Size(200, 200);

            StorageFile photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);

            if (photo == null)
            {
                // User cancelled photo capture
                return;
            }
            IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);
            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
            SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();
            SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap,
            BitmapPixelFormat.Bgra8,
            BitmapAlphaMode.Premultiplied);

            SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
            await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);

            imageControl.Source = bitmapSource;

        }
コード例 #33
0
 //TODO:未登录时不能选择上传头像
 private async void headimgRectangle_Tapped(object sender, TappedRoutedEventArgs e)
 {
     //if (appSetting.Values.ContainsKey("idNum"))
     try
     {
         var vault = new Windows.Security.Credentials.PasswordVault();
         var credentialList = vault.FindAllByResource(resourceName);
         credentialList[0].RetrievePassword();
         if (credentialList.Count > 0)
         {
             FileOpenPicker openPicker = new FileOpenPicker();
             openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
             openPicker.FileTypeFilter.Add(".png");
             openPicker.FileTypeFilter.Add(".jpg");
             openPicker.FileTypeFilter.Add(".bmp");
             openPicker.FileTypeFilter.Add(".gif");
             openPicker.ContinuationData["Operation"] = "img";
             StorageFile file = await openPicker.PickSingleFileAsync();
             if (file != null)
             {
                 ClipHeadGrid.Visibility = Visibility.Visible;
                 BackOpacityGrid.Visibility = Visibility.Visible;
                 SoftwareBitmap sb = null;
                 using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
                 {
                     // Create the decoder from the stream
                     BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
                     // Get the SoftwareBitmap representation of the file
                     SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
                     sb = softwareBitmap;
                     // return softwareBitmap;
                 }
                 SoftwareBitmapSource source = new SoftwareBitmapSource();
                 await source.SetBitmapAsync(sb);
                 headImage.Source = source;
             }
         }
         else
         {
             var msgPopup = new Data.loginControl("您还没有登录 不能上传头像哦~");
             msgPopup.LeftClick += (s, c) => { Frame rootFrame = Window.Current.Content as Frame; rootFrame.Navigate(typeof(LoginPage)); };
             msgPopup.RightClick += (s, c) => { new MessageDialog("您可以先去社区逛一逛~"); };
             msgPopup.ShowWIndow();
         }
     }
     catch
     {
         var msgPopup = new Data.loginControl("您还没有登录 不能上传头像哦~");
         msgPopup.LeftClick += (s, c) => { Frame rootFrame = Window.Current.Content as Frame; rootFrame.Navigate(typeof(LoginPage)); };
         msgPopup.RightClick += (s, c) => { new MessageDialog("您可以先去社区逛一逛~"); };
         msgPopup.ShowWIndow();
     }
 }
コード例 #34
0
ファイル: FaceViewModel.cs プロジェクト: evgri243/pubic-demos
        private static async Task<SoftwareBitmapSource> CropPhoto(StorageFile photo, FaceRectangle rectangle)
        {
            using (var imageStream = await photo.OpenReadAsync())
            {
                var decoder = await BitmapDecoder.CreateAsync(imageStream);
                if (decoder.PixelWidth >= rectangle.Left + rectangle.Width || decoder.PixelHeight >= rectangle.Top + rectangle.Height)
                {
                    var transform = new BitmapTransform
                    {
                        Bounds = new BitmapBounds
                        {
                            X = (uint)rectangle.Left,
                            Y = (uint)rectangle.Top,
                            Height = (uint)rectangle.Height,
                            Width = (uint)rectangle.Width
                        }
                    };

                    var softwareBitmapBGR8 = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage);
                    SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
                    await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);

                    return bitmapSource;
                }
                return null;
            }
        }
コード例 #35
0
        private async Task<SoftwareBitmapSource> PreparePhoto(StorageFile photo)
        {
            IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);
            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
            SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();

            SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
            SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
            await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);

            return bitmapSource;
        }
コード例 #36
0
        private async void file2SoftwareBitmapSource(StorageFile file, int index = 0, bool isAddImage = false)
        {
            if (!isAddImage)
            {
                IStorageFolder applicationFolder = ApplicationData.Current.TemporaryFolder;
                await file.CopyAndReplaceAsync(await applicationFolder.CreateFileAsync(file.Name, CreationCollisionOption.ReplaceExisting));
            }

            SoftwareBitmapSource source = new SoftwareBitmapSource();
            SoftwareBitmap sb = null;
            using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read))
            {
                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
                SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
                sb = softwareBitmap;
            }
            await source.SetBitmapAsync(sb);
            imageList.Insert(index, new CommunityImageList { imgUri = source, imgName = file.Name, imgPath = file.Path, imgAppPath = "ms-appdata:///Temp/" + file.Name });
        }
コード例 #37
0
        /// <summary>
        /// Gets the current preview frame as a SoftwareBitmap, displays its properties in a TextBlock, and can optionally display the image
        /// in the UI and/or save it to disk as a jpg
        /// </summary>
        /// <returns></returns>
        private async Task GetPreviewFrameAsSoftwareBitmapAsync()
        {
            // Get information about the preview
            var previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;

            // Create the video frame to request a SoftwareBitmap preview frame
            var videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height);

            // Capture the preview frame
            using (var currentFrame = await _mediaCapture.GetPreviewFrameAsync(videoFrame))
            {
                // Collect the resulting frame
                SoftwareBitmap previewFrame = currentFrame.SoftwareBitmap;

                // Show the frame information
                FrameInfoTextBlock.Text = String.Format("{0}x{1} {2}", previewFrame.PixelWidth, previewFrame.PixelHeight, previewFrame.BitmapPixelFormat);

                // Add a simple green filter effect to the SoftwareBitmap
                if (GreenEffectCheckBox.IsChecked == true)
                {
                    ApplyGreenFilter(previewFrame);
                }

                // Show the frame (as is, no rotation is being applied)
                if (ShowFrameCheckBox.IsChecked == true)
                {
                    // Create a SoftwareBitmapSource to display the SoftwareBitmap to the user
                    var sbSource = new SoftwareBitmapSource();
                    await sbSource.SetBitmapAsync(previewFrame);

                    // Display it in the Image control
                    PreviewFrameImage.Source = sbSource;
                }

                // Save the frame (as is, no rotation is being applied)
                if (SaveFrameCheckBox.IsChecked == true)
                {
                    await SaveSoftwareBitmapAsync(previewFrame);
                }
            }
        }
コード例 #38
0
ファイル: Camera.xaml.cs プロジェクト: wpdu/ControlTest
        private static async Task<ImageModel> Capture()
        {
            var allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

            // Get the desired camera by panel
            DeviceInformation desiredDevice = allVideoDevices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back);
            if (desiredDevice ==  null)
            {
                Debug.Write("no Camrea");
                return null;
            }
            CameraCaptureUI cameraUI = new CameraCaptureUI();
            cameraUI.PhotoSettings.Format = CameraCaptureUIPhotoFormat.Jpeg;
            cameraUI.PhotoSettings.AllowCropping = false;
            //cameraUI.PhotoSettings.CroppedSizeInPixels = new Size(1280,720);

            StorageFile photo = await cameraUI.CaptureFileAsync(CameraCaptureUIMode.Photo);
            if (photo == null)
                return null;

            IRandomAccessStream stream = await photo.OpenAsync(FileAccessMode.Read);
            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
            decoder = await BitmapDecoder.CreateAsync( await decoder.GetThumbnailAsync());

            SoftwareBitmap bitmapBgr8 = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);

            SoftwareBitmapSource imgsource = new SoftwareBitmapSource();
            await imgsource.SetBitmapAsync(bitmapBgr8);
            ImageModel imgModel = new ImageModel() { ImgFile = photo, ThumbnailImage = imgsource };
            
            return imgModel;
        }
        async Task InitSize()
        {
            try
            {
//                this._file = await StorageHelper.TryGetFileFromPathAsync(path);

                if (this._file == null) return;

                var appView = ApplicationView.GetForCurrentView();

                //this.gifViewer.Visibility = Visibility.Collapsed;
                this.Img.Visibility = Visibility.Visible;

                IInputStream inputStream = await this._file.OpenReadAsync();
                IRandomAccessStream memStream = new InMemoryRandomAccessStream();
                await RandomAccessStream.CopyAsync(inputStream, memStream);
                BitmapDecoder decoder = await BitmapDecoder.CreateAsync(memStream);
                SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);

                if (softwareBitmap == null)
                    return;
                SoftwareBitmapSource source = new SoftwareBitmapSource();
                await source.SetBitmapAsync(softwareBitmap);

                if (softwareBitmap.PixelHeight > appView.VisibleBounds.Height || softwareBitmap.PixelWidth > appView.VisibleBounds.Width)
                {
                    double ratio1, ratio2;
                    ratio1 = (double)softwareBitmap.PixelWidth / (double)appView.VisibleBounds.Width;
                    ratio2 = (double)softwareBitmap.PixelHeight / (double)appView.VisibleBounds.Height;

                    if (ratio1 > ratio2)
                    {
                        this.Img.Width = appView.VisibleBounds.Width;
                        this.Img.Height = softwareBitmap.PixelHeight / ratio1;
                        if (ratio1 > 1.0)
                            this.sv.MaxZoomFactor *= (float)ratio1;
                    }
                    else
                    {
                        this.Img.Width = softwareBitmap.PixelWidth / ratio2;
                        this.Img.Height = appView.VisibleBounds.Height;
                        if (ratio2 > 1.0)
                            this.sv.MaxZoomFactor *= (float)ratio2;
                    }
                }
                else
                {
                    this.Img.Width = softwareBitmap.PixelWidth;
                    this.Img.Height = softwareBitmap.PixelHeight;
                }

                this.sv.Width = appView.VisibleBounds.Width;
                this.sv.Height = appView.VisibleBounds.Height;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }
コード例 #40
0
        private async void mostrarFoto(StorageFile file)
        {
            IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read);
            BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
            SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();
            SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);

            SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
            await bitmapSource.SetBitmapAsync(softwareBitmapBGR8);

            Image ima = new Image();
            ima.Source = bitmapSource;

            editImage.Source = bitmapSource;
        }
コード例 #41
0
 public async Task TakePhotoAsync(Action<SoftwareBitmapSource> callback)
 {
     //kada uslika postaviti svoj stream 
     Slika = new InMemoryRandomAccessStream();
     try
     {
         //konvertovati uslikano u Software bitmap da se moze prikazati u image kontroli
         await MediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateBmp(), Slika);
         BitmapDecoder decoder = await BitmapDecoder.CreateAsync(Slika);
         SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();
         SoftwareBitmap softwareBitmapBGR8 = SoftwareBitmap.Convert(softwareBitmap,
 BitmapPixelFormat.Bgra8,
 BitmapAlphaMode.Premultiplied);
          SlikaBitmap = new SoftwareBitmapSource();
         await SlikaBitmap.SetBitmapAsync(softwareBitmapBGR8);
         callback(SlikaBitmap);
     }
     catch (Exception ex)
     {
         Debug.WriteLine("Exception when taking a photo: {0}", ex.ToString());
     }
 }
コード例 #42
0
        public static async Task<SoftwareBitmapSource> ImagePathToSoftwareBitmapSource(string path)
        {
            var file = await StorageHelper.TryGetFileAsync(path);
            var bitmap = await StorageFileToSoftwareBitmap(file);

            if (bitmap == null) return null;

            var source = new SoftwareBitmapSource();
            await source.SetBitmapAsync(bitmap);

            return source;
        }