Esempio n. 1
0
        public static async void CaptureOnePhoto(int type, Action <IRandomAccessStream> overAction)
        {
            var captureTime = DateTime.Now;

            if (type == 0)
            {
                using (IRandomAccessStream s = new InMemoryRandomAccessStream())
                {
                    await MainCamera.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), s);

                    overAction(s);
                }
            }
            else if (type == 1)
            {
                if (LowLag == null)
                {
                    return;
                }
                var photoData = await LowLag.CaptureAsync();

                using (var stream = photoData.Frame.CloneStream())
                {
                    overAction(stream);
                }
            }
        }
        /// <summary>
        /// Capture a photo.
        /// </summary>
        /// <param name="bitmapPixelFormat"><see cref="BitmapPixelFormat"/></param>
        /// <returns>Captured image</returns>
        public async Task <SoftwareBitmap> CapturePhotoAsync(BitmapPixelFormat bitmapPixelFormat)
        {
            SoftwareBitmap convert = null;

            try
            {
                CapturedPhoto asyncOperation = null;
                try
                {
                    asyncOperation = await _lowLagPhotoCapture.CaptureAsync();
                }
                catch (Exception exception)
                {
                    await OnProcessNotice(exception.ToString());
                }

                using (var asyncOperationFrame = asyncOperation.Frame)
                {
                    convert = SoftwareBitmap.Convert(asyncOperationFrame.SoftwareBitmap, bitmapPixelFormat);
                }
            }
            catch (Exception e)
            {
                await OnProcessNotice(e.ToString());
            }

            return(convert);
        }
Esempio n. 3
0
        public async Task TakeSnapshotAsync(bool save = false)
        {
            // Camera device streaming may shutdown.  Have to re-initalize if this happens
            // real application should handle this by registering for CameraStreamStateChange event -
            // one of many camera state changes to handle.  For an example, see...
            // https://github.com/Microsoft/Windows-universal-samples/blob/master/Samples/BasicFaceDetection/cs/Scenario2_DetectInWebcam.xaml.cs

            if (this._mediaCapture.CameraStreamState == Windows.Media.Devices.CameraStreamState.Shutdown)
            {
                _mediaCapture.Dispose();
                _mediaCapture = new MediaCapture();
                await InitializeCameraAsync();
            }
            var capturedPhoto = await _lowLagCapture.CaptureAsync();

            // Convert to format that allows display by XAML as source.
            this.LastImage = SoftwareBitmap.Convert(capturedPhoto.Frame.SoftwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);;

            var source = new SoftwareBitmapSource();
            await source.SetBitmapAsync(this.LastImage);

            this.LastImageSource = source;

            if (save)
            {
                await SaveSoftwareBitmapToFile(this.LastImage, "mypic");
            }
        }
Esempio n. 4
0
        public async Task StartCapture()
        {
            mCancelaationToken?.Cancel();
            if (mStreamWebSocket != null)
            {
                closeSocket(mStreamWebSocket);
            }

            await init();

            mCancelaationToken = new CancellationTokenSource();
            mStartTime         = DateTime.UtcNow;

            try
            {
                await mStreamWebSocket.ConnectAsync(new Uri($"{Globals.WEBSOCKET_ENDPOINT}?device={MainPage.GetUniqueDeviceId()}"));

                var task = Task.Run(async() =>
                {
                    var socket = mStreamWebSocket;
                    while (!mCancelaationToken.IsCancellationRequested)
                    {
                        try
                        {
                            var capturedPhoto = await mLowLagCapture.CaptureAsync();
                            using (var rac = capturedPhoto.Frame.CloneStream())
                            {
                                var dr    = new DataReader(rac.GetInputStreamAt(0));
                                var bytes = new byte[rac.Size];
                                await dr.LoadAsync((uint)rac.Size);
                                dr.ReadBytes(bytes);

                                await socket.OutputStream.WriteAsync(bytes.AsBuffer());
                            }
                        }
                        catch (Exception ex)
                        {
                            AppInsights.Client.TrackException(ex);
                        }

                        if ((DateTime.UtcNow - mStartTime) > mAutoStopAfter)
                        {
                            AppInsights.Client.TrackEvent("CameraAutoTurnOff");
                            mCancelaationToken.Cancel();
                        }
                    }
                }, mCancelaationToken.Token);
            }
            catch (Exception ex)
            {
                mStreamWebSocket.Dispose();
                mStreamWebSocket = null;
                AppInsights.Client.TrackException(ex);
            }
        }
        /// <summary>
        /// Takes the camera output and displays the Clarifai predictions on the pane.
        /// </summary>
        /// <returns>a task</returns>
        private async Task RunPredictionsAndDisplayResponse()
        {
            await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
            {
                try
                {
                    if (_predictionApi == null)
                    {
                        return;
                    }

                    LowLagPhotoCapture lowLagCapture =
                        await _mediaCapture.PrepareLowLagPhotoCaptureAsync(
                            ImageEncodingProperties.CreateUncompressed(MediaPixelFormat
                                                                       .Bgra8));
                    CapturedPhoto capturedPhoto   = await lowLagCapture.CaptureAsync();
                    SoftwareBitmap softwareBitmap = capturedPhoto.Frame.SoftwareBitmap;
                    byte[] data = await EncodedBytes(softwareBitmap,
                                                     BitmapEncoder.JpegEncoderId);

                    try
                    {
                        ConceptsTextBlock.Text = await _predictionApi
                                                 .PredictConcepts(data, _selectedModel);
                    }
                    catch (Exception ex)
                    {
                        ShowMessageToUser("Error: " + ex.Message);
                    }

                    try
                    {
                        (double camWidth, double camHeight) = CameraOutputDimensions();

                        List <Rect> rects = await _predictionApi.PredictFaces(data,
                                                                              camWidth, camHeight);
                        CameraGrid.Children.Clear();
                        foreach (Rect r in rects)
                        {
                            CameraGrid.Children.Add(CreateGuiRectangle(r, camWidth, camHeight));
                        }
                    }
                    catch (Exception ex)
                    {
                        ShowMessageToUser("Error: " + ex.Message);
                    }

                    await lowLagCapture.FinishAsync();
                }
                catch (COMException) // This is thrown when application exits.
                {
                    // No need to handle this since the application is exiting.
                }
            });
Esempio n. 6
0
        public async Task <SoftwareBitmap> GetImageAsync()
        {
            if (_photoCapture == null)
            {
                _photoCapture = await MediaCapture.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8));
            }

            var cc = await _photoCapture.CaptureAsync();

            return(cc.Frame.SoftwareBitmap);
        }
Esempio n. 7
0
        /// <summary>
        /// photo lowlag capture
        /// </summary>
        /// <returns></returns>
        private async Task CaptureLowLagPhotoAsync()
        {
            try
            {
                var folder = await KnownFolders.PicturesLibrary.CreateFolderAsync("Cam360", CreationCollisionOption.OpenIfExists);

                var file = await folder.CreateFileAsync("PhotoWithRecord.jpg", CreationCollisionOption.GenerateUniqueName);

                var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite);

                if (_lowLagPhotoCapture != null)
                {
                    CapturedPhoto photo = await _lowLagPhotoCapture.CaptureAsync();

                    var properties     = photo.Frame.BitmapProperties;
                    var softwareBitmap = photo.Frame.SoftwareBitmap;
                    if (softwareBitmap != null)
                    {
                        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, outputStream);

                        if (properties != null)
                        {
                            await encoder.BitmapProperties.SetPropertiesAsync(properties);
                        }

                        encoder.SetSoftwareBitmap(softwareBitmap);
                        await encoder.FlushAsync();
                    }
                    else
                    {
                        // This is a jpeg frame
                        var decoder = await BitmapDecoder.CreateAsync(photo.Frame.CloneStream());

                        var encoder = await BitmapEncoder.CreateForTranscodingAsync(outputStream, decoder);

                        await encoder.FlushAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                string errorMessage = string.Format("Photo capture did not complete: {0}", ex.Message);
                Debug.Write(errorMessage);
                await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
                {
                    await new MessageDialog(errorMessage).ShowAsync();
                });
            }
        }
        internal async Task <SoftwareBitmap> CaptureImage()
        {
            if (isPreviewing)
            {
                CapturedPhoto capturedPhoto = null;

                capturedPhoto = await lowLagCapture.CaptureAsync();

                var softwareBitmap = capturedPhoto.Frame.SoftwareBitmap;


                return(softwareBitmap);
            }

            return(null);
        }
Esempio n. 9
0
        async private void CaptureLagPhotoCapture_Click(object sender, RoutedEventArgs e)
        {
            // Take photo
            CapturedPhoto photo = await lowLagCaptureMgr.CaptureAsync();

            // Get photo as a BitmapImage
            BitmapImage bitmap = new BitmapImage();
            await bitmap.SetSourceAsync(photo.Frame);

            // Get thumbnail as a BitmapImage
            BitmapImage bitmapThumbnail = new BitmapImage();
            await bitmapThumbnail.SetSourceAsync(photo.Thumbnail);

            // imageLowLagPhoto is a <Image> object defined in XAML
            imageLowLagPhoto.Source = bitmap;

            // imageLowLagThumbnail is a <Image> object defined in XAML
            imageLowLagThumbnail.Source = bitmapThumbnail;
        }
Esempio n. 10
0
        private async void M_videoTimer_Tick(object sender, object e)
        {
            if (m_mediaCapture == null)
            {
                m_videoTimer.Stop();
                m_videoTimer = null;
                return;
            }

            lock (m_mediaCapture)
            {
                if (isrunning)
                {
                    return;
                }
                isrunning = true;
            }

            LowLagPhotoCapture cap = await m_mediaCapture.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateBmp());

            CapturedPhoto photo = await cap.CaptureAsync();


            WriteableBitmap bitmap = new WriteableBitmap((int)photo.Frame.Width, (int)photo.Frame.Height);
            await bitmap.SetSourceAsync(photo.Frame.AsStreamForRead().AsRandomAccessStream());

            bitmap.Invalidate();
            byte[] imageArray = bitmap.PixelBuffer.ToArray();

            int  starting = (bitmap.PixelHeight / 2) * bitmap.PixelWidth * 4 + (bitmap.PixelWidth * 2);
            byte blue     = imageArray[starting];
            byte green    = imageArray[starting + 1];
            byte red      = imageArray[starting + 2];

            m_liveColor[0, 0] = red;
            m_liveColor[0, 1] = green;
            m_liveColor[0, 2] = blue;

            MakeLiveSettingsUpdate();

            isrunning = false;
        }
        public static async Task <SoftwareBitmap> CaptureImage(MediaCapture MediaCaptureElement)
        {
            if (lowLagCapture == null)
            {
                lowLagCapture = await MediaCaptureElement.PrepareLowLagPhotoCaptureAsync(ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8));
            }

            try
            {
                CapturedPhoto capturedPhoto = await lowLagCapture.CaptureAsync();

                SoftwareBitmap softwareBitmap = capturedPhoto.Frame.SoftwareBitmap;

                return(softwareBitmap);
            }
            catch
            {
                lowLagCapture = null;
                return(await CaptureImage(MediaCaptureElement));
            }
        }
Esempio n. 12
0
        public async Task <SoftwareBitmap> GetFrameAsync()
        {
            if (_mediaCapture != null)
            {
                try
                {
                    CapturedPhoto capturedPhoto = await _lowLagCapture.CaptureAsync();

                    SoftwareBitmap swBmp = capturedPhoto.Frame.SoftwareBitmap;
                    return(swBmp);
                }
                catch (COMException e)
                {
                    Debug.WriteLine(e);
                    return(null);
                }
            }
            else
            {
                return(new SoftwareBitmap(BitmapPixelFormat.Bgra8, 400, 300, BitmapAlphaMode.Ignore));
            }
        }
Esempio n. 13
0
        public async Task StartCapture()
        {
            mCancelaationToken?.Cancel();
            mCancelaationToken = new CancellationTokenSource();
            mStartTime         = DateTime.UtcNow;

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed - yeah i know :-P
            Task.Run(async() =>
            {
                while (!mCancelaationToken.IsCancellationRequested)
                {
                    try
                    {
                        var capturedPhoto = await mLowLagCapture.CaptureAsync();
                        using (var rac = capturedPhoto.Frame.CloneStream())
                        {
                            var dr    = new DataReader(rac.GetInputStreamAt(0));
                            var bytes = new byte[rac.Size];
                            await dr.LoadAsync((uint)rac.Size);
                            dr.ReadBytes(bytes);
                            var message = new Message(bytes);
                            message.Properties["path"] = "imagefeed";
                            await mDeviceClient.SendEventAsync(message);
                        }

                        ////await Task.Delay(300);
                    }
                    catch (Exception ex)
                    {
                    }
                    if ((DateTime.UtcNow - mStartTime) > mAutoStopAfter)
                    {
                        mCancelaationToken.Cancel();
                    }
                }
            }, mCancelaationToken.Token);
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
        }
Esempio n. 14
0
        public async Task <Stream> MakePhotoAsync()
        {
            var photo = await _capture.CaptureAsync();

            return(photo.Frame.AsStream());
        }
Esempio n. 15
0
        private async void Camera_Click(object sender, RoutedEventArgs e)
        {
            if (isPreviewing)
            {
                CameraControl.Visibility = Visibility.Collapsed;
                LowLagPhotoCapture lowLagCapture = await mediaCapture.PrepareLowLagPhotoCaptureAsync(
                    ImageEncodingProperties.CreateUncompressed(MediaPixelFormat.Bgra8));

                CapturedPhoto capturedPhoto = await lowLagCapture.CaptureAsync();

                SoftwareBitmap softwareBitmap = capturedPhoto.Frame.SoftwareBitmap;
                if (softwareBitmap.BitmapPixelFormat != BitmapPixelFormat.Bgra8 ||
                    softwareBitmap.BitmapAlphaMode == BitmapAlphaMode.Straight)
                {
                    softwareBitmap = SoftwareBitmap.Convert(softwareBitmap,
                                                            BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
                }

                var source = new SoftwareBitmapSource();
                await source.SetBitmapAsync(softwareBitmap);

                Job j = DataContext as Job;
                if (j != null)
                {
                    j.Photo = softwareBitmap;
                }

                await lowLagCapture.FinishAsync();
                await CleanupCameraAsync();

                isPreviewing = false;
            }
            else
            {
                CameraControl.Visibility = Visibility.Visible;

                try
                {
                    MediaCaptureInitializationSettings mediaInitSettings =
                        new MediaCaptureInitializationSettings();

                    DeviceInformationCollection devices =
                        await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

                    foreach (var device in devices)
                    {
                        EnclosureLocation loc = device.EnclosureLocation;
                        Debug.WriteLine($"Location: {loc.Panel.ToString()} ID: {device.Id}");
                        if (sender is Button &&
                            ((Button)sender).Tag.ToString() == loc.Panel.ToString())
                        {
                            mediaInitSettings.VideoDeviceId        = device.Id;
                            mediaInitSettings.StreamingCaptureMode = StreamingCaptureMode.Video;
                            mediaInitSettings.PhotoCaptureSource   = PhotoCaptureSource.VideoPreview;


                            break;
                        }
                    }

                    mediaCapture = new MediaCapture();
                    await mediaCapture.InitializeAsync(mediaInitSettings);

                    var resolutions = mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(
                        MediaStreamType.Photo).Select(x => x as VideoEncodingProperties);
                    var minRes = resolutions.OrderBy(x => x.Height * x.Width).FirstOrDefault();
                    await mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(
                        MediaStreamType.VideoPreview, minRes);

                    PreviewControl.Source = mediaCapture;
                    await mediaCapture.StartPreviewAsync();

                    isPreviewing = true;

                    displayRequest.RequestActive();
                    DisplayInformation.AutoRotationPreferences = DisplayOrientations.Landscape;
                }
                catch (UnauthorizedAccessException)
                {
                    // This will be thrown if the user denied access to the camera in privacy settings
                    Debug.WriteLine("The app was denied access to the camera");
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("MediaCapture initialization failed. {0}", ex.Message);
                }
            }
        }