本文整理汇总了C#中Windows.Media.Capture.MediaCapture.GetPreviewFrameAsync方法的典型用法代码示例。如果您正苦于以下问题:C# MediaCapture.GetPreviewFrameAsync方法的具体用法?C# MediaCapture.GetPreviewFrameAsync怎么用?C# MediaCapture.GetPreviewFrameAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Windows.Media.Capture.MediaCapture
的用法示例。
在下文中一共展示了MediaCapture.GetPreviewFrameAsync方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: StartScanningAsync
//.........这里部分代码省略.........
if (ScanningOptions.CameraResolutionSelector != null)
previewResolution = ScanningOptions.CameraResolutionSelector(availableResolutions);
// If the user did not specify a resolution, let's try and find a suitable one
if (previewResolution == null)
{
// Loop through all supported sizes
foreach (var sps in availableResolutions)
{
// Find one that's >= 640x360 but <= 1000x1000
// This will likely pick the *smallest* size in that range, which should be fine
if (sps.Width >= 640 && sps.Width <= 1000 && sps.Height >= 360 && sps.Height <= 1000)
{
previewResolution = new CameraResolution
{
Width = sps.Width,
Height = sps.Height
};
break;
}
}
}
if (previewResolution == null)
previewResolution = availableResolutions.LastOrDefault();
System.Diagnostics.Debug.WriteLine("Using Preview Resolution: {0}x{1}", previewResolution.Width, previewResolution.Height);
// Find the matching property based on the selection, again
var chosenProp = availableProperties.FirstOrDefault(ap => ((VideoEncodingProperties)ap).Width == previewResolution.Width && ((VideoEncodingProperties)ap).Height == previewResolution.Height);
// Set the selected resolution
await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(MediaStreamType.VideoPreview, chosenProp);
await SetPreviewRotationAsync();
captureElement.Stretch = Stretch.UniformToFill;
// Get our preview properties
var previewProperties = mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;
// Setup a frame to use as the input settings
var destFrame = new VideoFrame(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height);
var zxing = ScanningOptions.BuildBarcodeReader();
timerPreview = new Timer(async (state) => {
if (stopping)
return;
if (mediaCapture == null || mediaCapture.CameraStreamState != Windows.Media.Devices.CameraStreamState.Streaming)
return;
if (processing)
return;
if (!isAnalyzing)
return;
processing = true;
SoftwareBitmapLuminanceSource luminanceSource = null;
try
{
// Get preview
var frame = await mediaCapture.GetPreviewFrameAsync(destFrame);
// Create our luminance source
luminanceSource = new SoftwareBitmapLuminanceSource(frame.SoftwareBitmap);
} catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("GetPreviewFrame Failed: {0}", ex);
}
ZXing.Result result = null;
try
{
// Try decoding the image
if (luminanceSource != null)
result = zxing.Decode(luminanceSource);
}
catch (Exception ex)
{
}
// Check if a result was found
if (result != null && !string.IsNullOrEmpty (result.Text))
{
if (!ContinuousScanning)
await StopScanningAsync();
LastScanResult = result;
ScanCallback(result);
}
processing = false;
}, null, TimeSpan.FromMilliseconds(200), TimeSpan.FromMilliseconds(200));
}
示例2: StartScanningAsync
//.........这里部分代码省略.........
{
// Find one that's >= 640x360 but <= 1000x1000
// This will likely pick the *smallest* size in that range, which should be fine
if (sps.Width >= 640 && sps.Width <= 1000 && sps.Height >= 360 && sps.Height <= 1000)
{
previewResolution = new CameraResolution
{
Width = sps.Width,
Height = sps.Height
};
break;
}
}
}
if (previewResolution == null)
previewResolution = availableResolutions.LastOrDefault();
MobileBarcodeScanner.Log("Using Preview Resolution: {0}x{1}", previewResolution.Width, previewResolution.Height);
// Find the matching property based on the selection, again
var chosenProp = availableProperties.FirstOrDefault(ap => ((VideoEncodingProperties)ap).Width == previewResolution.Width && ((VideoEncodingProperties)ap).Height == previewResolution.Height);
// Pass in the requested preview size properties
// so we can set them at the same time as the preview rotation
// to save an additional set property call
await SetPreviewRotationAsync(chosenProp);
// *after* the preview is setup, set this so that the UI layout happens
// otherwise the preview gets stuck in a funny place on screen
captureElement.Stretch = Stretch.UniformToFill;
await SetupAutoFocus();
var zxing = ScanningOptions.BuildBarcodeReader();
timerPreview = new Timer(async (state) => {
var delay = ScanningOptions.DelayBetweenAnalyzingFrames;
if (stopping || processing || !isAnalyzing
|| (mediaCapture == null || mediaCapture.CameraStreamState != Windows.Media.Devices.CameraStreamState.Streaming))
{
timerPreview.Change(delay, Timeout.Infinite);
return;
}
processing = true;
SoftwareBitmapLuminanceSource luminanceSource = null;
try
{
// Get preview
var frame = await mediaCapture.GetPreviewFrameAsync(videoFrame);
// Create our luminance source
luminanceSource = new SoftwareBitmapLuminanceSource(frame.SoftwareBitmap);
} catch (Exception ex)
{
MobileBarcodeScanner.Log ("GetPreviewFrame Failed: {0}", ex);
}
ZXing.Result result = null;
try
{
// Try decoding the image
if (luminanceSource != null)
result = zxing.Decode(luminanceSource);
}
catch (Exception ex)
{
MobileBarcodeScanner.Log("Warning: zxing.Decode Failed: {0}", ex);
}
// Check if a result was found
if (result != null && !string.IsNullOrEmpty (result.Text))
{
if (!ContinuousScanning)
{
delay = Timeout.Infinite;
await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () => { await StopScanningAsync(); });
}
else
{
delay = ScanningOptions.DelayBetweenContinuousScans;
}
LastScanResult = result;
ScanCallback(result);
}
processing = false;
timerPreview.Change(delay, Timeout.Infinite);
}, null, ScanningOptions.InitialDelayBeforeAnalyzingFrames, Timeout.Infinite);
}