C# 拍摄的照片有垂直条纹
我正在使用C# 拍摄的照片有垂直条纹,c#,windows-runtime,windows-phone-8.1,C#,Windows Runtime,Windows Phone 8.1,我正在使用WinRTMediacapture类拍摄一张照片,但是当我拍摄照片时,它会出现奇怪的透明条纹,这有点难以解释,所以这里有一些照片: 拍摄图片前(预览) 拍照后 我在这附近见过其他人也有类似的问题(比如,但他们的解决方案似乎对我不起作用。(要么没有结果,要么照片被弄糟了) 我用于设置分辨率的代码: System.Collections.Generic.IEnumerable<VideoEncodingProperties> available_resolutions =
WinRT
Mediacapture类拍摄一张照片,但是当我拍摄照片时,它会出现奇怪的透明条纹,这有点难以解释,所以这里有一些照片:
拍摄图片前(预览)
拍照后
我在这附近见过其他人也有类似的问题(比如,但他们的解决方案似乎对我不起作用。(要么没有结果,要么照片被弄糟了)
我用于设置分辨率的代码:
System.Collections.Generic.IEnumerable<VideoEncodingProperties> available_resolutions = captureManager.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo).Select(x => x as VideoEncodingProperties);
foreach (VideoEncodingProperties resolution in available_resolutions)
{
if (resolution != null && resolution.Width == 640 && resolution.Height == 480) //(resolution.Width==1920 && resolution.Height==1080) //resolution.Width==640 && resolution.Height==480)
{
await captureManager.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo, resolution);
}
}
System.Collections.Generic.IEnumerable available\u resolutions=captureManager.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.Photo)。选择(x=>x作为视频编码属性);
foreach(视频编码属性可用分辨率的分辨率)
{
如果(分辨率!=null&&resolution.Width==640&&resolution.Height==480)//(分辨率.Width==1920&&resolution.Height==1080)//分辨率.Width==640&&resolution.Height==480)
{
等待captureManager.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.Photo,分辨率);
}
}
我用来拍照的代码:
private async Task<BitmapImage> ByteArrayToBitmapImage(byte[] byteArray)
{
var bitmapImage = new BitmapImage();
using (var stream = new InMemoryRandomAccessStream())
{
await stream.WriteAsync(byteArray.AsBuffer());
stream.Seek(0);
await bitmapImage.SetSourceAsync(stream);
await stream.FlushAsync();
}
return bitmapImage;
}
/// <summary>
/// Relayed Execute method for TakePictureCommand.
/// </summary>
async void ExecuteTakePicture()
{
System.Diagnostics.Debug.WriteLine("Started making picture");
DateTime starttime = DateTime.Now;
ImageEncodingProperties format = ImageEncodingProperties.CreateJpeg();
using (var imageStream = new InMemoryRandomAccessStream())
{
await captureManager.CapturePhotoToStreamAsync(format, imageStream);
//Compresses the image if it exceedes the maximum file size
imageStream.Seek(0);
//Resize the image if needed
uint maxImageWidth = 640;
uint maxImageHeight = 480;
if (AvatarPhoto)
{
maxImageHeight = 200;
maxImageWidth = 200;
//Create a BitmapDecoder from the stream
BitmapDecoder resizeDecoder = await BitmapDecoder.CreateAsync(imageStream);
if (resizeDecoder.PixelWidth > maxImageWidth || resizeDecoder.PixelHeight > maxImageHeight)
{
//Resize the image if it exceedes the maximum width or height
WriteableBitmap tempBitmap = new WriteableBitmap((int)resizeDecoder.PixelWidth, (int)resizeDecoder.PixelHeight);
imageStream.Seek(0);
await tempBitmap.SetSourceAsync(imageStream);
WriteableBitmap resizedImage = tempBitmap.Resize((int)maxImageWidth, (int)maxImageHeight, WriteableBitmapExtensions.Interpolation.Bilinear);
tempBitmap = null;
//Assign to imageStream the resized WriteableBitmap
await resizedImage.ToStream(imageStream, BitmapEncoder.JpegEncoderId);
resizedImage = null;
}
//Converts the final image into a Base64 String
imageStream.Seek(0);
}
//Converts the final image into a Base64 String
imageStream.Seek(0);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(imageStream);
PixelDataProvider pixels = await decoder.GetPixelDataAsync();
byte[] bytes = pixels.DetachPixelData();
//Encode image
InMemoryRandomAccessStream encoded = new InMemoryRandomAccessStream();
BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, encoded);
encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, maxImageWidth, maxImageHeight, decoder.DpiX, decoder.DpiY, bytes);
//Rotate the image based on the orientation of the camera
if (currentOrientation == DisplayOrientations.Portrait)
{
encoder.BitmapTransform.Rotation = BitmapRotation.Clockwise90Degrees;
}
else if (currentOrientation == DisplayOrientations.LandscapeFlipped)
{
encoder.BitmapTransform.Rotation = BitmapRotation.Clockwise180Degrees;
}
if (FrontCam)
{
if (currentOrientation == DisplayOrientations.Portrait)
{
encoder.BitmapTransform.Rotation = BitmapRotation.Clockwise270Degrees;
}
else if (currentOrientation == DisplayOrientations.LandscapeFlipped)
{
encoder.BitmapTransform.Rotation = BitmapRotation.Clockwise180Degrees;
}
}
await encoder.FlushAsync();
encoder = null;
//Read bytes
byte[] outBytes = new byte[encoded.Size];
await encoded.AsStream().ReadAsync(outBytes, 0, outBytes.Length);
encoded.Dispose();
encoded = null;
//Create Base64
image = await ByteArrayToBitmapImage(outBytes);
System.Diagnostics.Debug.WriteLine("Pixel width: " + image.PixelWidth + " height: " + image.PixelHeight);
base64 = Convert.ToBase64String(outBytes);
Array.Clear(outBytes, 0, outBytes.Length);
await imageStream.FlushAsync();
imageStream.Dispose();
}
DateTime endtime = DateTime.Now;
TimeSpan span = (endtime - starttime);
//Kind of a hacky way to prevent high RAM usage and even crashing, remove when overal RAM usage has been lowered
GC.Collect();
System.Diagnostics.Debug.WriteLine("Making the picture took: " + span.Seconds + " seconds");
if (image != null)
{
RaisePropertyChanged("CapturedImage");
//Tell both UsePictureCommand and ResetCommand that the situation has changed.
((RelayedCommand)UsePictureCommand).RaiseCanExecuteChanged();
((RelayedCommand)ResetCommand).RaiseCanExecuteChanged();
}
else
{
throw new InvalidOperationException("Imagestream is not valid");
}
}
专用异步任务ByteArrayToBitmapImage(字节[]byteArray)
{
var bitmapImage=新的bitmapImage();
使用(var stream=new InMemoryRandomAccessStream())
{
wait stream.WriteAsync(byteArray.AsBuffer());
stream.Seek(0);
等待bitmapImage.SetSourceAsync(流);
等待stream.FlushAsync();
}
返回位图图像;
}
///
///TakePictureCommand的中继执行方法。
///
异步void ExecuteTakePicture()
{
System.Diagnostics.Debug.WriteLine(“开始制作图片”);
DateTime starttime=DateTime.Now;
ImageEncodingProperties格式=ImageEncodingProperties.CreateJpeg();
使用(var imageStream=new InMemoryRandomAccessStream())
{
等待captureManager.CapturePhotoToStreamAsync(格式,imageStream);
//如果图像超过最大文件大小,则压缩图像
imageStream.Seek(0);
//如果需要,调整图像大小
uint最大宽度=640;
uint最大高度=480;
如果(阿凡达照片)
{
最大高度=200;
最大宽度=200;
//从流创建位图解码器
BitmapDecoder resizeDecoder=等待BitmapDecoder.CreateAsync(imageStream);
if(resizeDecoder.PixelWidth>maxImageWidth | | resizeDecoder.PixelHeight>maxImageHeight)
{
//如果图像超出最大宽度或高度,请调整图像大小
WriteableBitmap tempBitmap=新的WriteableBitmap((int)resizeDecoder.PixelWidth,(int)resizeDecoder.PixelHeight);
imageStream.Seek(0);
等待tempBitmap.SetSourceAsync(imageStream);
WriteableBitmap resizedImage=tempBitmap.Resize((int)maxImageWidth,(int)maxImageHeight,WriteableBitmapExtensions.Interpolation.Bilinear);
tempBitmap=null;
//将调整大小的WriteableBitmap分配给imageStream
等待resizedImage.ToStream(imageStream,BitmapEncoder.JpegEncoderId);
resizedImage=null;
}
//将最终图像转换为Base64字符串
imageStream.Seek(0);
}
//将最终图像转换为Base64字符串
imageStream.Seek(0);
BitmapDecoder decoder=等待BitmapDecoder.CreateAsync(imageStream);
PixelDataProvider pixels=等待解码器。GetPixelDataAsync();
byte[]bytes=像素。DetachPixelData();
//编码图像
InMemoryRandomAccessStream encoded=新的InMemoryRandomAccessStream();
BitmapEncoder编码器=等待BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId,encoded);
编码器.SetPixelData(BitmapPixelFormat.Bgra8,BitmapAlphaMode.Ignore,maxImageWidth,maxImageHeight,decoder.DpiX,decoder.DpiY,bytes);
//根据相机的方向旋转图像
if(currentOrientation==DisplayOrientations.grait)
{
编码器.BitmapTransform.Rotation=BitmapRotation.Clockwise90度;
}
else if(currentOrientation==DisplayOrientations.LandscapeFlipped)
{
encoder.BitmapTransform.Rotation=BitmapRotation.Clockwise180度;
}
if(前摄像头)
{
if(currentOrientation==DisplayOrientations.grait)
{
encoder.BitmapTransform.Rotation=BitmapRotation.ClockWise270度;
}
else if(currentOrientation==DisplayOrientations.LandscapeFlipped)
{
encoder.BitmapTransform.Rotation=BitmapRotation.Clockwise180度;
}
}
等待编码器。FlushAsync();
编码器=空;
//读取字节
byte[]outBytes=新字节[encoded.Size];
wait encoded.AsStream().ReadAsync(outBytes,0,outBytes.Length);
encoded.Dispose();
编码=空;
//创建Base64
image=wait ByteArrayToBitmapImage(outBytes);
System.Diagnostics.Debug.WriteLine(“像素宽度:+image.PixelWidth+”高度:+image.PixelHeight”);
base64=Convert.ToBase64String(outBytes);
Array.Clear(outBytes,0,outBytes.Length);
等待imageStream.FlushAsync();
Dispose();
}
DateTime endtime=DateTime.Now;
时间跨度=(结束时间-开始时间);
//这是一种防止高血压的方法