为我的应用添加相机功能正在扼杀我!从基本的UWP相机样本开始,但我必须添加缩放功能,在预览控件周围添加ScrollViewer并使我的生活变得复杂。在不同的样本中找到了裁剪代码。一切都在横向上工作,但当您将平板电脑旋转为肖像时,裁剪后的图像(即使没有任何实际裁剪)也会旋转90度。
裁剪逻辑采用originX / Y,ScrollViewer.ViewportWidth / Height& ScrollViewer.ExtentWidth /高度。我不明白这些值如何随着旋转而变化!在Landscape中我使用topLeft作为X / Y.我会为肖像做什么?我将宽度设置为400,在两个视图中它都是STILL 400,所以我不相信应该翻转宽度/高度。
这是裁剪代码。任何人都可以评论2(实际上是4个)方向应该有什么不同吗?
async public static Task<WriteableBitmap> GetCroppedBitmapAsync(StorageFile originalImageFile,
Point startPoint, Size cropSize, Size previewSize)
{
using (IRandomAccessStream stream = await originalImageFile.OpenReadAsync())
{
// Create a decoder from the stream. With the decoder, we can get the properties of the image.
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
// Adjust the start x/y to the stream size
uint scaledStartPointX = (uint)Math.Round((decoder.PixelWidth * (startPoint.X / previewSize.Width)));
uint scaledStartPointY = (uint)Math.Round((decoder.PixelHeight * (startPoint.Y / previewSize.Height)));
// Now get the scaled size of the viewport (ie the cropped area)
uint selectedAreaWidth = (uint)Math.Round(decoder.PixelWidth * (cropSize.Width / previewSize.Width));
uint selectedAreaHeight = (uint)Math.Round(decoder.PixelHeight * (cropSize.Height / previewSize.Height));
// Get the cropped pixels.
byte[] pixels = await GetPixelData(decoder, scaledStartPointX, scaledStartPointY, selectedAreaWidth, selectedAreaHeight,
decoder.PixelWidth, decoder.PixelHeight);
// Stream the bytes into a WriteableBitmap
WriteableBitmap cropBmp = new WriteableBitmap((int)selectedAreaWidth, (int)selectedAreaHeight);
Stream pixStream = cropBmp.PixelBuffer.AsStream();
pixStream.Write(pixels, 0, (int)(selectedAreaWidth * selectedAreaHeight * 4));
return cropBmp;
}
}