public async Task<BitmapSource> LoadImageAsync(Stream imageStream, Uri uri) { if (imageStream == null) { return null; } var stream = new InMemoryRandomAccessStream(); imageStream.CopyTo(stream.AsStreamForWrite()); stream.Seek(0); BitmapImage bitmap = new BitmapImage(); await bitmap.SetSourceAsync(stream); // convert to a writable bitmap so we can get the PixelBuffer back out later... // in case we need to edit and/or re-encode the image. WriteableBitmap bmp = new WriteableBitmap(bitmap.PixelHeight, bitmap.PixelWidth); stream.Seek(0); bmp.SetSource(stream); List<Byte> allBytes = new List<byte>(); byte[] buffer = new byte[4000]; int bytesRead = 0; while ((bytesRead = await imageStream.ReadAsync(buffer, 0, 4000)) > 0) { allBytes.AddRange(buffer.Take(bytesRead)); } DataContainerHelper.Instance.WriteableBitmapToStorageFile(bmp, uri); return bmp; }
/// <summary> /// Applys a blur to a UI element /// </summary> /// <param name="sourceElement">UIElement to blur, generally an Image control, but can be anything</param> /// <param name="blurAmount">Level of blur to apply</param> /// <returns>Blurred UIElement as BitmapImage</returns> public static async Task<BitmapImage> BlurElementAsync(this UIElement sourceElement, float blurAmount = 2.0f) { if (sourceElement == null) return null; var rtb = new RenderTargetBitmap(); await rtb.RenderAsync(sourceElement); var buffer = await rtb.GetPixelsAsync(); var array = buffer.ToArray(); var displayInformation = DisplayInformation.GetForCurrentView(); using (var stream = new InMemoryRandomAccessStream()) { var pngEncoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream); pngEncoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, (uint) rtb.PixelWidth, (uint) rtb.PixelHeight, displayInformation.RawDpiX, displayInformation.RawDpiY, array); await pngEncoder.FlushAsync(); stream.Seek(0); var canvasDevice = new CanvasDevice(); var bitmap = await CanvasBitmap.LoadAsync(canvasDevice, stream); var renderer = new CanvasRenderTarget(canvasDevice, bitmap.SizeInPixels.Width, bitmap.SizeInPixels.Height, bitmap.Dpi); using (var ds = renderer.CreateDrawingSession()) { var blur = new GaussianBlurEffect { BlurAmount = blurAmount, Source = bitmap }; ds.DrawImage(blur); } stream.Seek(0); await renderer.SaveAsync(stream, CanvasBitmapFileFormat.Png); var image = new BitmapImage(); await image.SetSourceAsync(stream); return image; } }
public async void Quadraliteral(StorageFile file, IList<WF.Point> points) { var data = await FileIO.ReadBufferAsync(file); OpencvImageProcess opencv = new OpencvImageProcess(); // create a stream from the file var ms = new InMemoryRandomAccessStream(); var dw = new DataWriter(ms); dw.WriteBuffer(data); await dw.StoreAsync(); ms.Seek(0); // find out how big the image is, don't need this if you already know var bm = new BitmapImage(); await bm.SetSourceAsync(ms); // create a writable bitmap of the right size var wb = new WriteableBitmap(bm.PixelWidth, bm.PixelHeight); ms.Seek(0); // load the writable bitpamp from the stream await wb.SetSourceAsync(ms); Bitmap bmp = (Bitmap)wb; var wb1 = opencv.GetImageCorners(wb); wb1.Invalidate(); //wb.Invalidate(); // define quadrilateral's corners //List<IntPoint> corners = new List<IntPoint>(); //foreach (var point in points) //{ // corners.Add(new IntPoint((int)point.X, (int)point.Y)); //} //// create filter //var filter = // new SimpleQuadrilateralTransformation(corners); //// apply the filter //Bitmap newImage = filter.Apply(bmp); //wb = (WriteableBitmap)newImage; var f = await this.WriteableBitmapToStorageFile(wb1, FileFormat.Jpeg); }
public async Task Should_detect_corrupt_data() { using (var input = new InMemoryRandomAccessStream()) { await CopyData(input, "IO.HashedBlockStream.bin"); input.Seek(200); await input.WriteAsync(CryptographicBuffer.GenerateRandom(8)); input.Seek(0); await Assert.ThrowsAsync<InvalidDataException>( () => HashedBlockFileFormat.Read(input)); } }
private async void GetBijinPicture() { var date = DateTime.Now; var url = "http://www.bijint.com/jp/tokei_images/" + date.ToString("HHmm") + ".jpg"; HttpClient client = new HttpClient(); client.DefaultRequestHeaders.Referrer = new Uri("http://www.bijint.com/jp/"); using (var strm = await client.GetStreamAsync(new Uri(url))) { // BitmapImageインスタンスへはStream型をそのまま読み込ませることができないため、 // InMemoryRandomAccessStreamへソースストリームをコピーする InMemoryRandomAccessStream ims = new InMemoryRandomAccessStream(); var output = ims.GetOutputStreamAt(0); await RandomAccessStream.CopyAsync(strm.AsInputStream(), output); // BitmapImageへソースを設定し、Imageコントロールで表示させる var bitmap = new BitmapImage(); bitmap.SetSource(ims); image.Source = bitmap; // Save用にbyte配列に保存 ims.Seek(0); imageBuffer = new byte[ims.Size]; IBuffer ibuffer = imageBuffer.AsBuffer(); await ims.ReadAsync(ibuffer, (uint)ims.Size, InputStreamOptions.None); } }
public static async Task<IRandomAccessStream> RenderToRandomAccessStream(this UIElement element) { var rtb = new RenderTargetBitmap(); await rtb.RenderAsync(element); IBuffer pixelBuffer = await rtb.GetPixelsAsync(); byte[] pixels = pixelBuffer.ToArray(); // Useful for rendering in the correct DPI DisplayInformation displayInformation = DisplayInformation.GetForCurrentView(); var stream = new InMemoryRandomAccessStream(); BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied, (uint)rtb.PixelWidth, (uint)rtb.PixelHeight, displayInformation.RawDpiX, displayInformation.RawDpiY, pixels); await encoder.FlushAsync(); stream.Seek(0); return stream; }
async void BeginReadCCTV() { IsBeginRead = true; ISExit = false; tblCCTV cctvinfo=null; await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { cctvinfo = this.DataContext as tblCCTV; }); // client=new HttpClient(); while (!ISExit) { try { Uri uri = new Uri("http://192.192.161.3/" + cctvinfo.REF_CCTV_ID.Trim() + ".jpg?" + rnd.Next(), UriKind.Absolute); using (httpClient = new HttpClient()) { httpClient.Timeout = TimeSpan.FromSeconds(0.5); var contentBytes = await httpClient.GetByteArrayAsync(uri); var ims = new InMemoryRandomAccessStream(); var dataWriter = new DataWriter(ims); dataWriter.WriteBytes(contentBytes); await dataWriter.StoreAsync(); //ims.seak 0 ims.Seek(0 ); await Dispatcher.RunAsync( Windows.UI.Core.CoreDispatcherPriority.Normal,()=> { BitmapImage bitmap = new BitmapImage(); bitmap.SetSource(ims ); this.imgCCTV.Source = bitmap; imginx = (imginx + 1) % 90000; this.RunFrameRate.Text = imginx.ToString(); }); } } catch (Exception ex) { // this.textBlock1.Text = ex.Message; } // BitmapImage img = new BitmapImage(); // img.SetSource(stream); } }
public async Task Should_read_correctly_formatted_stream() { using (var input = new InMemoryRandomAccessStream()) using (var expectedData = TestFiles.Read("IO.HashedBlockStream.Content.bin")) { await CopyData(input, "IO.HashedBlockStream.bin"); input.Seek(0); expectedData.Seek(0); using (var actualData = await HashedBlockFileFormat.Read(input)) { Assert.Equal(expectedData.Size, (ulong)actualData.Length); var actual = new byte[1024]; var expected = WindowsRuntimeBuffer.Create(actual.Length); while (true) { expected = await expectedData.ReadAsync(expected); var read = await actualData.ReadAsync(actual, 0, actual.Length); Assert.Equal(expected.Length, (uint)read); if (read == 0) break; Assert.Equal(expected.ToArray(), actual.Take(read)); } } } }
public async Task Should_produce_read_bytes_hash(int bufferSize) { var data = CryptographicBuffer.GenerateRandom(2048); var expected = HashAlgorithmProvider .OpenAlgorithm(HashAlgorithmNames.Sha256) .HashData(data); using (var file = new InMemoryRandomAccessStream()) { await file.WriteAsync(data); file.Seek(0); var buffer = WindowsRuntimeBuffer.Create(bufferSize); using (var hashed = new HashedInputStream(file)) { for (var i = 0; i < 8; i++) { await hashed.ReadAsync( buffer, buffer.Capacity); } var hash = hashed.GetHashAndReset(); Assert.Equal(expected.ToArray(), hash.ToArray()); } } }
public static async Task <Windows.UI.Xaml.Media.Imaging.BitmapImage> GetBitmapAsync(byte[] data) { if (data is null) { return(null); } var bitmapImage = new Windows.UI.Xaml.Media.Imaging.BitmapImage(); using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream()) { using (var writer = new Windows.Storage.Streams.DataWriter(stream)) { writer.WriteBytes(data); await writer.StoreAsync(); await writer.FlushAsync(); writer.DetachStream(); } stream.Seek(0); await bitmapImage.SetSourceAsync(stream); } return(bitmapImage); }
/// <summary> /// Decrypts the specified input stream. /// </summary> /// <param name="input">The input stream.</param> /// <param name="masterKey">The master key.</param> /// <param name="masterSeed">The master seed.</param> /// <param name="encryptionIV">The encryption initialization vector.</param> /// <returns>The decrypted buffer.</returns> /// <exception cref="ArgumentNullException"> /// The <paramref name="input"/>, <paramref name="masterSeed"/>, <paramref name="masterKey"/> /// and <paramref name="encryptionIV"/> cannot be <c>null</c>. /// </exception> public static async Task<IInputStream> Decrypt(IRandomAccessStream input, IBuffer masterKey, IBuffer masterSeed, IBuffer encryptionIV) { if (input == null) throw new ArgumentNullException("input"); if (masterSeed == null) throw new ArgumentNullException("masterSeed"); if (masterKey == null) throw new ArgumentNullException("masterKey"); if (encryptionIV == null) throw new ArgumentNullException("encryptionIV"); var sha = HashAlgorithmProvider .OpenAlgorithm(HashAlgorithmNames.Sha256) .CreateHash(); sha.Append(masterSeed); sha.Append(masterKey); var seed = sha.GetValueAndReset(); var aes = SymmetricKeyAlgorithmProvider .OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7) .CreateSymmetricKey(seed); var buffer = WindowsRuntimeBuffer.Create( (int)(input.Size - input.Position)); buffer = await input.ReadAsync(buffer, buffer.Capacity); buffer = CryptographicEngine.Decrypt(aes, buffer, encryptionIV); var stream = new InMemoryRandomAccessStream(); await stream.WriteAsync(buffer); stream.Seek(0); return stream; }
private async void Page_Loaded(object sender, RoutedEventArgs e) { await InitializeQrCode(); var imgProp = new ImageEncodingProperties { Subtype = "BMP", Width = 600, Height = 800 }; var bcReader = new BarcodeReader(); while (true) { var stream = new InMemoryRandomAccessStream(); await _mediaCapture.CapturePhotoToStreamAsync(imgProp, stream); stream.Seek(0); var wbm = new WriteableBitmap(600, 800); await wbm.SetSourceAsync(stream); var result = bcReader.Decode(wbm); if (result != null) { var msgbox = new MessageDialog(result.Text); await msgbox.ShowAsync(); } } }
public async static Task<Uri> ToQrDataUri(this ISdp sdp, int width, int height) { var qrCodeWriter = new QRCodeWriter(); var bitMatrix = qrCodeWriter.encode(sdp.ToString(), ZXing.BarcodeFormat.QR_CODE, width, height); using (var canvasRenderTarget = new CanvasRenderTarget(CanvasDevice.GetSharedDevice(), 500, 500, 96)) { using (var drawingSession = canvasRenderTarget.CreateDrawingSession()) { for (var y = 0; y < height; y++) { for (var x = 0; x < width; x++) { drawingSession.DrawRectangle(x, y, 1, 1, bitMatrix.get(x, y) ? Color.FromArgb(0, 0, 0, 0) : Color.FromArgb(255, 255, 255, 255)); } } } using (var inMemoryRandomAccessStream = new InMemoryRandomAccessStream()) { await canvasRenderTarget.SaveAsync(inMemoryRandomAccessStream, CanvasBitmapFileFormat.Png); inMemoryRandomAccessStream.Seek(0); var buffer = new byte[inMemoryRandomAccessStream.Size]; await inMemoryRandomAccessStream.ReadAsync(buffer.AsBuffer(), (uint)inMemoryRandomAccessStream.Size, InputStreamOptions.None); return new Uri($"data:image/png;base64,{Convert.ToBase64String(buffer)}"); } } }
public static async Task <byte[]> TranscodeImageFile(byte[] data) { using (IRandomAccessStream stream = data.AsBuffer().AsStream().AsRandomAccessStream()) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); var memStream = new Windows.Storage.Streams.InMemoryRandomAccessStream(); var outstream = new InMemoryRandomAccessStream(); BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(memStream, decoder); encoder.BitmapTransform.ScaledWidth = 320; encoder.BitmapTransform.ScaledHeight = 240; await encoder.FlushAsync(); memStream.Seek(0); await RandomAccessStream.CopyAsync(memStream, outstream); memStream.Dispose(); var outbuff = new byte[outstream.Size]; await outstream.WriteAsync(outbuff.AsBuffer()); return(outbuff); } }
public async Task<byte[]> ConvertToGrayScale(byte[] imageBytes, int height, int width) { using (InMemoryRandomAccessStream rasStream = new InMemoryRandomAccessStream()) { await rasStream.WriteAsync(imageBytes.AsBuffer()); var decoder = await BitmapDecoder.CreateAsync(rasStream); var pixelData = await decoder.GetPixelDataAsync(); var pixels = pixelData.DetachPixelData(); if (_filter == null) _filter = new ImageFilter(); await _filter.ToGrayScale(pixels.AsBuffer()); BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, rasStream); encoder.SetPixelData(decoder.BitmapPixelFormat, BitmapAlphaMode.Ignore, (uint)width, (uint)height, decoder.DpiX, decoder.DpiY, pixels); await encoder.FlushAsync(); using (BinaryReader br = new BinaryReader(rasStream.AsStreamForRead())) { rasStream.Seek(0); return br.ReadBytes((int)rasStream.AsStreamForRead().Length); } } }
private async void ImageUpload(string deviceId) { try { using (Windows.Storage.Streams.InMemoryRandomAccessStream captureStream = new Windows.Storage.Streams.InMemoryRandomAccessStream()) { await mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), captureStream); await captureStream.FlushAsync(); captureStream.Seek(0); // Drops file onto device file system for debugging only #if DEBUG IStorageFile photoFile = await KnownFolders.PicturesLibrary.CreateFileAsync("Timelapse.jpg", CreationCollisionOption.ReplaceExisting); ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg(); await mediaCapture.CapturePhotoToStorageFileAsync(imageProperties, photoFile); #endif LoggingService.Log("ImageUploadService Upload starting"); ImageUploadService.Upload(deviceId, captureStream); LoggingService.Log("ImageUploadService Upload done"); } } catch (Exception ex) { LoggingService.Error($"Image capture or upload failed ", ex); } }
public async void ProccesImage(StorageFile imageFile) { var data = await FileIO.ReadBufferAsync(imageFile); // create a stream from the file var ms = new InMemoryRandomAccessStream(); var dw = new DataWriter(ms); dw.WriteBuffer(data); await dw.StoreAsync(); ms.Seek(0); // find out how big the image is, don't need this if you already know var bm = new BitmapImage(); await bm.SetSourceAsync(ms); // create a writable bitmap of the right size var wb = new WriteableBitmap(bm.PixelWidth, bm.PixelHeight); ms.Seek(0); // load the writable bitpamp from the stream await wb.SetSourceAsync(ms); Bitmap bmp = (Bitmap)wb; //var filter1 = Grayscale.CommonAlgorithms.BT709; //bmp = filter1.Apply(bmp); wb = (WriteableBitmap)bmp; var file = await this.WriteableBitmapToStorageFile(wb, FileFormat.Jpeg); }
async Task<WriteableBitmap> ResizeImage(WriteableBitmap baseWriteBitmap, uint width, uint height) { Stream stream = baseWriteBitmap.PixelBuffer.AsStream(); byte[] pixels = new byte[(uint)stream.Length]; await stream.ReadAsync(pixels, 0, pixels.Length); var inMemoryRandomStream = new InMemoryRandomAccessStream(); var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, inMemoryRandomStream); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)baseWriteBitmap.PixelWidth, (uint)baseWriteBitmap.PixelHeight, 96, 96, pixels); await encoder.FlushAsync(); var transform = new BitmapTransform { ScaledWidth = width, ScaledHeight = height }; inMemoryRandomStream.Seek(0); var decoder = await BitmapDecoder.CreateAsync(inMemoryRandomStream); var pixelData = await decoder.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage); var sourceDecodedPixels = pixelData.DetachPixelData(); var inMemoryRandomStream2 = new InMemoryRandomAccessStream(); var encoder2 = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, inMemoryRandomStream2); encoder2.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, width, height, 96, 96, sourceDecodedPixels); await encoder2.FlushAsync(); inMemoryRandomStream2.Seek(0); var bitmap = new WriteableBitmap((int)width, (int)height); await bitmap.SetSourceAsync(inMemoryRandomStream2); return bitmap; }
public static async Task<IBuffer> SaveAsPngIntoBufferAsync(this Canvas canvas, double _scaleFactor, int dpiForImage = 200) { //string currentresolution = Window.Current.Bounds.Width * scaleFactor + "*" + Window.Current.Bounds.Height * scaleFactor; RenderTargetBitmap renderTargetBitmap = new RenderTargetBitmap(); await renderTargetBitmap.RenderAsync(canvas); var pixels = await renderTargetBitmap.GetPixelsAsync(); using (IRandomAccessStream stream = new InMemoryRandomAccessStream()) { var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, stream); byte[] bytes = pixels.ToArray(); await CoreWindow.GetForCurrentThread().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)(canvas.ActualWidth * _scaleFactor), (uint)(canvas.ActualHeight * _scaleFactor), dpiForImage, dpiForImage, bytes); }); await encoder.FlushAsync(); stream.Seek(0); var buffer = WindowsRuntimeBuffer.Create((int)stream.Size); await stream.ReadAsync(buffer, (uint)stream.Size, InputStreamOptions.None); return buffer; } }
public async Task<BitmapImage> Base64ToBitmapImage(string base64String) { BitmapImage img = new BitmapImage(); if (string.IsNullOrEmpty(base64String)) return img; using (var ims = new InMemoryRandomAccessStream()) { byte[] bytes = Convert.FromBase64String(base64String); base64String = ""; using (DataWriter dataWriter = new DataWriter(ims)) { dataWriter.WriteBytes(bytes); bytes = null; await dataWriter.StoreAsync(); ims.Seek(0); await img.SetSourceAsync(ims); //not in RC return img; } } }
/// <summary> /// This function rotates the passed WriteableBitmap clockwise or counter-clockwise /// </summary> /// <param name="baseWriteBitmap"> WriteableBitmap to be rotated </param> /// <param name="width"> Width of the WriteableBitmap </param> /// <param name="height"> Height of the WriteableBitmap </param> /// <param name="position"> If 'right', it rotates clockwise, if 'left' - counterclockwise </param> public async Task<WriteableBitmap> RotateImage(WriteableBitmap baseWriteBitmap, uint width, uint height, string position) { // Get the pixel buffer of the writable bitmap in bytes Stream stream = baseWriteBitmap.PixelBuffer.AsStream(); byte[] pixels = new byte[(uint)stream.Length]; await stream.ReadAsync(pixels, 0, pixels.Length); //Encoding the data of the PixelBuffer we have from the writable bitmap var inMemoryRandomStream = new InMemoryRandomAccessStream(); var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, inMemoryRandomStream); encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)baseWriteBitmap.PixelWidth, (uint)baseWriteBitmap.PixelHeight, 96, 96, pixels); await encoder.FlushAsync(); // At this point we have an encoded image in inMemoryRandomStream // We apply the transform and decode BitmapRotation rotateTo = BitmapRotation.None; if (position == "right") { rotateTo = BitmapRotation.Clockwise90Degrees; } else if (position == "left") { rotateTo = BitmapRotation.Clockwise270Degrees; } var transform = new BitmapTransform { ScaledWidth = width, ScaledHeight = height, Rotation = rotateTo }; inMemoryRandomStream.Seek(0); var decoder = await BitmapDecoder.CreateAsync(inMemoryRandomStream); var pixelData = await decoder.GetPixelDataAsync( BitmapPixelFormat.Bgra8, BitmapAlphaMode.Straight, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage); // An array containing the decoded image data var sourceDecodedPixels = pixelData.DetachPixelData(); // We encode the image buffer again: // Encoding data var inMemoryRandomStream2 = new InMemoryRandomAccessStream(); var encoder2 = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, inMemoryRandomStream2); encoder2.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, height, width, 96, 96, sourceDecodedPixels); await encoder2.FlushAsync(); inMemoryRandomStream2.Seek(0); // Finally the resized WritableBitmap var bitmap = new WriteableBitmap((int)width, (int)height); await bitmap.SetSourceAsync(inMemoryRandomStream2); return bitmap; }
/// <summary> /// 将Stream转换成IRandomAccessStream /// </summary> /// <param name="stream"></param> /// <returns></returns> public static async Task<IRandomAccessStream> ToRandomAccessStream(this Stream stream) { var buffer = CryptographicBuffer.CreateFromByteArray(stream.GetBytes()); InMemoryRandomAccessStream inme = new InMemoryRandomAccessStream(); await inme.WriteAsync(buffer); inme.Seek(0); return inme; }
private async void mjpeg_FrameReady(object sender, FrameReadyEventArgs e) { InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream(); await stream.WriteAsync(e.FrameBuffer); stream.Seek(0); _bmp.SetSource(stream); image.Source = _bmp; }
private async void ViewFinder_OnTapped() { ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg(); var stream = new InMemoryRandomAccessStream(); await captureManager.CapturePhotoToStreamAsync(imageProperties, stream); _bitmap = new WriteableBitmap(300, 300); stream.Seek(0); await _bitmap.SetSourceAsync(stream); stream.Seek(0); var buffer = new global::Windows.Storage.Streams.Buffer((uint)stream.Size); stream.ReadAsync(buffer, (uint)stream.Size, InputStreamOptions.None); await captureManager.StopPreviewAsync(); }
public static async Task<IRandomAccessStream> AsRandomAccessStreamAsync(this byte[] array) { var stream = new InMemoryRandomAccessStream(); await stream.WriteAsync(array.AsBuffer()); stream.Seek(0); return stream; }
public static IRandomAccessStream AsRandomAccessStream(this Stream stream) { // This is bad, as we are reading the whole input stream in memory, but better than nothing. var randomStream = new InMemoryRandomAccessStream(); stream.CopyTo(randomStream.AsStream()); randomStream.Seek(0); return randomStream; }
private async void client_ColorFrameReady(object sender, ColorFrameData e) { InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream(); await stream.WriteAsync(e.PixelBuffer); stream.Seek(0); _colorBitmap.SetSource(stream); this.Color.Source = _colorBitmap; }
public static async Task<InMemoryRandomAccessStream> ConvertToInMemoryStream(byte[] arr) { var randomAccessStream = new InMemoryRandomAccessStream(); await randomAccessStream.WriteAsync(arr.AsBuffer()); randomAccessStream.Seek(0); await randomAccessStream.FlushAsync(); // TODO: don't sure it's needed return randomAccessStream; }
private async Task WriteImage() { if (currentImageIndex == -1) { return; } try { await CreateFolder(); StorageFile outputFile = await projectFolder.CreateFileAsync(filesNames.ElementAt(currentImageIndex), CreationCollisionOption.ReplaceExisting); var stream = (await outputFile.OpenStreamForWriteAsync()).AsRandomAccessStream(); Guid encoderId; if (filesNames.ElementAt(currentImageIndex).Contains(".jpg")) { encoderId = Windows.Graphics.Imaging.BitmapEncoder.JpegEncoderId; } var inputStream = await files.ElementAt(currentImageIndex).OpenStreamForReadAsync(); BitmapDecoder decoder = await BitmapDecoder.CreateAsync(inputStream.AsRandomAccessStream()); var inputProperties = decoder.BitmapProperties; var memStream = new Windows.Storage.Streams.InMemoryRandomAccessStream(); BitmapEncoder encoder2 = await BitmapEncoder.CreateForTranscodingAsync(memStream, decoder); WriteImageMetadata(encoder2, inputProperties); await encoder2.FlushAsync(); memStream.Seek(0); stream.Seek(0); stream.Size = 0; await RandomAccessStream.CopyAsync(memStream, stream); memStream.Dispose(); stream.Dispose(); inputStream.Dispose(); if (latitude.Text != null) { await GeotagHelper.SetGeotagAsync(outputFile, myLocation); } } catch (Exception err) { switch (err.HResult) { case unchecked ((int)0x88982F41): // WINCODEC_ERR_PROPERTYNOTSUPPORTED // The file format does not support this property. break; default: break; } } }
public void SetSource(byte[] bytes) { var ms = new InMemoryRandomAccessStream(); ms.WriteAsync(bytes.AsBuffer()); ms.FlushAsync().AsTask().Wait(); ms.Seek(0); bitmap = new BitmapImage(); bitmap.SetSource(ms); }
private static async Task<InMemoryRandomAccessStream> ByteArrayToRandomAccessStream(byte[] tile) { var stream = new InMemoryRandomAccessStream(); var dataWriter = new DataWriter(stream); dataWriter.WriteBytes(tile); await dataWriter.StoreAsync(); stream.Seek(0); return stream; }
private async void SaveSoftwareBitmapToFile(SoftwareBitmap softwareBitmap) { using (var memStream = new Windows.Storage.Streams.InMemoryRandomAccessStream()) // (IRandomAccessStream stream = await outputFile.OpenAsync(FileAccessMode.ReadWrite)) { // Create an encoder with the desired format BitmapEncoder encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, memStream); // Set the software bitmap encoder.SetSoftwareBitmap(softwareBitmap); // Set additional encoding parameters, if needed encoder.BitmapTransform.ScaledWidth = 320; encoder.BitmapTransform.ScaledHeight = 240; //encoder.BitmapTransform.Rotation = Windows.Graphics.Imaging.BitmapRotation.Clockwise90Degrees; encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant; encoder.IsThumbnailGenerated = true; try { await encoder.FlushAsync(); // UploadImage // An array containing the decoded image data, which could be modified before being displayed IBuffer buff = new Windows.Storage.Streams.Buffer((uint)memStream.Size); memStream.Seek(0); IBuffer c = await memStream.ReadAsync(buff, buff.Capacity, InputStreamOptions.None); //WindowsRuntimeBufferExtensions.ToArray http://cctvapi.azurewebsites.net //await UploadImage("http://localhost:56407/Api/File/", buff.ToArray()) ; http://peopledetectionapiapp.azurewebsites.net/swagger/ui/index //await UploadImage("http://cctvapi.azurewebsites.net/Api/File/", buff.ToArray()); await UploadImage("http://peopledetectionapiapp.azurewebsites.net/swagger/ui/index", buff.ToArray()); } catch (Exception err) { switch (err.HResult) { case unchecked ((int)0x88982F81): //WINCODEC_ERR_UNSUPPORTEDOPERATION // If the encoder does not support writing a thumbnail, then try again // but disable thumbnail generation. encoder.IsThumbnailGenerated = false; break; default: throw err; } } if (encoder.IsThumbnailGenerated == false) { await encoder.FlushAsync(); } } }
public static async Task<IRandomAccessStream> ToRandomAccessStream(this MemoryStream memoryStream) { var tile = memoryStream.ToArray(); var stream = new InMemoryRandomAccessStream(); var dataWriter = new DataWriter(stream); dataWriter.WriteBytes(tile); await dataWriter.StoreAsync(); stream.Seek(0); return stream; }
private async Task LoadBrushAsync() { using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream()) { await ImageSource.ToStream(stream, BitmapEncoder.BmpEncoderId); stream.Seek(0); ImageEffectsBrush.LoadImageFromStream(stream); } }
private async void Run() { await _HubConnection.Start(); var cam = new MediaCapture(); await cam.InitializeAsync(new MediaCaptureInitializationSettings() { MediaCategory = MediaCategory.Media, StreamingCaptureMode = StreamingCaptureMode.Video }); _Sensor.MotionDetected += async (int pinNum) => { var stream = new InMemoryRandomAccessStream(); Stream imageStream = null; try { await Task.Factory.StartNew(async () => { _Sensor.IsActive = false; await cam.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream); stream.Seek(0); imageStream = stream.AsStream(); imageStream.Seek(0, SeekOrigin.Begin); string imageUrl = await NotificationHelper.UploadImageAsync(imageStream); switch (await OxfordHelper.IdentifyAsync(imageUrl)) { case AuthenticationResult.IsOwner: // open the door MotorController.PWM(26); break; case AuthenticationResult.Unkown: // send notification to the owner NotificationHelper.NotifyOwnerAsync(imageUrl); break; case AuthenticationResult.None: default: break; } _Sensor.IsActive = true; }); } finally { if (stream != null) stream.Dispose(); if (imageStream != null) imageStream.Dispose(); } }; }
private async void btnTakePhoto_Click(object sender, RoutedEventArgs e) { btnTakePhoto.IsEnabled = false; btnStartPreview.IsEnabled = false; InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream(); await mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream); stream.Seek(0); BitmapImage bitmap = new BitmapImage(); bitmap.SetSource(stream); captureImage.Source = bitmap; stream.Seek(0); Stream st = stream.AsStream(); if (isPreviewing == true) await mediaCapture.StopPreviewAsync(); isPreviewing = false; previewElement.Visibility = Visibility.Collapsed; progring.IsActive = true; try { EmotionServiceClient emotionServiceClient = new EmotionServiceClient("12345678901234567890123456789012"); // replace 12345678901234567890123456789012 with your key taken from https://www.projectoxford.ai/Subscription/ emotionResult = await emotionServiceClient.RecognizeAsync(st); } catch { } progring.IsActive = false; if ((emotionResult != null) && (emotionResult.Length > 0)) { emo.Clear(); emo.Add(emotionResult[0]); this.DataContext = emo.ElementAt(0); } btnStartPreview.IsEnabled = true; btnTakePhoto.IsEnabled = true; }
public async Task <WriteableBitmap> GetImageSourceAsync() { WriteableBitmap bmp = new WriteableBitmap(ImageBitmap.PixelWidth, ImageBitmap.PixelHeight); using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream()) { await ImageBitmap.ToStream(stream, BitmapEncoder.BmpEncoderId); stream.Seek(0); await bmp.SetSourceAsync(stream); } return(bmp); }
// </SnippetCreateSoftwareBitmapFromSurface> // <SnippetTranscodeImageFile> private async void TranscodeImageFile(StorageFile imageFile) { using (IRandomAccessStream fileStream = await imageFile.OpenAsync(FileAccessMode.ReadWrite)) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream); var memStream = new Windows.Storage.Streams.InMemoryRandomAccessStream(); BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(memStream, decoder); encoder.BitmapTransform.ScaledWidth = 320; encoder.BitmapTransform.ScaledHeight = 240; await encoder.FlushAsync(); memStream.Seek(0); fileStream.Seek(0); fileStream.Size = 0; await RandomAccessStream.CopyAsync(memStream, fileStream); memStream.Dispose(); } }
private async Task ImageUpdate(bool isCommand) { DateTime currentTime = DateTime.UtcNow; // Just incase - stop code being called while photo already in progress if (this.cameraBusy) { return; } this.cameraBusy = true; this.displayGpioPin.Write(GpioPinValue.High); // Check that enough time has passed for picture to be taken if ((currentTime - this.imageLastCapturedAtUtc) < this.debounceTimeout) { this.displayOffTimer.Change(this.timerPeriodDetectIlluminated, this.timerPeriodInfinite); return; } this.imageLastCapturedAtUtc = currentTime; try { ImagePrediction imagePrediction; using (Windows.Storage.Streams.InMemoryRandomAccessStream captureStream = new Windows.Storage.Streams.InMemoryRandomAccessStream()) { this.mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), captureStream).AsTask().Wait(); captureStream.FlushAsync().AsTask().Wait(); captureStream.Seek(0); IStorageFile photoFile = await KnownFolders.PicturesLibrary.CreateFileAsync(ImageFilename, CreationCollisionOption.ReplaceExisting); ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg(); await this.mediaCapture.CapturePhotoToStorageFileAsync(imageProperties, photoFile); switch (modelType) { case ModelType.Classification: imagePrediction = await this.customVisionClient.ClassifyImageAsync(this.projectId, this.modelPublishedName, captureStream.AsStreamForRead()); break; case ModelType.Detection: imagePrediction = await this.customVisionClient.DetectImageAsync(this.projectId, this.modelPublishedName, captureStream.AsStreamForRead()); break; default: throw new ArgumentException("ModelType Invalid"); } Debug.WriteLine($"Prediction count {imagePrediction.Predictions.Count}"); } JObject telemetryDataPoint = new JObject(); LoggingFields imageInformation = new LoggingFields(); imageInformation.AddDateTime("TakenAtUTC", currentTime); imageInformation.AddBoolean("IsCommand", isCommand); imageInformation.AddDouble("Probability threshold", probabilityThreshold); imageInformation.AddInt32("Predictions", imagePrediction.Predictions.Count); // Display and log the results of the prediction foreach (var prediction in imagePrediction.Predictions) { Debug.WriteLine($" Tag:{prediction.TagName} {prediction.Probability}"); imageInformation.AddDouble($"Tag:{prediction.TagName}", prediction.Probability); } // Post process the predictions based on the type of model switch (modelType) { case ModelType.Classification: // Use only the tags above the specified minimum probability foreach (var prediction in imagePrediction.Predictions) { if (prediction.Probability >= probabilityThreshold) { // Display and log the individual tag probabilities Debug.WriteLine($" Tag valid:{prediction.TagName} {prediction.Probability:0.00}"); imageInformation.AddDouble($"Tag valid:{prediction.TagName}", prediction.Probability); telemetryDataPoint.Add(prediction.TagName, prediction.Probability); } } break; case ModelType.Detection: // Group the tags to get the count, include only the predictions above the specified minimum probability var groupedPredictions = from prediction in imagePrediction.Predictions where prediction.Probability >= probabilityThreshold group prediction by new { prediction.TagName } into newGroup select new { TagName = newGroup.Key.TagName, Count = newGroup.Count(), }; // Display and log the agregated predictions foreach (var prediction in groupedPredictions) { Debug.WriteLine($" Tag valid:{prediction.TagName} {prediction.Count}"); imageInformation.AddInt32($"Tag valid:{prediction.TagName}", prediction.Count); telemetryDataPoint.Add(prediction.TagName, prediction.Count); } break; default: throw new ArgumentException("ModelType Invalid"); } this.logging.LogEvent("Captured image processed by Cognitive Services", imageInformation); try { using (Message message = new Message(Encoding.ASCII.GetBytes(JsonConvert.SerializeObject(telemetryDataPoint)))) { Debug.WriteLine(" {0:HH:mm:ss} AzureIoTHubClient SendEventAsync start", DateTime.UtcNow); await this.azureIoTHubClient.SendEventAsync(message); Debug.WriteLine(" {0:HH:mm:ss} AzureIoTHubClient SendEventAsync finish", DateTime.UtcNow); } this.logging.LogEvent("SendEventAsync payload", imageInformation, LoggingLevel.Information); } catch (Exception ex) { imageInformation.AddString("Exception", ex.ToString()); this.logging.LogEvent("SendEventAsync payload", imageInformation, LoggingLevel.Error); } } catch (Exception ex) { this.logging.LogMessage("Camera photo or save failed " + ex.Message, LoggingLevel.Error); } finally { this.displayGpioPin.Write(GpioPinValue.Low); this.cameraBusy = false; } }
private async Task ImageUpdate(bool isCommand) { DateTime currentTime = DateTime.UtcNow; // Just incase - stop code being called while photo already in progress if (this.cameraBusy) { return; } this.cameraBusy = true; try { using (Windows.Storage.Streams.InMemoryRandomAccessStream captureStream = new Windows.Storage.Streams.InMemoryRandomAccessStream()) { await this.mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), captureStream); await captureStream.FlushAsync(); #if DEBUG IStorageFile photoFile = await KnownFolders.PicturesLibrary.CreateFileAsync(ImageFilenameLocal, CreationCollisionOption.ReplaceExisting); ImageEncodingProperties imageProperties = ImageEncodingProperties.CreateJpeg(); await this.mediaCapture.CapturePhotoToStorageFileAsync(imageProperties, photoFile); #endif string azureFilenameLatest = string.Format(this.azureStorageimageFilenameLatestFormat, currentTime); string azureFilenameHistory = string.Format(this.azureStorageImageFilenameHistoryFormat, currentTime); LoggingFields imageInformation = new LoggingFields(); imageInformation.AddDateTime("TakenAtUTC", currentTime); imageInformation.AddBoolean("IsCommand", isCommand); #if DEBUG imageInformation.AddString("LocalFilename", photoFile.Path); #endif imageInformation.AddString("AzureFilenameLatest", azureFilenameLatest); imageInformation.AddString("AzureFilenameHistory", azureFilenameHistory); this.logging.LogEvent("Saving image(s) to Azure storage", imageInformation); // Update the latest image in storage if (!string.IsNullOrWhiteSpace(azureFilenameLatest)) { captureStream.Seek(0); Debug.WriteLine("AzureIoT Hub latest image upload start"); await this.azureIoTHubClient.UploadToBlobAsync(azureFilenameLatest, captureStream.AsStreamForRead()); Debug.WriteLine("AzureIoT Hub latest image upload done"); } // Upload the historic image to storage if (!string.IsNullOrWhiteSpace(azureFilenameHistory)) { captureStream.Seek(0); Debug.WriteLine("AzureIoT Hub historic image upload start"); await this.azureIoTHubClient.UploadToBlobAsync(azureFilenameHistory, captureStream.AsStreamForRead()); Debug.WriteLine("AzureIoT Hub historic image upload done"); } } } catch (Exception ex) { this.logging.LogMessage("Image capture or AzureIoTHub storage upload failed " + ex.Message, LoggingLevel.Error); } finally { this.cameraBusy = false; } }