我正在使用内置摄像头解码条形码,我使用df_new
执行此操作以从预览中捕获照片。它有效,但冻结了应用程序一小段时间,感觉非常笨拙和马车。
因此,我希望在后台使用此功能,同时至少在处理照片时留下响应式用户界面。
到目前为止,我想出了这个来捕获视频流:
capElement.Source.CapturePhotoToStreamAsync
这个方法从流中获取字节:
private async void ScanInBackground()
{
bool failedScan = true;
var stream = new InMemoryRandomAccessStream();
await capElement.Source.StartRecordToStreamAsync(MediaEncodingProfile.CreateWmv(VideoEncodingQuality.HD1080p), stream);
while(failedScan)
{
Byte[] bytes = await GetBytesFromStream(stream);
//How to split the bytes into frames?
Task.Delay(50);
}
Dispatcher.RunAsync(CoreDispatcherPriority.Low,() => StopCap());
}
从public static async Task<byte[]> GetBytesFromStream(IRandomAccessStream randomStream)
{
var reader = new DataReader(randomStream.GetInputStreamAt(0));
var bytes = new byte[randomStream.Size];
try
{
await reader.LoadAsync((uint)randomStream.Size); reader.ReadBytes(bytes);
}
catch(Exception ex)
{
Logger.LogExceptionAsync(ex, "GetBytesFromStream");
}
return bytes;
}
的评论中,您可以看到我不知道如何将流分割为照片/帧。
答案 0 :(得分:2)
Microsoft github页面上有一个相关的示例,尽管它们的目标是Windows 10.您可能有兴趣迁移项目以获得此功能。
GetPreviewFrame:此示例将捕获预览帧而不是完整的照片。一旦它有预览框架,它就可以读取和编辑它上面的像素。
以下是相关部分:
private async Task GetPreviewFrameAsSoftwareBitmapAsync()
{
// Get information about the preview
var previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties;
// Create the video frame to request a SoftwareBitmap preview frame
var videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height);
// Capture the preview frame
using (var currentFrame = await _mediaCapture.GetPreviewFrameAsync(videoFrame))
{
// Collect the resulting frame
SoftwareBitmap previewFrame = currentFrame.SoftwareBitmap;
// Add a simple green filter effect to the SoftwareBitmap
EditPixels(previewFrame);
}
}
private unsafe void EditPixels(SoftwareBitmap bitmap)
{
// Effect is hard-coded to operate on BGRA8 format only
if (bitmap.BitmapPixelFormat == BitmapPixelFormat.Bgra8)
{
// In BGRA8 format, each pixel is defined by 4 bytes
const int BYTES_PER_PIXEL = 4;
using (var buffer = bitmap.LockBuffer(BitmapBufferAccessMode.ReadWrite))
using (var reference = buffer.CreateReference())
{
// Get a pointer to the pixel buffer
byte* data;
uint capacity;
((IMemoryBufferByteAccess)reference).GetBuffer(out data, out capacity);
// Get information about the BitmapBuffer
var desc = buffer.GetPlaneDescription(0);
// Iterate over all pixels
for (uint row = 0; row < desc.Height; row++)
{
for (uint col = 0; col < desc.Width; col++)
{
// Index of the current pixel in the buffer (defined by the next 4 bytes, BGRA8)
var currPixel = desc.StartIndex + desc.Stride * row + BYTES_PER_PIXEL * col;
// Read the current pixel information into b,g,r channels (leave out alpha channel)
var b = data[currPixel + 0]; // Blue
var g = data[currPixel + 1]; // Green
var r = data[currPixel + 2]; // Red
// Boost the green channel, leave the other two untouched
data[currPixel + 0] = b;
data[currPixel + 1] = (byte)Math.Min(g + 80, 255);
data[currPixel + 2] = r;
}
}
}
}
}
并在课堂外宣布:
[ComImport]
[Guid("5b0d3235-4dba-4d44-865e-8f1d0e4fd04d")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
unsafe interface IMemoryBufferByteAccess
{
void GetBuffer(out byte* buffer, out uint capacity);
}
当然,您的项目必须允许所有这些的不安全代码才能正常工作。
仔细查看示例,了解如何获取所有详细信息。或者,要进行演练,您可以观看最近//版本/会议中的camera session,其中包括一些相机示例的演练。
答案 1 :(得分:1)
我认为展示媒体预览和处理不同的可能异常是必要的,这里有一个如何做到这一点的简单示例,
假设您有以下用户界面,其中CaptureElement
显示预览,Image
控件显示已捕获的图片,
mc:Ignorable="d" Loaded="MainPage_OnLoaded">
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}" VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<CaptureElement x:Name="PreviewElement" Width="400" Height="400" Grid.Column="0" Grid.Row="0"/>
<Image x:Name="ImageElement" Width="400" Height="400" Grid.Column="1" Grid.Row="0"/>
<Button Click="TakePhoto_Click" Content="Take Photo" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" />
</Grid>
在后面的代码声明了一个mediaCapture字段,
private MediaCapture _mediaCapture;
然后在页面加载的事件处理程序中你需要
并启动凸轮预览
private async void MainPage_OnLoaded(object sender, RoutedEventArgs e)
{
//Start the device
try
{
_mediaCapture = new MediaCapture();
_mediaCapture.RecordLimitationExceeded += MediaCapture_RecordLimitationExceeded;
_mediaCapture.Failed += MediaCapture_Failed;
await _mediaCapture.InitializeAsync();
}
catch (UnauthorizedAccessException ex)
{
(new MessageDialog("Set the permission to use the webcam")).ShowAsync();
}
catch (Exception ex)
{
(new MessageDialog("Can't initialize the webcam !")).ShowAsync();
}
//Start the preview
if (_mediaCapture != null)
{
try
{
PreviewElement.Source = _mediaCapture;
await _mediaCapture.StartPreviewAsync();
}
catch (Exception ex)
{
(new MessageDialog("Something went wrong !")).ShowAsync();
}
}
}
private async void MediaCapture_Failed(MediaCapture sender, MediaCaptureFailedEventArgs errorEventArgs)
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => (new MessageDialog("Media capture failed")).ShowAsync());
}
private async void MediaCapture_RecordLimitationExceeded(MediaCapture sender)
{
await _mediaCapture.StopRecordAsync();
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => (new MessageDialog("Record limitation exceeded")).ShowAsync());
}
最后在这里如何正确地拍摄,每一件事都是异步的,所以没有任何延迟或者无论如何
private async void TakePhoto_Click(object sender, RoutedEventArgs e)
{
if (_mediaCapture != null)
{
try
{
ImageEncodingProperties encodingProperties = ImageEncodingProperties.CreateJpeg();
WriteableBitmap bitmap = new WriteableBitmap((int)ImageElement.Width, (int)ImageElement.Height);
using (var imageStream = new InMemoryRandomAccessStream())
{
await this._mediaCapture.CapturePhotoToStreamAsync(encodingProperties, imageStream);
await imageStream.FlushAsync();
imageStream.Seek(0);
bitmap.SetSource(imageStream);
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
() =>
{
ImageElement.Source = bitmap;
});
}
}
catch (Exception ex)
{
(new MessageDialog("Something went wrong !")).ShowAsync();
}
}
}