[UWP]转换为灰度时获得损坏的图像 [英] [UWP] Getting Corrupt image when converting to greyscale
本文介绍了[UWP]转换为灰度时获得损坏的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试捕获图像并转换为灰度。点击移动设备时,我会看到这样的损坏图像,但点击移动设备时也不会发生同样的情况。此外,如果我从图库中选择文件,我会得到适当的灰度图像。
这是我的代码:
private async void CapturePic_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)
{
//用户取消照片拍摄
返回;
}
BitmapImage Image = await StorageFileToBitmapImage(photo);
BitmapDecoder decoder = null;
using(IRandomAccessStreamWithContentType stream = await photo.OpenReadAsync())
{
decoder = await BitmapDecoder.CreateAsync(stream);
//获取第一帧
BitmapFrame bitmapFrame = await decoder.GetFrameAsync(0);
//保存分辨率(稍后将用于保存文件)
// dpiX = bitmapFrame.DpiX;
// dpiY = bitmapFrame.DpiY;
//获取像素
PixelDataProvider dataProvider =
等待bitmapFrame.GetPixelDataAsync(BitmapPixelFormat.Bgra8,
BitmapAlphaMode.Premultiplied,
new BitmapTransform(),
ExifOrientationMode.RespectExifOrientation,
ColorManagementMode.ColorManageToSRgb);
byte [] pixels = dataProvider.DetachPixelData();
//创建WriteableBitmap并设置像素
WriteableBitmap bitmap = new WriteableBitmap((int)bitmapFrame.PixelWidth,
(int)bitmapFrame.PixelHeight);
using(Stream pixelStream = bitmap.PixelBuffer.AsStream())
{
await pixelStream.WriteAsync(pixels,0,pixels.Length);
}
//使WriteableBitmap无效并设置为Image源
bitmap.Invalidate();
// image.Source = bitmap;
imageControl.Source = bitmap;
}
WriteableBitmap srcBitmap = imageControl.Source as WriteableBitmap;
byte [] srcPixels = new byte [4 * srcBitmap.PixelWidth * srcBitmap.PixelHeight];
使用(Stream pixelStream = srcBitmap.PixelBuffer.AsStream())
{
await pixelStream.ReadAsync(srcPixels,0,srcPixels.Length);
}
//创建目标位图和像素数组
WriteableBitmap dstBitmap =
new WriteableBitmap(srcBitmap.PixelWidth,srcBitmap.PixelHeight);
byte [] dstPixels = new byte [4 * dstBitmap.PixelWidth * dstBitmap.PixelHeight];
for(int i = 0; i< srcPixels.Length; i + = 4)
{
double b =(double)srcPixels [i] / 255.0;
double g =(double)srcPixels [i + 1] / 255.0;
double r =(double)srcPixels [i + 2] / 255.0;
byte a = srcPixels [i + 3];
double z =(0.21 * r + 0.71 * g + 0.07 * b)* 255;
byte f = Convert.ToByte(z);
dstPixels [i] = f;
dstPixels [i + 1] = f;
dstPixels [i + 2] = f;
dstPixels [i + 3] = a;
}
//使用(Stream pixelStream = dstBitmap.PixelBuffer.AsStream())将像素移动到目标位图
{
等待pixelStream.WriteAsync(dstPixels,0,dstPixels.Length);
}
dstBitmap.Invalidate();
//显示新位图
imageControl.Source = dstBitmap;
// imageControl.Source = Image;
StorageFolder destinationFolder =
await ApplicationData.Current.LocalFolder.CreateFolderAsync(" ProfilePhotoFolder",
CreationCollisionOption.OpenIfExists);
await photo.CopyAsync(destinationFolder," ProfilePhoto.jpg",NameCollisionOption.ReplaceExisting);
await photo.DeleteAsync();
}
公共静态异步任务< BitmapImage> StorageFileToBitmapImage(StorageFile savedStorageFile)
{
using(IRandomAccessStream fileStream = await savedStorageFile.OpenAsync(Windows.Storage.FileAccessMode.Read))
{
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.DecodePixelHeight = 28;
bitmapImage.DecodePixelWidth = 28;
等待bitmapImage.SetSourceAsync(fileStream);
返回bitmapImage;
}
}
这背后可能是什么原因?
解决方案
我会使用lumia成像sdk并对图像应用灰度滤镜
https://msdn.microsoft.com/library/mt598502.aspx?f=255&MSPPError=-2147217396
I am trying to capture Image and convert to greyscale. I get a corrupt image like this while clicking through mobile but the same doesn't happen when clicking through mobile. Also if i select file from gallery I get proper greyscale image.
Here is My Code:
private async void CapturePic_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; } BitmapImage Image = await StorageFileToBitmapImage(photo); BitmapDecoder decoder = null; using (IRandomAccessStreamWithContentType stream = await photo.OpenReadAsync()) { decoder = await BitmapDecoder.CreateAsync(stream); // Get the first frame BitmapFrame bitmapFrame = await decoder.GetFrameAsync(0); // Save the resolution (will be used for saving the file later) // dpiX = bitmapFrame.DpiX; //dpiY = bitmapFrame.DpiY; // Get the pixels PixelDataProvider dataProvider = await bitmapFrame.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, new BitmapTransform(), ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb); byte[] pixels = dataProvider.DetachPixelData(); // Create WriteableBitmap and set the pixels WriteableBitmap bitmap = new WriteableBitmap((int)bitmapFrame.PixelWidth, (int)bitmapFrame.PixelHeight); using (Stream pixelStream = bitmap.PixelBuffer.AsStream()) { await pixelStream.WriteAsync(pixels, 0, pixels.Length); } // Invalidate the WriteableBitmap and set as Image source bitmap.Invalidate(); // image.Source = bitmap; imageControl.Source = bitmap; } WriteableBitmap srcBitmap = imageControl.Source as WriteableBitmap; byte[] srcPixels = new byte[4 * srcBitmap.PixelWidth * srcBitmap.PixelHeight]; using (Stream pixelStream = srcBitmap.PixelBuffer.AsStream()) { await pixelStream.ReadAsync(srcPixels, 0, srcPixels.Length); } // Create a destination bitmap and pixels array WriteableBitmap dstBitmap = new WriteableBitmap(srcBitmap.PixelWidth, srcBitmap.PixelHeight); byte[] dstPixels = new byte[4 * dstBitmap.PixelWidth * dstBitmap.PixelHeight]; for (int i = 0; i < srcPixels.Length; i += 4) { double b = (double)srcPixels[i] / 255.0; double g = (double)srcPixels[i + 1] / 255.0; double r = (double)srcPixels[i + 2] / 255.0; byte a = srcPixels[i + 3]; double z = (0.21 * r + 0.71 * g + 0.07 * b) * 255; byte f = Convert.ToByte(z); dstPixels[i] = f; dstPixels[i + 1] = f; dstPixels[i + 2] = f; dstPixels[i + 3] = a; } // Move the pixels into the destination bitmap using (Stream pixelStream = dstBitmap.PixelBuffer.AsStream()) { await pixelStream.WriteAsync(dstPixels, 0, dstPixels.Length); } dstBitmap.Invalidate(); // Display the new bitmap imageControl.Source = dstBitmap; // imageControl.Source = Image; StorageFolder destinationFolder = await ApplicationData.Current.LocalFolder.CreateFolderAsync("ProfilePhotoFolder", CreationCollisionOption.OpenIfExists); await photo.CopyAsync(destinationFolder, "ProfilePhoto.jpg", NameCollisionOption.ReplaceExisting); await photo.DeleteAsync(); } public static async Task<BitmapImage> StorageFileToBitmapImage(StorageFile savedStorageFile) { using (IRandomAccessStream fileStream = await savedStorageFile.OpenAsync(Windows.Storage.FileAccessMode.Read)) { BitmapImage bitmapImage = new BitmapImage(); bitmapImage.DecodePixelHeight = 28; bitmapImage.DecodePixelWidth = 28; await bitmapImage.SetSourceAsync(fileStream); return bitmapImage; } }
What could be the reason behind this?
解决方案
I would use the lumia imaging sdk and apply a gray scale filter to the image
https://msdn.microsoft.com/library/mt598502.aspx?f=255&MSPPError=-2147217396
这篇关于[UWP]转换为灰度时获得损坏的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文