Windows phone 8.1 windows phone 8.1显示预览帧并计算每帧红色像素的平均值

Windows phone 8.1 windows phone 8.1显示预览帧并计算每帧红色像素的平均值,windows-phone-8.1,flashlight,lumia-imaging-sdk,Windows Phone 8.1,Flashlight,Lumia Imaging Sdk,目前我正在使用Lumia.Imaging获取预览帧并显示它 我创建了一个新方法“GetPreview()”来遍历像素,找到红色像素,然后计算每一帧的红色像素的平均值 我的问题是,当我通过像素有滞后的应用程序:( 在没有性能损失的情况下,计算每帧红色像素平均值的正确解决方案是什么 其他如何在预览开始时打开闪光灯 private async Task startCameraPreview() { // Create a camera preview image source (fr

目前我正在使用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);
        }
    }
    
    private异步任务startCameraPreview()
    {
    //创建相机预览图像源(来自Lumia Imaging SDK)
    _cameraPreviewImageSource=新cameraPreviewImageSource();
    //检查后摄像头的id
    DeviceInformation Collection devices=等待Windows.devices.Enumeration.DeviceInformation.FindAllAsync(Windows.devices.Enumeration.DeviceClass.VideoCapture);
    字符串backCameraId=devices.FirstOrDefault(x=>x.EnclosureLocation!=null&&x.EnclosureLocation.Panel==Windows.devices.Enumeration.Panel.Back).Id;
    等待_cameraPreviewImageSource.InitializeAsync(backCameraId);//使用后摄像头
    var previewProperties=await _cameraPreviewImageSource.startPreviewSync();
    fps=previewProperties.FrameRate.Numerator/previewProperties.FrameRate.Denominator;
    _cameraPreviewImageSource.PreviewFrameAvailable+=drawPreview;//每次新帧可用时调用drawPreview方法
    //使用预览开始时返回的properties对象创建具有正确纵横比的预览位图。
    可变宽度=640.0;
    变量高度=(宽度/previewProperties.width)*previewProperties.height;
    var bitmap=新的WriteableBitmap((int)宽度,(int)高度);
    _writeableBitmap=位图;
    //创建位图渲染器,将预览图像源转换为我们在PreviewBitmap对象中保存的位图
    _效果=新过滤器效果(\u cameraPreviewImageSource);
    _effect.Filters=new IFilter[0];//暂时为空筛选器
    _writeableBitmapRenderer=新的writeableBitmapRenderer(_效果,_writeableBitmap);
    }
    专用异步void drawPreview(IImageSize参数)
    {
    //一次防止多次渲染尝试
    如果(_isRendering==false)
    {
    _isRendering=true;
    wait _writeableBitmapRenderer.RenderAsync();//渲染图像(不带过滤器)
    //将图像绘制到previewImage XAML元素上
    等待Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High,
    () =>
    {
    getPreview();
    previewImage.Source=\u writeableBitmap;//previewImage是MainPage.xaml中的图像元素
    _writeableBitmap.Invalidate();//强制重新绘制PreviewBitmap
    });
    _isRendering=false;
    }
    }
    私有void getPreview()
    {
    var pixelBuffer=_writeableBitmap.pixelBuffer;
    对于(uint i=0;i+4

在Lumia Imaging SDK处理图像之后,但在使位图无效之前,您可以:

  • 立即使可写位图无效,然后在单独的异步任务中执行分析步骤。这意味着将立即显示内容,并单独执行分析。基于示例的一些伪代码如下:
等待Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High,()=>{
var analysisTask=Task.Run(()=>getPreview());
previewImage.Source=\u writeableBitmap;//previewImage是MainPage.xaml中的图像元素
_writeableBitmap.Invalidate();//强制重新绘制PreviewBitmap
等待任务;
});
这样,分析图像的任务不会阻止屏幕上的更新。当然,如果您需要渲染链本身的分析结果,则此选项可能不可行

  • 为分析创建自定义过滤器,这样您就可以利用优化的Lumia Imaging SDK处理

    开始编写自定义过滤器


在Lumia Imaging SDK处理图像之后,但在使位图无效之前,您可以:

  • 立即使可写位图无效,然后在单独的异步任务中执行分析步骤。这意味着将立即显示内容,并单独执行分析。基于示例的一些伪代码如下:
等待Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.High,()=>{
var analysisTask=Task.Run(()=>getPreview());
previewImage.Source=\u writeableBitmap;//previewImage是MainPage.xaml中的图像元素
_writeableBitmap.Invalidate();//强制重新绘制PreviewBitmap
等待任务;
});
这样,分析图像的任务不会阻止屏幕上的更新。当然,如果您需要渲染链本身的分析结果,则此选项可能不可行

  • 为分析创建自定义过滤器,这样您就可以利用优化的Lumia Imaging SDK处理

    开始编写自定义过滤器