Exemple #1
0
        public static void LoadPhoto()
        {
            if (App.isRPi)
            {
                return;
            }

            Task.Run(async() =>
            {
                try
                {
                    var stream = await BlobStorageService.DownloadData("dronepictures");
                    if (stream == null)
                    {
                        return;
                    }

                    stream.Seek(0, SeekOrigin.Begin);
                    IRandomAccessStream photoStream = stream.AsRandomAccessStream();


                    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() =>
                    {
                        BitmapImage bitmap = new BitmapImage();
                        await bitmap.SetSourceAsync(photoStream);
                        var currentPage     = ((ContentControl)Window.Current.Content).Content as Page;
                        var captureImage    = currentPage.FindName("captureImage") as Image;
                        captureImage.Source = bitmap;
                    });
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"Error during taking photo: {ex.Message}");
                }
            });
        }
Exemple #2
0
        private static void ProcessFrames()
        {
            if (socket == null)
            {
                return;
            }

            _stoppedThreads--;

            while (_stopThreads == false)
            {
                try
                {
                    //GarbageCollectorCanWorkHere();

                    var frame = mediaFrameReader.TryAcquireLatestFrame();

                    var frameDuration = new Stopwatch();
                    frameDuration.Start();

                    if (frame == null ||
                        frame.VideoMediaFrame == null ||
                        frame.VideoMediaFrame.SoftwareBitmap == null)
                    {
                        continue;
                    }

                    using (var stream = new InMemoryRandomAccessStream())
                    {
                        using (var bitmap = SoftwareBitmap.Convert(frame.VideoMediaFrame.SoftwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore))
                        {
                            var imageTask = BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream, imageQuality).AsTask();
                            imageTask.Wait();
                            var encoder = imageTask.Result;
                            encoder.SetSoftwareBitmap(bitmap);

                            var flushTask = encoder.FlushAsync().AsTask();
                            flushTask.Wait();

                            using (var asStream = stream.AsStream())
                            {
                                asStream.Position = 0;

                                var bytes = new byte[asStream.Length];
                                asStream.Read(bytes, 0, bytes.Length);

                                if (getPhoto)
                                {
                                    getPhoto = false;
                                    Task.Run(async() =>
                                    {
                                        try
                                        {
                                            await BlobStorageService.UploadData("dronepictures", bytes);
                                            Debug.WriteLine($"Photo uploaded successfully.");

                                            var cmd = Encoding.ASCII.GetBytes("#load_photo");
                                            Socket.SendData(cmd);
                                        }
                                        catch (Exception ex)
                                        {
                                            Debug.WriteLine($"Error during photo upload: {ex.Message}");
                                        }
                                    });
                                }

                                socket.OutputStream.WriteAsync(bytes.AsBuffer());
                                encoder = null;
                            }
                        }
                    }
                }
                catch (ObjectDisposedException) { }
            }

            _stoppedThreads++;
        }