Win universal app 位图图像:使用纵向方向进行裁剪
在我的应用程序中添加摄像头功能真让我受不了!从基本的UWP相机示例开始,但我必须添加缩放功能,这在预览控件周围添加了滚动查看器,使我的生活变得复杂。在其他示例中找到裁剪代码。所有操作都可以在横向模式下进行,但当您旋转平板电脑来描绘裁剪后的图像时(即使没有任何实际裁剪),图像也会旋转90度 裁剪逻辑采用originX/Y、ScrollViewer.ViewportWidth/Height和ScrollViewer.ExtentWidth/Height。我不明白这些值是如何随旋转而变化的!在风景画中,我用左上角来画X/Y。我该怎么画肖像画?我将宽度设置为400,但在两个视图中它仍然是400,因此我认为宽度/高度不应该翻转 这是裁剪代码。有人能对2个(实际上是4个)方向的不同之处发表评论吗Win universal app 位图图像:使用纵向方向进行裁剪,win-universal-app,uwp,Win Universal App,Uwp,在我的应用程序中添加摄像头功能真让我受不了!从基本的UWP相机示例开始,但我必须添加缩放功能,这在预览控件周围添加了滚动查看器,使我的生活变得复杂。在其他示例中找到裁剪代码。所有操作都可以在横向模式下进行,但当您旋转平板电脑来描绘裁剪后的图像时(即使没有任何实际裁剪),图像也会旋转90度 裁剪逻辑采用originX/Y、ScrollViewer.ViewportWidth/Height和ScrollViewer.ExtentWidth/Height。我不明白这些值是如何随旋转而变化的!在风景画中
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;
}
}
async公共静态任务GetCroppedBitmapAsync(StorageFile originalImageFile,
点开始点、大小预览、大小预览)
{
使用(irandomaccesstream=await originalImageFile.OpenReadAsync())
{
//从流中创建解码器。使用解码器,我们可以获得图像的属性。
BitmapDecoder decoder=等待BitmapDecoder.CreateAsync(流);
//将开始x/y调整为流大小
uint scaledStartPointX=(uint)Math.Round((decoder.PixelWidth*(startPoint.X/previewSize.Width));
uint scaledStartPointY=(uint)Math.Round((decoder.PixelHeight*(startPoint.Y/previewSize.Height));
//现在获取视口的缩放大小(即裁剪区域)
uint selectedAreaWidth=(uint)Math.Round(decoder.PixelWidth*(cropSize.Width/previewSize.Width));
uint selectedreaheight=(uint)Math.Round(decoder.PixelHeight*(cropSize.Height/previewSize.Height));
//获取裁剪后的像素。
byte[]pixels=等待GetPixelData(解码器、缩放起始点X、缩放起始点、selectedAreaWidth、selectedAreaHeight、,
解码器.PixelWidth,解码器.PixelHeight);
//将字节流式传输到可写Bitmap中
WriteableBitmap cropBmp=新的WriteableBitmap((int)selectedAreaWidth,(int)selectedAreaHeight);
Stream pixStream=cropBmp.PixelBuffer.AsStream();
写入(像素,0,(int)(selectedAreaWidth*selectedAreaHeight*4));
返回cropBmp;
}
}