/// <summary> /// Initializes a new instance of the <see cref="Reader" /> class. /// </summary> /// <param name="capture">MediaCapture instance.</param> /// <param name="width">Capture frame width.</param> /// <param name="height">Capture frame height.</param> public Reader(MediaCapture capture, uint width, uint height) { this.capture = capture; this.encodingProps = new ImageEncodingProperties { Subtype = "BMP", Width = width, Height = height}; this.barcodeFound = false; this.cancelSearch = new CancellationTokenSource(); }
public async Task InitializeAsync() { _mediaCapture = new MediaCapture(); await _mediaCapture.InitializeAsync(); _imageEncodingProperties = ImageEncodingProperties.CreateJpeg(); }
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(); } } }
/// <summary> /// Initializes a new instance of the <see cref="Reader" /> class. /// </summary> /// <param name="capture">MediaCapture instance.</param> /// <param name="width">Capture frame width.</param> /// <param name="height">Capture frame height.</param> public void Init(MediaCapture capture, uint width, uint height) { this.capture = capture; encodingProps = ImageEncodingProperties.CreateJpeg(); encodingProps.Width = width; encodingProps.Height = height; barcodeReader = new BarcodeReader {Options = {TryHarder = true}}; cancelSearch = new CancellationTokenSource(); }
async private void CapturePhoto_Click(object sender, RoutedEventArgs e) { imgFormat = ImageEncodingProperties.CreateJpeg(); file = await ApplicationData.Current.LocalFolder.CreateFileAsync ("1.jpg", CreationCollisionOption.GenerateUniqueName); //using (IsolatedStorage store = IsolatedStorageFile.GetUserStoreForApplication()) await captureManager.CapturePhotoToStorageFileAsync(imgFormat, file); //bmpImage = new BitmapImage(new Uri(file.Path)); //imagePreview.Source = bmpImage; }
public async Task<MediaCapture> Initialize(CaptureUse primaryUse = CaptureUse.Photo) { // Create MediaCapture and init mediaCapture = new MediaCapture(); await mediaCapture.InitializeAsync(); mediaCapture.VideoDeviceController.PrimaryUse = primaryUse; // Create photo encoding properties as JPEG and set the size that should be used for photo capturing imgEncodingProperties = ImageEncodingProperties.CreateJpeg(); imgEncodingProperties.Width = 640; imgEncodingProperties.Height = 480; // Create video encoding profile as MP4 videoEncodingProperties = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto); // Lots of properties for audio and video could be set here... return mediaCapture; }
//System.Collections.Generic.IReadOnlyList<IMediaEncodingProperties> res; #endregion #region Constructor /// <summary> /// Initializes a new instance of the <see cref="Reader" /> class. /// </summary> /// <param name="capture">MediaCapture instance.</param> /// <param name="width">Capture frame width.</param> /// <param name="height">Capture frame height.</param> public void Init(MediaCapture capture, uint width, uint height) { this.capture = capture; encodingProps = ImageEncodingProperties.CreatePng(); encodingProps.Width = 1366; encodingProps.Height = 768; barcodeReader = new BarcodeReader { Options = { TryHarder = true, PossibleFormats = new BarcodeFormat[] { BarcodeFormat.QR_CODE }, } }; cancelSearch = new CancellationTokenSource(); }
public async Task<MediaCapture> Initialize(CaptureUse primaryUse = CaptureUse.Photo) { var devices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture); mediaCapture = new MediaCapture(); if (devices.Count() > 0) { await this.mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings { VideoDeviceId = devices.ElementAt(1).Id, PhotoCaptureSource = Windows.Media.Capture.PhotoCaptureSource.VideoPreview }); } mediaCapture.VideoDeviceController.PrimaryUse = primaryUse; mediaCapture.SetPreviewRotation(VideoRotation.Clockwise90Degrees); imgEncodingProperties = ImageEncodingProperties.CreateJpeg(); imgEncodingProperties.Width = 640; imgEncodingProperties.Height = 480; videoEncodingProperties = MediaEncodingProfile.CreateMp4(VideoEncodingQuality.Auto); return mediaCapture; }
/// <summary> /// Captures a photo to a stream asynchronously. /// </summary> /// <param name="stream">The stream.</param> /// <param name="imageEncodingProperties">The image encoding properties.</param> /// <returns></returns> public async Task CapturePhotoToStreamAsync(IRandomAccessStream stream, ImageEncodingProperties imageEncodingProperties) { await MediaCapture.CapturePhotoToStreamAsync(imageEncodingProperties, stream); }
private async void ScanBarCode() { var imgProp = new ImageEncodingProperties { Subtype = "BMP", Width = 450, Height = 450 }; var bcReader = new BarcodeReader(); while (exit == 0) { var stream = new InMemoryRandomAccessStream(); await _mediaCapture.CapturePhotoToStreamAsync(imgProp, stream); stream.Seek(0); var wbm = new WriteableBitmap(450, 450); await wbm.SetSourceAsync(stream); var result = bcReader.Decode(wbm); if (result != null) { exit = 1; int swissmedicNr = 0; try { swissmedicNr = Int32.Parse(result.Text.Substring(3, 6)); } catch { } if(swissmedicNr > 0) { Drug med = DatabaseStore.GetDrugForSwissMedicNumber(swissmedicNr); List<Drug> list = new List<Drug>(); if(med != null) { list.Add(med); } Frame.Navigate(typeof(SearchDrugResults), list); } } await Task.Delay(TimeSpan.FromSeconds(1)); } }
private async Task ScanPreviewBuffer() { try { using ( var stream = new InMemoryRandomAccessStream() ) { // Fairly small values to avoid scanning very large images var props = new ImageEncodingProperties { Subtype = "BMP", Width = 600, Height = 800 }; await _camera.CapturePhotoToStreamAsync( props, stream ); var bitmap = new WriteableBitmap( (int) props.Width, (int) props.Height ); stream.Seek( 0 ); await bitmap.SetSourceAsync( stream ); var reader = new BarcodeReader(); var result = reader.Decode( bitmap ); if ( result != null ) { ProcessResultText( result.Text ); } } } catch { // The camera is prone to odd things. Don't crash. } }
/// <summary> /// Creates a new <see cref="CapturedImageFrame"/> instance from the <paramref name="stream"/> specified. /// </summary> /// <param name="stream">Captured image stream.</param> /// <param name="encoding">Encoding used to capture the frame <paramref name="stream"/></param> /// <param name="orientation">Camera orientation.</param> /// <param name="cameraType">Camera type.</param> /// <returns>Captured image frame.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="stream"/> is <see langword="null"/>. /// <para>-or-</para> /// <paramref name="encoding"/> is <see langword="null"/>. /// </exception> /// <exception cref="ArgumentException"> /// <paramref name="stream"/> cannot be read. /// <para>-or-</para> /// <paramref name="stream"/> is empty. /// </exception> public static CapturedImageFrame CreateFromStream(IRandomAccessStream stream, ImageEncodingProperties encoding, PageOrientation orientation, CameraType cameraType) { if (stream == null) { throw new ArgumentNullException("stream"); } if (!stream.CanRead) { throw new ArgumentException("Stream cannot be read.", "stream"); } if (stream.Size == 0) { throw new ArgumentException("Stream is empty.", "stream"); } if (encoding == null) { throw new ArgumentNullException("encoding"); } return new CapturedImageFrame { Rotation = OrientationHelper.ConvertOrientationToRotation(orientation, cameraType), Flip = OrientationHelper.ConvertOrientationToFlip(orientation, cameraType), Stream = stream, Height = encoding.Height, Width = encoding.Width }; }
private void SetEncodingProperties(CaptureUse captureUse) { if (this._videoEncodingProfile.Video != null) { if (captureUse == CaptureUse.Photo) { var allProps = this._mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties( MediaStreamType.Photo); var photoProps = GetMediaProperties(allProps, CaptureUse.Photo) as ImageEncodingProperties; if (photoProps == null) { var videoProps = GetMediaProperties(allProps, CaptureUse.Video) as VideoEncodingProperties; if (videoProps != null) { this._imageEncodingProperties.Height = videoProps.Height; this._imageEncodingProperties.Width = videoProps.Width; this._mediaEncodingProperties = videoProps; } else { VideoEncodingProperties maxProps = null; var max = 0; foreach (var res in from res in allProps.OfType<VideoEncodingProperties>() where res.Width * res.Height > max select res) { max = (int)(res.Width * res.Height); maxProps = res; } if (maxProps != null) { this._imageEncodingProperties.Height = maxProps.Height; this._imageEncodingProperties.Width = maxProps.Width; this._mediaEncodingProperties = maxProps; } } } else { this._imageEncodingProperties = photoProps; this._mediaEncodingProperties = this._imageEncodingProperties; } } else { var mediaStreamProps = this._mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties( MediaStreamType.VideoPreview).ToList(); var videoProps = mediaStreamProps.OfType<VideoEncodingProperties>() .FirstOrDefault( x => x != null && this._supportedVideoFormats.Contains(x.Subtype.ToLower()) && x.Width == this._videoEncodingProfile.Video.Width && x.Height == this._videoEncodingProfile.Video.Height); if (videoProps != null) { this._mediaEncodingProperties = videoProps; } else { var allVideoProps = mediaStreamProps.OfType<VideoEncodingProperties>() .Where( x => x != null && this._supportedVideoFormats.Contains(x.Subtype.ToLower()) && x.Width <= this._videoEncodingProfile.Video.Width && x.Height <= this._videoEncodingProfile.Video.Height); VideoEncodingProperties maxVideoPropsRatio = null; var max = 0; foreach (var res in from res in allVideoProps where res.Width * res.Height > max let ratio = (double)res.Width / res.Height where ratio > 1.34 select res) { max = (int)(res.Width * res.Height); maxVideoPropsRatio = res; } if (maxVideoPropsRatio != null) { this._mediaEncodingProperties = maxVideoPropsRatio; } else { VideoEncodingProperties maxVideoProps = null; max = 0; foreach (var res in from res in allVideoProps where res.Width * res.Height > max select res) { max = (int)(res.Width * res.Height); maxVideoProps = res; } if (maxVideoProps != null) { this._mediaEncodingProperties = maxVideoProps; } } } } } }
/// <summary> /// Initializes the <see cref="MediaCapture"/> element. /// </summary> /// <param name="primaryUse"> /// The primary use for the camera. /// </param> /// <param name="videoQuality"> /// The video quality (for recording only). /// </param> /// <returns> /// The <see cref="MediaCapture"/>. /// </returns> public async Task<MediaCapture> Initialize( CaptureUse primaryUse, VideoEncodingQuality videoQuality) { if (this._mediaCapture != null) { this.Dispose(); } this.IsCameraAvailable = true; var camera = await this.GetCamera(this.ActiveCamera); this._mediaCapture = new MediaCapture(); this._mediaCapture.Failed += this.OnMediaCaptureFailed; await this._mediaCapture.InitializeAsync(new MediaCaptureInitializationSettings { VideoDeviceId = camera.Id }); this._mediaCapture.VideoDeviceController.PrimaryUse = primaryUse; this._imageEncodingProperties = ImageEncodingProperties.CreateJpeg(); this._videoEncodingProfile = MediaEncodingProfile.CreateMp4(videoQuality); this.SetEncodingProperties(primaryUse); var rotation = this.GetVideoRotation(DisplayInformation.GetForCurrentView().CurrentOrientation); if (primaryUse == CaptureUse.Photo) { await this._mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync( MediaStreamType.Photo, this._mediaEncodingProperties); this.IsFlashAvailable = this.Settings.FlashControl.Supported; } else if (primaryUse == CaptureUse.Video) { await this._mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync( MediaStreamType.VideoPreview, this._mediaEncodingProperties); await this._mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync( MediaStreamType.VideoRecord, this._mediaEncodingProperties); this.SetRecordOrientation(rotation); this.IsFlashAvailable = this.Settings.TorchControl.Supported; } this._mediaCapture.SetPreviewRotation(rotation); return this._mediaCapture; }
private async void ScanQrCode() { string retCode = String.Empty; try { await InitializeQrCode(); var imgProp = new ImageEncodingProperties { Subtype = "BMP", Width = 380, Height = 380 }; var bcReader = new BarcodeReader(); while (exit == 0) { var stream = new InMemoryRandomAccessStream(); await _mediaCapture.CapturePhotoToStreamAsync(imgProp, stream); stream.Seek(0); var wbm = new WriteableBitmap(380, 380); await wbm.SetSourceAsync(stream); var result = bcReader.Decode(wbm); if (result != null) { var torch = _mediaCapture.VideoDeviceController.TorchControl; if (torch.Supported) torch.Enabled = false; await _mediaCapture.StopPreviewAsync(); retCode = result.Text; //var msgbox = new MessageDialog(result.Text); //await msgbox.ShowAsync(); exit = 1; //try //{ // StorageFolder folder = ApplicationData.Current.LocalFolder; // if (folder != null) // { // //Saving Scan Text // StorageFile sampleFile = await folder.CreateFileAsync("sample.txt", CreationCollisionOption.ReplaceExisting); // await Windows.Storage.FileIO.WriteTextAsync(sampleFile, "Swift as a shadow"); // //Saving Scan Image // StorageFile file = await folder.CreateFileAsync("imagefile" + ".jpg", CreationCollisionOption.ReplaceExisting); // using (var storageStream = await file.OpenAsync(FileAccessMode.ReadWrite)) // { // var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, storageStream); // var pixelStream = wbm.PixelBuffer.AsStream(); // var pixels = new byte[pixelStream.Length]; // await pixelStream.ReadAsync(pixels, 0, pixels.Length); // encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)wbm.PixelWidth, (uint)wbm.PixelHeight, 48, 48, pixels); // await encoder.FlushAsync(); // } // } // //MyImage.Source = wbm; // exit = 1; //} //catch (Exception ex) //{ // MessageDialog dialog = new MessageDialog("Error while initializing media capture device: " + ex.Message); // dialog.ShowAsync(); // GC.Collect(); //} // } } KeyValuePair<string, string> retObj = new KeyValuePair<string, string>("UserCode", retCode); _mediaCapture.Dispose(); Frame.Navigate(typeof(Login), retObj); } catch { } }