//<SnippetOpenCVFrameArrived> private void ColorFrameReader_FrameArrived_OpenCV(MediaFrameReader sender, MediaFrameArrivedEventArgs args) { var mediaFrameReference = sender.TryAcquireLatestFrame(); if (mediaFrameReference != null) { SoftwareBitmap openCVInputBitmap = null; var inputBitmap = mediaFrameReference.VideoMediaFrame?.SoftwareBitmap; if (inputBitmap != null) { //The XAML Image control can only display images in BRGA8 format with premultiplied or no alpha if (inputBitmap.BitmapPixelFormat == BitmapPixelFormat.Bgra8 && inputBitmap.BitmapAlphaMode == BitmapAlphaMode.Premultiplied) { openCVInputBitmap = SoftwareBitmap.Copy(inputBitmap); } else { openCVInputBitmap = SoftwareBitmap.Convert(inputBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); } SoftwareBitmap openCVOutputBitmap = new SoftwareBitmap(BitmapPixelFormat.Bgra8, openCVInputBitmap.PixelWidth, openCVInputBitmap.PixelHeight, BitmapAlphaMode.Premultiplied); // operate on the image and render it openCVHelper.Blur(openCVInputBitmap, openCVOutputBitmap); _frameRenderer.PresentSoftwareBitmap(openCVOutputBitmap); } } }
private async void OpenCVButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) { // <SnippetOpenCVBlur> FileOpenPicker fileOpenPicker = new FileOpenPicker(); fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; fileOpenPicker.FileTypeFilter.Add(".jpg"); fileOpenPicker.ViewMode = PickerViewMode.Thumbnail; var inputFile = await fileOpenPicker.PickSingleFileAsync(); if (inputFile == null) { // The user cancelled the picking operation return; } SoftwareBitmap inputBitmap; using (IRandomAccessStream stream = await inputFile.OpenAsync(FileAccessMode.Read)) { // Create the decoder from the stream BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); // Get the SoftwareBitmap representation of the file inputBitmap = await decoder.GetSoftwareBitmapAsync(); } if (inputBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 || inputBitmap.BitmapAlphaMode != BitmapAlphaMode.Premultiplied) { inputBitmap = SoftwareBitmap.Convert(inputBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); } SoftwareBitmap outputBitmap = new SoftwareBitmap(inputBitmap.BitmapPixelFormat, inputBitmap.PixelWidth, inputBitmap.PixelHeight, BitmapAlphaMode.Premultiplied); var helper = new OpenCVBridge.OpenCVHelper(); helper.Blur(inputBitmap, outputBitmap); var bitmapSource = new SoftwareBitmapSource(); await bitmapSource.SetBitmapAsync(outputBitmap); imageControl.Source = bitmapSource; // </SnippetOpenCVBlur> }
private async void Options_changed(object sender, SelectionChangedEventArgs e) { if (Cut.IsSelected) { string desiredName = DateTime.Now.Ticks + ".jpg"; StorageFolder applicationFolder = ApplicationData.Current.LocalFolder; StorageFolder folder = await applicationFolder.CreateFolderAsync("Pic", CreationCollisionOption.OpenIfExists); StorageFile saveFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.OpenIfExists); RenderTargetBitmap bitmap = new RenderTargetBitmap(); await bitmap.RenderAsync(Img); var pixelBuffer = await bitmap.GetPixelsAsync(); using (var fileStream = await saveFile.OpenAsync(FileAccessMode.ReadWrite)) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, DisplayInformation.GetForCurrentView().LogicalDpi, DisplayInformation.GetForCurrentView().LogicalDpi, pixelBuffer.ToArray()); await encoder.FlushAsync(); } if (saveFile != null) { CutPicture(saveFile); } } else { if (addfilter.IsSelected) { string desiredName = DateTime.Now.Ticks + ".jpg"; StorageFolder applicationFolder = ApplicationData.Current.LocalFolder; StorageFolder folder = await applicationFolder.CreateFolderAsync("Pic", CreationCollisionOption.OpenIfExists); StorageFile saveFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.OpenIfExists); RenderTargetBitmap bitmap = new RenderTargetBitmap(); await bitmap.RenderAsync(Img); var pixelBuffer = await bitmap.GetPixelsAsync(); using (var fileStream = await saveFile.OpenAsync(FileAccessMode.ReadWrite)) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, DisplayInformation.GetForCurrentView().LogicalDpi, DisplayInformation.GetForCurrentView().LogicalDpi, pixelBuffer.ToArray()); await encoder.FlushAsync(); } if (saveFile != null) { BlankPage2 editor = new BlankPage2(); editor.Show(saveFile); editor.ImageEditedCompleted += (image_edited) => { Img.Source = image_edited; }; } } else { if (blur.IsSelected) { string desiredName = DateTime.Now.Ticks + ".jpg"; StorageFolder applicationFolder = ApplicationData.Current.LocalFolder; StorageFolder folder = await applicationFolder.CreateFolderAsync("Pic", CreationCollisionOption.OpenIfExists); StorageFile saveFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.OpenIfExists); RenderTargetBitmap bitmap = new RenderTargetBitmap(); await bitmap.RenderAsync(Img); var pixelBuffer = await bitmap.GetPixelsAsync(); using (var fileStream = await saveFile.OpenAsync(FileAccessMode.ReadWrite)) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, DisplayInformation.GetForCurrentView().LogicalDpi, DisplayInformation.GetForCurrentView().LogicalDpi, pixelBuffer.ToArray()); await encoder.FlushAsync(); } if (saveFile == null) { // The user cancelled the picking operation return; } SoftwareBitmap inputBitmap; using (IRandomAccessStream stream = await saveFile.OpenAsync(FileAccessMode.Read)) { // Create the decoder from the stream BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); // Get the SoftwareBitmap representation of the file inputBitmap = await decoder.GetSoftwareBitmapAsync(); } if (inputBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 || inputBitmap.BitmapAlphaMode != BitmapAlphaMode.Premultiplied) { inputBitmap = SoftwareBitmap.Convert(inputBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); } SoftwareBitmap outputBitmap = new SoftwareBitmap(inputBitmap.BitmapPixelFormat, inputBitmap.PixelWidth, inputBitmap.PixelHeight, BitmapAlphaMode.Premultiplied); var helper = new OpenCVBridge.OpenCVHelper(); helper.Blur(inputBitmap, outputBitmap); var bitmapSource = new SoftwareBitmapSource(); await bitmapSource.SetBitmapAsync(outputBitmap); Img.Source = bitmapSource; } else { if (scrawl.IsSelected) { string desiredName = DateTime.Now.Ticks + ".jpg"; StorageFolder applicationFolder = ApplicationData.Current.LocalFolder; StorageFolder folder = await applicationFolder.CreateFolderAsync("Pic", CreationCollisionOption.OpenIfExists); StorageFile saveFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.OpenIfExists); RenderTargetBitmap bitmap = new RenderTargetBitmap(); await bitmap.RenderAsync(Img); var pixelBuffer = await bitmap.GetPixelsAsync(); using (var fileStream = await saveFile.OpenAsync(FileAccessMode.ReadWrite)) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, DisplayInformation.GetForCurrentView().LogicalDpi, DisplayInformation.GetForCurrentView().LogicalDpi, pixelBuffer.ToArray()); await encoder.FlushAsync(); } if (saveFile != null) { scrawl editor = new scrawl(); editor.Show(saveFile); editor.ImageEditedCompleted += (image_edited) => { Img.Source = image_edited; }; } } else { if (open.IsSelected) { //文件选择器 FileOpenPicker openPicker = new FileOpenPicker(); //初始位置 openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; //添加文件类型 openPicker.FileTypeFilter.Add(".jpg"); openPicker.FileTypeFilter.Add(".jpeg"); openPicker.FileTypeFilter.Add(".png"); openPicker.FileTypeFilter.Add(".gif"); //选取单个文件 Windows.Storage.StorageFile file = await openPicker.PickSingleFileAsync(); if (file != null) { using (IRandomAccessStream stream = await file.OpenAsync(FileAccessMode.Read)) { var srcImage = new BitmapImage(); await srcImage.SetSourceAsync(stream); Img.Source = srcImage; } } } else { if (regenerate.IsSelected) { MyFrame.Navigate(typeof(Page1)); } else { if (Camera.IsSelected) { 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; } else { if (save.IsSelected) { FileSavePicker savefile = new FileSavePicker(); //初始位置 savefile.SuggestedStartLocation = PickerLocationId.PicturesLibrary; // 显示在下拉列表的文件类型 savefile.FileTypeChoices.Add("图片", new List <string>() { ".png", ".jpg", ".jpeg", ".bmp" }); // 默认的文件名 savefile.SuggestedFileName = "SaveFile"; StorageFile sFile = await savefile.PickSaveFileAsync(); if (sFile != null) { // 在用户完成更改并调用CompleteUpdatesAsync之前,阻止对文件的更新 CachedFileManager.DeferUpdates(sFile); //把控件变成图像 RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(); //传入参数Image控件 await renderTargetBitmap.RenderAsync(Img); IBuffer pixelbuffer = await renderTargetBitmap.GetPixelsAsync(); using (var fileStream = await sFile.OpenAsync(FileAccessMode.ReadWrite)) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream); encoder.SetPixelData( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)renderTargetBitmap.PixelWidth, (uint)renderTargetBitmap.PixelHeight, DisplayInformation.GetForCurrentView().LogicalDpi, DisplayInformation.GetForCurrentView().LogicalDpi, pixelbuffer.ToArray() ); //刷新图像 await encoder.FlushAsync(); } } } else { if (Sharpen.IsSelected) { string desiredName = DateTime.Now.Ticks + ".jpg"; StorageFolder applicationFolder = ApplicationData.Current.LocalFolder; StorageFolder folder = await applicationFolder.CreateFolderAsync("Pic", CreationCollisionOption.OpenIfExists); StorageFile saveFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.OpenIfExists); RenderTargetBitmap bitmap = new RenderTargetBitmap(); await bitmap.RenderAsync(Img); var pixelBuffer = await bitmap.GetPixelsAsync(); using (var fileStream = await saveFile.OpenAsync(FileAccessMode.ReadWrite)) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, DisplayInformation.GetForCurrentView().LogicalDpi, DisplayInformation.GetForCurrentView().LogicalDpi, pixelBuffer.ToArray()); await encoder.FlushAsync(); } if (saveFile != null) { BlankPage1 editor = new BlankPage1(); editor.Show(saveFile); editor.ImageEditedCompleted += (image_edited) => { Img.Source = image_edited; }; } } else { string desiredName = DateTime.Now.Ticks + ".jpg"; StorageFolder applicationFolder = ApplicationData.Current.LocalFolder; StorageFolder folder = await applicationFolder.CreateFolderAsync("Pic", CreationCollisionOption.OpenIfExists); StorageFile saveFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.OpenIfExists); RenderTargetBitmap bitmap = new RenderTargetBitmap(); await bitmap.RenderAsync(Img); var pixelBuffer = await bitmap.GetPixelsAsync(); using (var fileStream = await saveFile.OpenAsync(FileAccessMode.ReadWrite)) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, DisplayInformation.GetForCurrentView().LogicalDpi, DisplayInformation.GetForCurrentView().LogicalDpi, pixelBuffer.ToArray()); await encoder.FlushAsync(); } if (saveFile != null) { paster editor = new paster(); editor.Show(saveFile); editor.ImageEditedCompleted += (image_edited) => { Img.Source = image_edited; }; } } } } } } } } } } }
private async void mohu_Click(object sender, RoutedEventArgs e) { string desiredName = DateTime.Now.Ticks + ".jpg"; StorageFolder applicationFolder = ApplicationData.Current.LocalFolder; StorageFolder folder = await applicationFolder.CreateFolderAsync("Pic", CreationCollisionOption.OpenIfExists); StorageFile saveFile = await folder.CreateFileAsync(desiredName, CreationCollisionOption.OpenIfExists); RenderTargetBitmap bitmap = new RenderTargetBitmap(); await bitmap.RenderAsync(Img); var pixelBuffer = await bitmap.GetPixelsAsync(); using (var fileStream = await saveFile.OpenAsync(FileAccessMode.ReadWrite)) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, DisplayInformation.GetForCurrentView().LogicalDpi, DisplayInformation.GetForCurrentView().LogicalDpi, pixelBuffer.ToArray()); await encoder.FlushAsync(); } if (saveFile == null) { // The user cancelled the picking operation return; } SoftwareBitmap inputBitmap; using (IRandomAccessStream stream = await saveFile.OpenAsync(FileAccessMode.Read)) { // Create the decoder from the stream BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); // Get the SoftwareBitmap representation of the file inputBitmap = await decoder.GetSoftwareBitmapAsync(); } if (inputBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 || inputBitmap.BitmapAlphaMode != BitmapAlphaMode.Premultiplied) { inputBitmap = SoftwareBitmap.Convert(inputBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); } SoftwareBitmap outputBitmap = new SoftwareBitmap(inputBitmap.BitmapPixelFormat, inputBitmap.PixelWidth, inputBitmap.PixelHeight, BitmapAlphaMode.Premultiplied); var helper = new OpenCVBridge.OpenCVHelper(); helper.Blur(inputBitmap, outputBitmap); var bitmapSource = new SoftwareBitmapSource(); await bitmapSource.SetBitmapAsync(outputBitmap); Img.Source = bitmapSource; }
// 选取图片 private async Task PickFile() { var picker = new Windows.Storage.Pickers.FileOpenPicker { ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail, SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary }; //新建文件选取器 picker.FileTypeFilter.Add(".jpg"); picker.FileTypeFilter.Add(".jpeg"); //联合图像专家组(JPEG) picker.FileTypeFilter.Add(".png"); //便携式网络图形(PNG) picker.FileTypeFilter.Add(".gif"); //图形交换格式(GIF) picker.FileTypeFilter.Add(".tiff"); //标记图像文件格式(TIFF) picker.FileTypeFilter.Add(".bmp"); //位图(BMP) picker.FileTypeFilter.Add(".ico"); //图标(ICO) // Windows.Storage.StorageFile file = await picker.PickSingleFileAsync(); StorageFile file = await picker.PickSingleFileAsync(); if (file != null) { // IRandomAccessStream stream = await file.OpenReadAsync(); // <BitmapImage有关文档:https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.media.imaging.bitmapimage> // BitmapImage bitmap = new BitmapImage(); //新建Bitmap图像 // await bitmap.SetSourceAsync(stream); // originImage.Source = bitmap; // Application now has read/write access to the picked file SoftwareBitmap inputBitmap; 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 inputBitmap = await decoder.GetSoftwareBitmapAsync(); } if (inputBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 || inputBitmap.BitmapAlphaMode != BitmapAlphaMode.Premultiplied) { inputBitmap = SoftwareBitmap.Convert(inputBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); } SoftwareBitmap outputBitmap = new SoftwareBitmap(inputBitmap.BitmapPixelFormat, inputBitmap.PixelWidth, inputBitmap.PixelHeight, BitmapAlphaMode.Premultiplied); var helper = new OpenCVBridge.OpenCVHelper(); helper.Blur(inputBitmap, outputBitmap); var bitmapSource = new SoftwareBitmapSource(); await bitmapSource.SetBitmapAsync(outputBitmap); originImage.Source = bitmapSource; this.textBlock.Text = "Picked photo: " + file.Name; } else { this.textBlock.Text = "Operation cancelled."; } }