windows phone 8.1显示PreviewFrame并计算每帧红色像素的平均值
windows phone 8.1 dispaly PreviewFrame and calculate mean value of red pixels for every frame
目前我正在使用 Lumia.Imaging 获取预览帧并显示它。
我创建了新方法 "GetPreview()" 来遍历像素,找到红色像素,然后我想计算每一帧的红色像素的平均值。
我的问题是,当我浏览 Pixel 时,应用程序出现滞后 :(
在不损失性能的情况下计算每帧红色像素平均值的正确解决方案是什么?
另外如何在预览开始时打开闪光灯?
private async Task startCameraPreview()
{
// Create a camera preview image source (from the Lumia Imaging SDK)
_cameraPreviewImageSource = new CameraPreviewImageSource();
// Checking id of back camera
DeviceInformationCollection devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(Windows.Devices.Enumeration.DeviceClass.VideoCapture);
String backCameraId = devices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back).Id;
await _cameraPreviewImageSource.InitializeAsync(backCameraId); // use the back camera
var previewProperties = await _cameraPreviewImageSource.StartPreviewAsync();
fps = previewProperties.FrameRate.Numerator/previewProperties.FrameRate.Denominator;
_cameraPreviewImageSource.PreviewFrameAvailable += drawPreview; // call the drawPreview method every time a new frame is available
// Create a preview bitmap with the correct aspect ratio using the properties object returned when the preview started.
var width = 640.0;
var height = (width / previewProperties.Width) * previewProperties.Height;
var bitmap = new WriteableBitmap((int)width, (int)height);
_writeableBitmap = bitmap;
// Create a BitmapRenderer to turn the preview Image Source into a bitmap we hold in the PreviewBitmap object
_effect = new FilterEffect(_cameraPreviewImageSource);
_effect.Filters = new IFilter[0]; // null filter for now
_writeableBitmapRenderer = new WriteableBitmapRenderer(_effect, _writeableBitmap);
}
private async void drawPreview(IImageSize args)
{
// Prevent multiple rendering attempts at once
if (_isRendering == false)
{
_isRendering = true;
await _writeableBitmapRenderer.RenderAsync(); // Render the image (with no filter)
// Draw the image onto the previewImage XAML element
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High,
() =>
{
getPreview();
previewImage.Source = _writeableBitmap; // previewImage is an image element in MainPage.xaml
_writeableBitmap.Invalidate(); // force the PreviewBitmap to redraw
});
_isRendering = false;
}
}
private void getPreview()
{
var pixelBuffer = _writeableBitmap.PixelBuffer;
for (uint i = 0; i + 4 < pixelBuffer.Length; i += 4)
{
var red = pixelBuffer.GetByte(i + 2);
}
}
不是在 Lumia Imaging SDK 处理完图像后检查所有像素,而是在使位图无效之前,您可以:
- 立即使可写位图无效,然后在单独的异步任务中执行分析步骤。这意味着内容将立即显示,您的分析将单独完成。基于您的样本的一些伪代码是:
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () => {
var analysisTask = Task.Run(() => getPreview());
previewImage.Source = _writeableBitmap; // previewImage is an image element in MainPage.xaml
_writeableBitmap.Invalidate(); // force the PreviewBitmap to redraw
await analysisTask;
});
这样分析图像的任务就不会阻止屏幕上的更新。当然,如果您需要渲染链本身的分析结果,此选项可能不可行。
为分析创建自定义过滤器,这样您就可以利用优化的 Lumia Imaging SDK 处理。
开始编写自定义过滤器 look at the documentation。
目前我正在使用 Lumia.Imaging 获取预览帧并显示它。
我创建了新方法 "GetPreview()" 来遍历像素,找到红色像素,然后我想计算每一帧的红色像素的平均值。
我的问题是,当我浏览 Pixel 时,应用程序出现滞后 :(
在不损失性能的情况下计算每帧红色像素平均值的正确解决方案是什么?
另外如何在预览开始时打开闪光灯?
private async Task startCameraPreview() { // Create a camera preview image source (from the Lumia Imaging SDK) _cameraPreviewImageSource = new CameraPreviewImageSource(); // Checking id of back camera DeviceInformationCollection devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(Windows.Devices.Enumeration.DeviceClass.VideoCapture); String backCameraId = devices.FirstOrDefault(x => x.EnclosureLocation != null && x.EnclosureLocation.Panel == Windows.Devices.Enumeration.Panel.Back).Id; await _cameraPreviewImageSource.InitializeAsync(backCameraId); // use the back camera var previewProperties = await _cameraPreviewImageSource.StartPreviewAsync(); fps = previewProperties.FrameRate.Numerator/previewProperties.FrameRate.Denominator; _cameraPreviewImageSource.PreviewFrameAvailable += drawPreview; // call the drawPreview method every time a new frame is available // Create a preview bitmap with the correct aspect ratio using the properties object returned when the preview started. var width = 640.0; var height = (width / previewProperties.Width) * previewProperties.Height; var bitmap = new WriteableBitmap((int)width, (int)height); _writeableBitmap = bitmap; // Create a BitmapRenderer to turn the preview Image Source into a bitmap we hold in the PreviewBitmap object _effect = new FilterEffect(_cameraPreviewImageSource); _effect.Filters = new IFilter[0]; // null filter for now _writeableBitmapRenderer = new WriteableBitmapRenderer(_effect, _writeableBitmap); } private async void drawPreview(IImageSize args) { // Prevent multiple rendering attempts at once if (_isRendering == false) { _isRendering = true; await _writeableBitmapRenderer.RenderAsync(); // Render the image (with no filter) // Draw the image onto the previewImage XAML element await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () => { getPreview(); previewImage.Source = _writeableBitmap; // previewImage is an image element in MainPage.xaml _writeableBitmap.Invalidate(); // force the PreviewBitmap to redraw }); _isRendering = false; } } private void getPreview() { var pixelBuffer = _writeableBitmap.PixelBuffer; for (uint i = 0; i + 4 < pixelBuffer.Length; i += 4) { var red = pixelBuffer.GetByte(i + 2); } }
不是在 Lumia Imaging SDK 处理完图像后检查所有像素,而是在使位图无效之前,您可以:
- 立即使可写位图无效,然后在单独的异步任务中执行分析步骤。这意味着内容将立即显示,您的分析将单独完成。基于您的样本的一些伪代码是:
await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High, () => {
var analysisTask = Task.Run(() => getPreview());
previewImage.Source = _writeableBitmap; // previewImage is an image element in MainPage.xaml
_writeableBitmap.Invalidate(); // force the PreviewBitmap to redraw
await analysisTask;
});
这样分析图像的任务就不会阻止屏幕上的更新。当然,如果您需要渲染链本身的分析结果,此选项可能不可行。
为分析创建自定义过滤器,这样您就可以利用优化的 Lumia Imaging SDK 处理。
开始编写自定义过滤器 look at the documentation。