/// <summary> /// Will capture a single image and store it in the baseline images list. Each image will be used to determine if we have an alert or not. /// </summary> private async void CaptureBaseLineImage() { try { lowLagCapture = await MediaCaptureElement.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8)); CapturedPhoto capturedPhoto = await lowLagCapture.CaptureAsync(); SoftwareBitmap softwareBitmap = capturedPhoto.Frame.SoftwareBitmap; WriteableBitmap writeableBitmap = new WriteableBitmap(softwareBitmap.PixelWidth, softwareBitmap.PixelHeight); softwareBitmap.CopyToBuffer(writeableBitmap.PixelBuffer); byte[] imageBytes = new byte[4 * softwareBitmap.PixelWidth * softwareBitmap.PixelHeight]; softwareBitmap.CopyToBuffer(imageBytes.AsBuffer()); if (baselineImages.Count > 6) { baselineImages.Clear(); DisplayImages.Clear(); } baselineImages.Add(imageBytes); DisplayImages.Add(writeableBitmap); await lowLagCapture.FinishAsync(); } catch (Exception) { // Sometimes when you serial click the capture button we get an explosion... // Eat it and move on } }
private async Task <WriteableBitmap> ConvertPreviewToWriteableBitmap(SoftwareBitmap softwareBitmap, WriteableBitmap writeableBitmap) { int previewWidth = (int)m_renderer.PreviewSize.Width; int previewHeight = (int)m_renderer.PreviewSize.Height; if (writeableBitmap == null || writeableBitmap.PixelWidth != previewWidth || writeableBitmap.PixelHeight != previewHeight) { writeableBitmap = new WriteableBitmap(previewWidth, previewHeight); } if (softwareBitmap.PixelWidth != previewWidth || softwareBitmap.PixelHeight != previewHeight) { using (var renderer = new WriteableBitmapRenderer(new SoftwareBitmapImageSource(softwareBitmap))) { renderer.WriteableBitmap = writeableBitmap; await renderer.RenderAsync(); } } else { softwareBitmap.CopyToBuffer(writeableBitmap.PixelBuffer); } writeableBitmap.Invalidate(); return(writeableBitmap); }
private async void UserOption() { if (BitmapLang.ocrEngine != null && BitmapLang.softwareBitmap != null) { //Aparat ocrEngine = BitmapLang.ocrEngine; bitmap = BitmapLang.softwareBitmap; var imgSource = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight); bitmap.CopyToBuffer(imgSource.PixelBuffer); PreviewImage.Source = imgSource; OCR(); } if (BitmapLang.ocrEngine != null && BitmapLang.softwareBitmap == null) { //Zdjecie ocrEngine = BitmapLang.ocrEngine; PhotoPicker(); } if (BitmapLang.ocrEngine == null && BitmapLang.softwareBitmap == null) { Windows.UI.Popups.MessageDialog msg = new Windows.UI.Popups.MessageDialog("Coś poszło źle, spróbuj ponownie. :("); await msg.ShowAsync(); this.Frame.Navigate(typeof(MainPage)); } }
private void AutoOptimizeButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) { if (FilterImage != null) { FilterImage.Dispose(); FilterImage = null; FilterBackupImage.Dispose(); FilterBackupImage = null; } AlphaSlider.ValueChanged -= AlphaSlider_ValueChanged; BetaSlider.ValueChanged -= BetaSlider_ValueChanged; AlphaSlider.Value = 1; BetaSlider.Value = 0; AlphaSlider.ValueChanged += AlphaSlider_ValueChanged; BetaSlider.ValueChanged += BetaSlider_ValueChanged; FilterImage = ComputerVisionProvider.AutoColorEnhancement(OriginImage); FilterBackupImage = SoftwareBitmap.Copy(FilterImage); WriteableBitmap WBitmap = new WriteableBitmap(OriginImage.PixelWidth, OriginImage.PixelHeight); FilterImage.CopyToBuffer(WBitmap.PixelBuffer); Cropper.Source = WBitmap; using (SoftwareBitmap Histogram = ComputerVisionProvider.CalculateHistogram(FilterImage)) { WriteableBitmap HBitmap = new WriteableBitmap(Histogram.PixelWidth, Histogram.PixelHeight); Histogram.CopyToBuffer(HBitmap.PixelBuffer); HistogramImage.Source = HBitmap; } ResetButton.IsEnabled = true; }
/// <summary> /// Takes a preview frame and scans it for a QR code. /// </summary> /// <returns>The scan result or null if no code found.</returns> private async Task <Result> AnalyzePreviewFrameAsync() { Result result = null; if (!_analyzingFrame) { _analyzingFrame = true; var previewProperties = _mediaCapture.VideoDeviceController.GetMediaStreamProperties(MediaStreamType.VideoPreview) as VideoEncodingProperties; var videoFrame = new VideoFrame(BitmapPixelFormat.Bgra8, (int)previewProperties.Width, (int)previewProperties.Height); using (var currentFrame = await _mediaCapture.GetPreviewFrameAsync(videoFrame)) { SoftwareBitmap previewFrame = currentFrame.SoftwareBitmap; WriteableBitmap writeableBitmap = new WriteableBitmap(previewFrame.PixelWidth, previewFrame.PixelHeight); previewFrame.CopyToBuffer(writeableBitmap.PixelBuffer); result = mBarcodeReader.Decode(writeableBitmap); } _analyzingFrame = false; } else { System.Diagnostics.Debug.WriteLine("QrCodeScannerPage.AnalyzePreviewFrameAsync: Still analyzing the previous frame"); } return(result); }
async Task saveDebugImage(SoftwareBitmap bitmap, IList <PredictionModel> result) { WriteableBitmap newImage = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight); bitmap.CopyToBuffer(newImage.PixelBuffer); for (int predictIdx = 0; predictIdx < result.Count; predictIdx++) { PredictionModel predictInfo = result[predictIdx]; newImage.DrawRectangle((int)predictInfo.BoundingBox.Left * bitmap.PixelWidth, (int)predictInfo.BoundingBox.Top * bitmap.PixelHeight, (int)predictInfo.BoundingBox.Width * bitmap.PixelWidth, (int)predictInfo.BoundingBox.Height * bitmap.PixelHeight, Colors.Red); } Windows.Storage.StorageFile testFile = await folder.CreateFileAsync("test.jpg", Windows.Storage.CreationCollisionOption.ReplaceExisting); using (IRandomAccessStream stream = await testFile.OpenAsync(FileAccessMode.ReadWrite)) { Guid BitmapEncoderGuid = BitmapEncoder.JpegEncoderId; BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoderGuid, stream); Stream pixelStream = newImage.PixelBuffer.AsStream(); byte[] pixels = new byte[pixelStream.Length]; await pixelStream.ReadAsync(pixels, 0, pixels.Length); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)newImage.PixelWidth, (uint)newImage.PixelHeight, 96.0, 96.0, pixels); await encoder.FlushAsync(); } }
private async void SendImage() { SoftwareBitmap bmp = App.previewVideoFrames.Dequeue(); await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, async() => { SoftwareBitmapSource src = new SoftwareBitmapSource(); await src.SetBitmapAsync(bmp); imagePreview.Source = src; WriteableBitmap bmpBuffer = new WriteableBitmap(bmp.PixelWidth, bmp.PixelHeight); bmp.CopyToBuffer(bmpBuffer.PixelBuffer); byte[] bytes = bmpBuffer.PixelBuffer.ToArray(); byte[] widthBytes = BitConverter.GetBytes(bmp.PixelWidth); byte[] heightBytes = BitConverter.GetBytes(bmp.PixelHeight); byte[] imgSizeBytes = BitConverter.GetBytes(bytes.Length); bmp.Dispose(); stream.Write(imgSizeBytes, 0, imgSizeBytes.Length); stream.Write(widthBytes, 0, widthBytes.Length); stream.Write(heightBytes, 0, heightBytes.Length); stream.Write(bytes, 0, bytes.Length); }); }
private void RotationButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) { if (FilterImage == null) { SoftwareBitmap RotatedImage = ComputerVisionProvider.RotateEffect(OriginImage, 90); WriteableBitmap WBitmap = new WriteableBitmap(RotatedImage.PixelWidth, RotatedImage.PixelHeight); RotatedImage.CopyToBuffer(WBitmap.PixelBuffer); Cropper.Source = WBitmap; OriginImage.Dispose(); OriginImage = RotatedImage; } else { SoftwareBitmap OringinRotatedImage = ComputerVisionProvider.RotateEffect(OriginImage, 90); OriginImage.Dispose(); OriginImage = OringinRotatedImage; SoftwareBitmap RotatedImage = ComputerVisionProvider.RotateEffect(FilterImage, 90); WriteableBitmap WBitmap = new WriteableBitmap(RotatedImage.PixelWidth, RotatedImage.PixelHeight); RotatedImage.CopyToBuffer(WBitmap.PixelBuffer); Cropper.Source = WBitmap; FilterImage.Dispose(); FilterImage = RotatedImage; } ResetButton.IsEnabled = true; }
public static async Task <TensorFloat> NormalizeImage(VideoFrame frame, Vector3 mean, Vector3 std, uint width, uint height) { // , BitmapPixelFormat.Bgra8 var bitmapBuffer = new SoftwareBitmap(frame.SoftwareBitmap.BitmapPixelFormat, frame.SoftwareBitmap.PixelHeight, frame.SoftwareBitmap.PixelHeight, BitmapAlphaMode.Ignore); var buffer = VideoFrame.CreateWithSoftwareBitmap(bitmapBuffer); await frame.CopyToAsync(buffer); var innerBitmap = new WriteableBitmap(bitmapBuffer.PixelWidth, bitmapBuffer.PixelHeight); bitmapBuffer.CopyToBuffer(innerBitmap.PixelBuffer); var pixelsStream = innerBitmap.PixelBuffer.AsStream(); var transform = new BitmapTransform() { ScaledWidth = width, ScaledHeight = height, InterpolationMode = BitmapInterpolationMode.Cubic }; var decoder = await BitmapDecoder.CreateAsync(pixelsStream.AsRandomAccessStream()); var pixelData = await decoder.GetPixelDataAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.ColorManageToSRgb); var pixels = pixelData.DetachPixelData(); return(Normalize(pixels, mean, std, width, height)); }
string ProcessFrame(SoftwareBitmap bitmap) { if (bitmap == null) { return(null); } try { if (this.buffer == null) { this.buffer = new byte[4 * bitmap.PixelHeight * bitmap.PixelWidth]; } bitmap.CopyToBuffer(buffer.AsBuffer()); var zxingResult = ZXingQRCodeDecoder.DecodeBufferToQRCode( buffer, bitmap.PixelWidth, bitmap.PixelHeight, BitmapFormat.BGR32); if (zxingResult != null) { return(zxingResult.Text); } } catch { } return(null); }
private void FlipButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) { if (FilterImage == null) { SoftwareBitmap FlipImage = ComputerVisionProvider.FlipEffect(OriginImage, false); OriginImage.Dispose(); OriginImage = FlipImage; WriteableBitmap WBitmap = new WriteableBitmap(OriginImage.PixelWidth, OriginImage.PixelHeight); OriginImage.CopyToBuffer(WBitmap.PixelBuffer); Cropper.Source = WBitmap; } else { SoftwareBitmap FlipImage = ComputerVisionProvider.FlipEffect(OriginImage, false); OriginImage.Dispose(); OriginImage = FlipImage; SoftwareBitmap FilterFlipImage = ComputerVisionProvider.FlipEffect(FilterImage, false); FilterImage.Dispose(); FilterImage = FilterFlipImage; WriteableBitmap WBitmap = new WriteableBitmap(FilterImage.PixelWidth, FilterImage.PixelHeight); FilterImage.CopyToBuffer(WBitmap.PixelBuffer); Cropper.Source = WBitmap; } ResetButton.IsEnabled = true; }
/// <summary> /// Creates a <seealso cref="IRandomAccessStream"/> from <paramref name="softwareBitmap"/>. /// </summary> /// <param name="softwareBitmap">The bitmap which should be converted to a stream.</param> /// <returns>The generatede stream</returns> public static async Task <IRandomAccessStream> ToRandomAccesStreamAsync(this SoftwareBitmap softwareBitmap) { var stream = new InMemoryRandomAccessStream(); WriteableBitmap bitmap = new WriteableBitmap(softwareBitmap.PixelWidth, softwareBitmap.PixelHeight); softwareBitmap.CopyToBuffer(bitmap.PixelBuffer); BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream); Stream pixelStream = bitmap.PixelBuffer.AsStream(); byte[] pixels = new byte[pixelStream.Length]; await pixelStream.ReadAsync(pixels, 0, pixels.Length); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)bitmap.PixelWidth, (uint)bitmap.PixelHeight, 96, 96, pixels); await encoder.FlushAsync(); return(stream); }
// Converts a SoftwareBitmap to a PGM file, filtering out depth points with [upper, lower) private static byte[] ConvertFrameToPGM(SoftwareBitmap inputBitmap, int upper, int lower) { var w = inputBitmap.PixelWidth; var h = inputBitmap.PixelHeight; // PGM header var header = $"P5\n{w} {h}\n65535\n"; byte[] headerBuf = Encoding.ASCII.GetBytes(header); // Byte array of 16-bit pixel values byte[] inBuf = new byte[2 * w * h]; inputBitmap.CopyToBuffer(inBuf.AsBuffer()); // Filter pixels outside of range [200, 1000) for (var i = 0; i < inBuf.Length; i += 2) { ushort pixel = BitConverter.ToUInt16(inBuf, i); if (pixel < lower || pixel > upper) // invalid pixel { inBuf[i] = 0; inBuf[i + 1] = 0; } } // Outbound PGM buffer byte[] outBuf = new byte[inBuf.Length + headerBuf.Length]; // copy header System.Buffer.BlockCopy(headerBuf, 0, outBuf, 0, headerBuf.Length); // copy pixel values System.Buffer.BlockCopy(inBuf, 0, outBuf, headerBuf.Length, inBuf.Length); return(outBuf); }
private async void ProcessResults(Face[] faces, Emotion[] emotions, SoftwareBitmap swbitmap) { if (faces != null || swbitmap != null) { try { WriteableBitmap bitmap = new WriteableBitmap(swbitmap.PixelWidth, swbitmap.PixelHeight); swbitmap.CopyToBuffer(bitmap.PixelBuffer); List <FaceWithEmotions> result = new List <FaceWithEmotions>(); foreach (Face person in faces) { _seenAlready.Add(person.FaceId); Emotion personEmotion = null; int currentMinimum = 65000; if (emotions != null) { foreach (Emotion emo in emotions) { int diff = RectIntersectDifference(person.FaceRectangle, emo.FaceRectangle); if (diff < currentMinimum) { currentMinimum = diff; personEmotion = emo; } } } WriteableBitmap img = await CropAsync(bitmap, IncreaseSize(person.FaceRectangle, bitmap.PixelHeight, bitmap.PixelWidth)); result.Add(new FaceWithEmotions(person, personEmotion, img)); } while (_seenAlready.Count > 10) { _seenAlready.RemoveAt(0); } DetectedFaces?.Invoke(result.ToArray()); } catch (Exception ex) { Debug.WriteLine("ProcessResults exception : " + ex.Message); } } //_processingFace = false; // to avoid asking too many queries, the limit is 20 per minute // lets have a quick break after each query if (_threadPoolTimer == null) { _threadPoolTimer = ThreadPoolTimer.CreateTimer((source) => { _processingFace = false; _threadPoolTimer = null; }, TimeSpan.FromMilliseconds(3500)); } }
protected override async void OnNavigatedTo(NavigationEventArgs e) { try { if (e.Parameter is Tuple <Frame, object> Parameters) { FileControlNav = Parameters.Item1; PhotoDisplaySupport Item = Parameters.Item2 as PhotoDisplaySupport; OriginFile = (await Item.PhotoFile.GetStorageItem().ConfigureAwait(true)) as StorageFile; OriginImage = await Item.GenerateImageWithRotation().ConfigureAwait(true); OriginBackupImage = SoftwareBitmap.Copy(OriginImage); WriteableBitmap WBitmap = new WriteableBitmap(OriginImage.PixelWidth, OriginImage.PixelHeight); OriginImage.CopyToBuffer(WBitmap.PixelBuffer); Cropper.Source = WBitmap; UnchangeRegion = Cropper.CroppedRegion; await AddEffectsToPane().ConfigureAwait(false); } } catch (Exception ex) { ExceptionTracer.RequestBlueScreen(ex); } }
public IList <string> Decode(SoftwareBitmap image, bool tryMultipleBarcodes = false) { IList <string> txtContent = new List <string>(); WriteableBitmap bitmap = new WriteableBitmap(image.PixelWidth, image.PixelHeight); image.CopyToBuffer(bitmap.PixelBuffer); Result[] results = null; Result result = barcodeReader.Decode(bitmap); if (result != null) { results = new[] { result }; } if (results != null) { foreach (Result res in results) { txtContent.Add(res.Text); } } return(txtContent); }
/// <summary> /// Converts the given <see cref="SoftwareBitmap"/> to a <see cref="WriteableBitmap"/> and returns it. /// </summary> /// <param name="img">The <see cref="SoftwareBitmap"/> to convert.</param> public static WriteableBitmap ToWritableBitmap(SoftwareBitmap img) { WriteableBitmap wImg = new WriteableBitmap(img.PixelWidth, img.PixelHeight); img.CopyToBuffer(wImg.PixelBuffer); wImg.Invalidate(); return(wImg); }
public async Task SendStudyAsync(SoftwareBitmap images, IOutputStream stream) { using (var writer = new DataWriter(stream)) { Windows.Storage.Streams.Buffer buffer = new Windows.Storage.Streams.Buffer(1000); images.CopyToBuffer(buffer); writer.WriteBuffer(buffer); await writer.StoreAsync(); this.OnSentImage(); } }
public string Decode(SoftwareBitmap image) { var bitmap = new BinaryBitmap(new HybridBinarizer(new RGBLuminanceSource(); image.CopyToBuffer(bitmap.PixelBuffer); var result = qrCodeReader.decode(bitmap); if (result != null) { return result.Text; } return null; }
private async void PredictHangul(VideoFrame inputimage) { Stopwatch sw = new Stopwatch(); sw.Start(); // convert to bgr8 SoftwareBitmap bitgray8 = SoftwareBitmap.Convert(inputimage.SoftwareBitmap, BitmapPixelFormat.Gray8); var buff = new byte[64 * 64]; bitgray8.CopyToBuffer(buff.AsBuffer()); var fbuff = new float[4096]; for (int i = 0; i < 4096; i++) { fbuff[i] = (float)buff[i] / 255; } long[] shape = { 1, 4096 }; charInput.input00 = TensorFloat.CreateFromArray(shape, fbuff); var dummy = new float[1]; long[] dummy_shape = { }; charInput.keep_prob = TensorFloat.CreateFromArray(dummy_shape, dummy); //Evaluate the model charOuput = await charModel.EvaluateAsync(charInput); //Convert output to datatype IReadOnlyList <float> VectorImage = charOuput.output00.GetAsVectorView(); IList <float> ImageList = VectorImage.ToList(); //Display top results var topPred = ImageList.Select((value, index) => new { index, value }) .ToDictionary(pair => pair.index, pair => pair.value) .OrderByDescending(key => key.Value) .ToArray(); string topLabeltxt = ""; for (int i = 1; i < 6; i++) { var item = topPred[i]; Debug.WriteLine($"{item.Key}, {item.Value}, {charLabel[item.Key]}"); topLabeltxt += $"{charLabel[item.Key]} "; } numberLabel.Text = charLabel[topPred[0].Key]; topLabel.Text = topLabeltxt; Debug.WriteLine($"process time = {sw.Elapsed}"); }
private async Task SetImageViewSource(SoftwareBitmap softwareBitmap) { // Get byte array from software bitmap _byteData = await EncodedBytes(softwareBitmap, BitmapEncoder.JpegEncoderId); // Create writeable bitmap from software bitmap _imgSource = new WriteableBitmap(softwareBitmap.PixelWidth, softwareBitmap.PixelHeight); // Copy software bitmap buffer to writeable bitmap softwareBitmap.CopyToBuffer(_imgSource.PixelBuffer); // Set UI control source ImageView.Source = _imgSource; }
/// <summary> /// Get image source by steam. /// </summary> /// <param name="stream"> The steam. </param> /// <returns> Return image source. </returns> private static async Task <WriteableBitmap> GetImageSource(IRandomAccessStream stream) { // Display BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); using (SoftwareBitmap bitmap = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied)) { WriteableBitmap source = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight); bitmap.CopyToBuffer(source.PixelBuffer); return(source); } }
private async Task LoadImage(StorageFile file) { using (var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read)) { var decoder = await BitmapDecoder.CreateAsync(stream); bitmap = await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); var imgSource = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight); bitmap.CopyToBuffer(imgSource.PixelBuffer); } }
public void SetPixels(SoftwareBitmap bitmap) { if (bitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8) { throw new Exception("Unsupported file format (BGRA8 is supported) "); } byte[] imageBytes = new byte[4 * bitmap.PixelWidth * bitmap.PixelHeight]; bitmap.CopyToBuffer(imageBytes.AsBuffer()); this.SetPixels(imageBytes, bitmap.PixelWidth, bitmap.PixelHeight); }
/// <summary> /// Gets the current preview frame as a SoftwareBitmap, displays its properties in a TextBlock, and can optionally display the image /// in the UI and/or save it to disk as a jpg /// </summary> /// <returns></returns> 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; // Show the frame information FrameInfoTextBlock.Text = String.Format("{0}x{1} {2}", previewFrame.PixelWidth, previewFrame.PixelHeight, previewFrame.BitmapPixelFormat); // Add a simple green filter effect to the SoftwareBitmap if (EditPixelsCheckBox.IsChecked == true) { EditPixels(previewFrame); } // Show the frame (as is, no rotation is being applied) if (ShowFrameCheckBox.IsChecked == true) { // ************ The code below is commented out because of a bug ************** // that causes the image to be displayed incorrectly // // // Create a SoftwareBitmapSource to display the SoftwareBitmap to the user // var sbSource = new SoftwareBitmapSource(); // await sbSource.SetBitmapAsync(previewFrame); // // Display it in the Image control // PreviewFrameImage.Source = sbSource; // ***************************************************************************** // Copy the SoftwareBitmap to a WriteableBitmap to display it to the user var wb = new WriteableBitmap(previewFrame.PixelWidth, previewFrame.PixelHeight); previewFrame.CopyToBuffer(wb.PixelBuffer); // Display it in the Image control PreviewFrameImage.Source = wb; } // Save the frame (as is, no rotation is being applied) if (SaveFrameCheckBox.IsChecked == true) { await SaveSoftwareBitmapAsync(previewFrame); } } }
/// <summary> /// Captures a single frame from the running webcam stream and executes the FaceDetector on the image. If successful calls SetupVisualization to display the results. /// </summary> /// <returns>Async Task object returning true if the capture was successful and false if an exception occurred.</returns> private async Task <bool> TakeSnapshotAndFindFaces() { bool successful = true; try { if (this.currentState != ScenarioState.Streaming) { return(false); } WriteableBitmap displaySource = null; IList <DetectedFace> faces = null; // Create a VideoFrame object specifying the pixel format we want our capture image to be (NV12 bitmap in this case). // GetPreviewFrame will convert the native webcam frame into this format. const BitmapPixelFormat InputPixelFormat = BitmapPixelFormat.Nv12; using (VideoFrame previewFrame = new VideoFrame(InputPixelFormat, (int)this.videoProperties.Width, (int)this.videoProperties.Height)) { await this.mediaCapture.GetPreviewFrameAsync(previewFrame); // The returned VideoFrame should be in the supported NV12 format but we need to verify this. if (FaceDetector.IsBitmapPixelFormatSupported(previewFrame.SoftwareBitmap.BitmapPixelFormat)) { String filename = saveSoftwareBitmap(previewFrame.SoftwareBitmap); faces = await this.faceDetector.DetectFacesAsync(previewFrame.SoftwareBitmap); } else { // this.rootPage.NotifyUser("PixelFormat '" + InputPixelFormat.ToString() + "' is not supported by FaceDetector", NotifyType.ErrorMessage); } // Create a WritableBitmap for our visualization display; copy the original bitmap pixels to wb's buffer. // Note that WriteableBitmap doesn't support NV12 and we have to convert it to 32-bit BGRA. using (SoftwareBitmap convertedSource = SoftwareBitmap.Convert(previewFrame.SoftwareBitmap, BitmapPixelFormat.Bgra8)) { displaySource = new WriteableBitmap(convertedSource.PixelWidth, convertedSource.PixelHeight); convertedSource.CopyToBuffer(displaySource.PixelBuffer); } // Create our display using the available image and face results. this.SetupVisualization(displaySource, faces); } } catch (Exception ex) { // this.rootPage.NotifyUser(ex.ToString(), NotifyType.ErrorMessage); successful = false; } return(successful); }
public static WriteableBitmap SoftwareBitmapToWriteableBitmap(SoftwareBitmap softbitmap) { if (softbitmap == null) { return(null); } WriteableBitmap bitmap = new WriteableBitmap(softbitmap.PixelWidth, softbitmap.PixelHeight); softbitmap.CopyToBuffer(bitmap.PixelBuffer); return(bitmap); }
/// <summary> /// This acquires a new CameraFrame from the pool and copies the pixel data through the internal IBuffer. /// </summary> /// <param name="bmpFrame">The software bitmap from the camera</param> /// <returns>The camera frame (with the pixel data copied already)</returns> public CameraFrameInternal AcquireFrame(SoftwareBitmap bmpFrame, PixelFormat desiredPixelFormat) { lock (frameLock) { CameraFrameInternal frame = null; // convert the data format and get the data size - PixelFormat pixelFormat = PixelHelpers.ConvertFormat(bmpFrame.BitmapPixelFormat); if (pixelFormat != desiredPixelFormat) { BitmapPixelFormat bitmapPixelFormat = PixelHelpers.ConvertFormat(desiredPixelFormat); bmpFrame = SoftwareBitmap.Convert(bmpFrame, PixelHelpers.ConvertFormat(desiredPixelFormat)); } int dataSize = PixelHelpers.GetDataSize(bmpFrame.PixelWidth, bmpFrame.PixelHeight, desiredPixelFormat); if (freeCameraFrames.Count > 0) { // find a frame with the required size buffer for (LinkedListNode <CameraFrameInternal> node = freeCameraFrames.First; node != null; node = node.Next) { // check if the pixel data array is the correct length if (node.Value != null && node.Value?.PixelData?.Length == dataSize) { // remove the frame from the free list freeCameraFrames.Remove(node); // add the frame to the used list (need to create a new node) frame = node.Value; usedCameraFrames.AddFirst(frame); } } } // if there were no free frames, create a new one if (frame == null) { frame = new CameraFrameInternal(this); frame.PixelData = new byte[dataSize]; frame.PixelDataBuffer = frame.PixelData.AsBuffer(); // add the new frame to the used camera frames list usedCameraFrames.AddFirst(frame); } // copy the pixel data to the frame through the internal IBuffer bmpFrame.CopyToBuffer(frame.PixelDataBuffer); return(frame); } }
private byte[] ConvertFrameToByteArray(SoftwareBitmap bitmap) { byte[] bytes; WriteableBitmap newBitmap = new WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight); bitmap.CopyToBuffer(newBitmap.PixelBuffer); using (Stream stream = newBitmap.PixelBuffer.AsStream()) using (MemoryStream memoryStream = new MemoryStream()) { stream.CopyTo(memoryStream); bytes = memoryStream.ToArray(); } return(bytes); }
private static void OnThumbnailComplete(IImageProcessor processor, SoftwareBitmap softwareBitmap) { // Note: intentionally not awaited. We just deliver the thumbnail Bitmaps to the UI thread. TaskUtilities.RunOnDispatcherThreadAsync(() => { WriteableBitmap writeableBitmap = new WriteableBitmap(softwareBitmap.PixelWidth, softwareBitmap.PixelHeight); softwareBitmap.CopyToBuffer(writeableBitmap.PixelBuffer); writeableBitmap.Invalidate(); processor.ThumbnailImagSource = writeableBitmap; // The thumbnail image has been copied, so we can destroy the Bitmap. softwareBitmap.Dispose(); }); }