簡體   English   中英

Windows Phone 8.1釋放PreviewFrame並計算每幀紅色像素的平均值

[英]windows phone 8.1 dispaly PreviewFrame and calculate mean value of red pixels for every frame

目前我正在使用Lumia.Imaging獲取預覽幀並顯示它。

我創建新的方法“ GetPreview()”遍歷像素,找到紅色像素,然后我想計算每幀紅色像素的平均值。

我的問題是,當我通過像素時,應用程序存在滯后:(

  • 在不損失性能的情況下,為每幀計算紅色像素平均值的正確解決方案是什么?

  • 附加的如何在預覽開始時打開閃光燈?

      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處理功能。

    要開始編寫自定義過濾器,請參閱文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM