我正在尝试在单击事件上拍摄视频画笔显示的“快照”,但由于某种原因,我无法正确显示此快照。
double height = Application.Current.Host.Content.ActualHeight;
//Capture the screen
private void freeze_Click(object sender, EventArgs e)
{
// Takes the preview buffer pixel array from camera and returns
// an array of pixels representing the camera's preview buffer
int[] previewBuffer = GetPreviewBuffer();
//Process the preview image
int[] imageData = PhotofunDataContext.SelectedEffect.Effect.ProcessImage(previewBuffer);
//Copy to WriteableBitmap
//previewWriteableBitmap = new WriteableBitmap((int)camera.PreviewResolution.Width, (int)camera.PreviewResolution.Height);
WriteableBitmap freezeWriteableBitmap = new WriteableBitmap((int)camera.PreviewResolution.Width, (int)camera.PreviewResolution.Height);
imageData.CopyTo(freezeWriteableBitmap.Pixels, 0);
freezeWriteableBitmap.Invalidate();
//Display that on the screen
FreezeImage.Source = freezeWriteableBitmap;
//height of device
FreezeImage.Width = height;
//width to maintain 4:3 aspect ratio
FreezeImage.Height = height * (.75);
FreezeImage.Opacity = 1;
}
}
//Get the preview buffer
private int[] GetPreviewBuffer()
{
int[] pixelData = new int[(int)(camera.PreviewResolution.Width * camera.PreviewResolution.Height)];
//int[] pixelData = new int[(int)width * (int)height];
camera.GetPreviewBufferArgb32(pixelData);
return pixelData;
}
上面的代码用于拍摄视频画笔的快照并将其显示在当前活动的视频画笔上作为冻结帧。
<Rectangle x:Name="videoRectangle" Width="1" Height="1" HorizontalAlignment="Center">
<Rectangle.Fill>
<VideoBrush x:Name="viewfinderBrush" >
<VideoBrush.RelativeTransform>
<CompositeTransform x:Name="viewfinderTransform" Rotation="90"
CenterX="0.5" CenterY="0.5"/>
</VideoBrush.RelativeTransform>
</VideoBrush>
</Rectangle.Fill>
</Rectangle>
<Image x:Name="FreezeImage" Opacity="0" HorizontalAlignment="Center" Stretch="Fill" Canvas.ZIndex="900">
<Image.Projection>
<PlaneProjection RotationZ="-90"/>
</Image.Projection>
</Image>
FreezeImage 现在覆盖了活动的视频画笔,但图像尺寸与手机尺寸不匹配。我想在纵向模式下将 FreezeImage 不拉伸到整个手机屏幕。
当图像大小适合手机屏幕时,我尝试设置FreezeImage.Width = height;
并FreezeImage.Height = height * (.75);
保持相机的自然 4:3 纵横比,但这不会覆盖预览缓冲区和 FreezeWriteableBitmap 尺寸 (640x480)。
我还尝试对 previewbuffer 和 FreezeWriteableBitmap 的宽度(=1064)和高度(=800)尺寸进行硬编码,但是图像根本不显示(就像 writeableBitmap 一样,除了相机尺寸之外,它不接受任何其他值)。
在玩弄了相机初始化之后,我发现无论我将分辨率设置为什么,writeableBitmaps 始终默认为 640x480,这就是我认为问题所在,因为看起来这个纵横比是手机上实际显示的屏幕。如何修复此实现,以便生成的 freezeImage 高度与手机屏幕高度匹配并调整宽度以不执行拉伸?
FreezeImage.Width = height;
注意:设置时的高度值FreezeImage.Height = height * (.75);
是double height = Application.Current.Host.Content.ActualHeight;
为考虑到未来WP7设备除了当前标准480x800之外的其他尺寸而设置的。