Exemple #1
0
        public async void CreateCoverFromStream(Stream coverStream)
        {
            IRandomAccessStream src = WindowsRuntimeStreamExtensions.AsRandomAccessStream(coverStream);

            CoverSrc = new BitmapImage();
            await CoverSrc.SetSourceAsync(src);
        }
Exemple #2
0
        /// <summary>
        /// Convert a stream to an image, and dispose of the stream.
        /// This must be called on the UI thread!
        /// When the async is done, it may not do the callback on the UI thread!
        /// </summary>
        /// <param name="imageStream"></param>
        /// <returns></returns>
        private async Task <Tuple <BitmapImage, PDFPageViewModel> > ConvertToBMI(MemoryStream imageStream, PDFPageViewModel vm)
        {
            imageStream.Seek(0, SeekOrigin.Begin);
            var bm = new BitmapImage();
            await bm.SetSourceAsync(WindowsRuntimeStreamExtensions.AsRandomAccessStream(imageStream));

            imageStream.Dispose();
            return(Tuple.Create(bm, vm));
        }
Exemple #3
0
        /// <summary>
        /// Initialize with the page that we should track.
        /// </summary>
        /// <param name="pageInfo">A stream of cache tags and the PDF pages associated with it.</param>
        /// <remarks>We are really only interested in the first PdfPage we get - and we will re-subscribe, but not hold onto it.</remarks>
        public PDFPageViewModel(IObservable <Tuple <string, IObservable <PdfPage> > > pageInfo, IBlobCache cache = null)
        {
            _cache = cache ?? Blobs.LocalStorage;

            // Render an image when:
            //   - We have at least one render request
            //   - ImageStream is subscribed to
            //   - the file or page updates somehow (new pageInfo iteration).
            //
            // Always pull from the cache first, but if that doesn't work, then render and place
            // in the cache.

            // Get the size of the thing when it is requested. This sequence must be finished before
            // any size calculations can be done!
            var imageSize = from pgInfo in pageInfo
                            from sz in _cache.GetOrFetchPageSize(pgInfo.Item1, () => pgInfo.Item2.Take(1).Select(pdf => pdf.Size.ToIWalkerSize()))
                            select new
            {
                PGInfo = pgInfo,
                Size   = sz
            };
            var publishedSize = imageSize
                                .Do(info => _pageSize = info.Size)
                                .Select(info => info.PGInfo)
                                .Publish().RefCount();

            _pageSizeLoaded = publishedSize.AsUnit();

            // The render request must be "good" - that is well formed. Otherwise
            // we will just ignore it. This is because sometimes when things are being "sized" the
            // render request is malformed.
            RenderImage = ReactiveCommand.Create();
            var renderRequest = RenderImage
                                .Cast <Tuple <RenderingDimension, double, double> >()
                                .Where(info => (info.Item1 == RenderingDimension.Horizontal && info.Item2 > 0) ||
                                       (info.Item1 == RenderingDimension.Vertical && info.Item3 > 0) ||
                                       (info.Item1 == RenderingDimension.MustFit && info.Item2 > 0 && info.Item3 > 0)
                                       );

            // Generate an image when we have a render request and a everything else is setup.
            ImageStream = from requestInfo in Observable.CombineLatest(publishedSize, renderRequest, (pSize, rr) => new { pgInfo = pSize, RenderRequest = rr })
                          let imageDimensions = CalcRenderingSize(requestInfo.RenderRequest.Item1, requestInfo.RenderRequest.Item2, requestInfo.RenderRequest.Item3)
                                                from imageData in _cache.GetOrFetchPageImageData(requestInfo.pgInfo.Item1, imageDimensions.Item1, imageDimensions.Item2,
                                                                                                 () => requestInfo.pgInfo.Item2.SelectMany(pdfPg =>
            {
                var ms  = new MemoryStream();
                var ra  = WindowsRuntimeStreamExtensions.AsRandomAccessStream(ms);
                var opt = new PdfPageRenderOptions()
                {
                    DestinationWidth = (uint)imageDimensions.Item1, DestinationHeight = (uint)imageDimensions.Item2
                };
                return(Observable.FromAsync(() => pdfPg.RenderToStreamAsync(ra).AsTask())
                       .Select(_ => ms.ToArray()));
            }))
                                                select new MemoryStream(imageData);
        }
        private async void BtnPlay_ClickAsync(object sender, RoutedEventArgs e)
        {
            if (!User.IsLogIn())
            {
                await new ContentDialog
                {
                    Title             = "Информация",
                    Content           = "Авторизуйтесь в системе для получения возможности прослушивания песен",
                    PrimaryButtonText = "ОК"
                }.ShowAsync();
                return;
            }
            _mediaElement.MediaPlayer.Pause();
            IRandomAccessStream src = null;
            await Task.Run(() =>
            {
                App app = Application.Current as App;
                try
                {
                    using (SqlConnection connection = new SqlConnection(app.GetConnectionString()))
                    {
                        connection.Open();
                        SqlCommand cmd  = connection.CreateCommand();
                        cmd.CommandText = string.Format(DBQueryCollection.QUERY_FOR_MUSIC_SRC, _track.SrcId.ToString());
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            reader.Read();
                            src = WindowsRuntimeStreamExtensions.AsRandomAccessStream(reader.GetStream(0));
                        }
                    }
                }
                catch (Exception)
                {
                    ShowErrorDialog();
                    return;
                }
            });

            MediaSource                mediaSource       = MediaSource.CreateFromStream(src, "MPEG");
            MediaPlaybackItem          mediaPlaybackItem = new MediaPlaybackItem(mediaSource);
            MediaItemDisplayProperties props             = mediaPlaybackItem.GetDisplayProperties();

            props.Type = Windows.Media.MediaPlaybackType.Music;
            props.MusicProperties.Artist     = _track.Artist.Name;
            props.MusicProperties.AlbumTitle = _track.Album.Name;
            props.MusicProperties.Title      = _track.Name;
            mediaPlaybackItem.ApplyDisplayProperties(props);
            _mediaElement.MediaPlayer.Source = mediaPlaybackItem;
            _mediaElement.MediaPlayer.Play();
        }
        public static async void OCRWordPosition()
        {
            KeyUnReg();
            OptionKeyUnReg();
            Bitmap   Screen = new Bitmap(P[3] - P[1], P[4] - P[2]);
            Graphics g      = Graphics.FromImage(Screen);

            g.CopyFromScreen(P[1], P[2], 00, 0, Screen.Size);
            var engine       = Windows.Media.Ocr.OcrEngine.TryCreateFromLanguage(new Windows.Globalization.Language("en"));
            var memoryStream = new MemoryStream();

            Screen.Save(memoryStream, ImageFormat.Bmp);
            var WMS     = WindowsRuntimeStreamExtensions.AsRandomAccessStream(memoryStream);
            var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(WMS);

            var softwareBitmap = await decoder.GetSoftwareBitmapAsync();

            var OCRR = await engine.RecognizeAsync(softwareBitmap);

            OCRResultForm ORF = new         OCRResultForm();

            foreach (var Line in OCRR.Lines)
            {
                for (int I = 0; I <= Line.Words.Count - 1; I++)
                {
                    LW.Add(Line.Words[I].Text);
                    LX.Add(Convert.ToInt32(Line.Words[I].BoundingRect.X));
                    LY.Add(Convert.ToInt32(Line.Words[I].BoundingRect.Y));
                }
            }
            foreach (var W in LW)
            {
                ORF.WList.Items.Add(W);
            }
            ORF.ShowDialog();
            if (ORF.DialogResult == DialogResult.OK)
            {
                int SI = ORF.WList.SelectedIndex;
                MagicKeys.MouseMove(P[1] + LX[SI], P[2] + LY[SI], 0);
                LW.Clear();
                LX.Clear();
                LY.Clear();
            }
            OptionKeyReg();
            KeyReg();
        }
Exemple #6
0
        public static async void OCR()
        {
            Bitmap   Screen = new Bitmap(P[3] - P[1], P[4] - P[2]);
            Graphics g      = Graphics.FromImage(Screen);

            g.CopyFromScreen(P[1], P[2], 00, 0, Screen.Size);
            var engine       = Windows.Media.Ocr.OcrEngine.TryCreateFromLanguage(new Windows.Globalization.Language("en"));
            var memoryStream = new MemoryStream();

            Screen.Save(memoryStream, ImageFormat.Bmp);
            var WMS     = WindowsRuntimeStreamExtensions.AsRandomAccessStream(memoryStream);
            var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(WMS);

            var softwareBitmap = await decoder.GetSoftwareBitmapAsync();

            var OCRR = await engine.RecognizeAsync(softwareBitmap);

            Double Pos = OCRR.Lines[0].Words[0].BoundingRect.X;
        }
Exemple #7
0
        /// <summary>
        /// ImageSourceをBitmapFrameに変換
        /// 参考:https://marunaka-blog.com/wpf-ocr-windows10/2260/
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        private async Task <SoftwareBitmap> ConvertSoftwareBitmap(System.Windows.Controls.Image image)
        {
            SoftwareBitmap sbitmap = null;

            using (MemoryStream stream = new MemoryStream())
            {
                var encoder = new BmpBitmapEncoder();
                encoder.Frames.Add((System.Windows.Media.Imaging.BitmapFrame)image.Source);
                encoder.Save(stream);

                // メモリストリームを変換
                var irstream = WindowsRuntimeStreamExtensions.AsRandomAccessStream(stream);

                // 画像データをSoftwareBitmapに変換
                var decorder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(irstream);

                sbitmap = await decorder.GetSoftwareBitmapAsync();
            }

            return(sbitmap);
        }
Exemple #8
0
        public async static Task <string> OCR(int W, int H, int X, int Y, int Zoom)
        {
            Bitmap   Screen = new Bitmap(W, H);
            Graphics g      = Graphics.FromImage(Screen);

            g.CopyFromScreen(X, Y, 00, 0, Screen.Size);
            Bitmap S = new Bitmap(Screen, new Size(W * Zoom, H * Zoom));

            S.MakeTransparent();
            var engine       = Windows.Media.Ocr.OcrEngine.TryCreateFromLanguage(new Windows.Globalization.Language("en"));
            var memoryStream = new MemoryStream();

            S.Save(memoryStream, ImageFormat.Bmp);
            var WMS     = WindowsRuntimeStreamExtensions.AsRandomAccessStream(memoryStream);
            var decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(WMS);

            var softwareBitmap = await decoder.GetSoftwareBitmapAsync();

            var ocrResult = await engine.RecognizeAsync(softwareBitmap);

            return(ocrResult.Text);
        }
        private async Task <BitmapDecoder> GetImageFromImageSource(ImageSource imageSource)
        {
            IImageSourceHandler handler;

            if (imageSource is FileImageSource)
            {
                handler = new FileImageSourceHandler();
            }
            else if (imageSource is StreamImageSource)
            {
                handler = new StreamImageSourceHandler();
                var stream = await((IStreamImageSource)imageSource).GetStreamAsync();
                if (stream != null)
                {
                    return(await BitmapDecoder.CreateAsync(WindowsRuntimeStreamExtensions.AsRandomAccessStream(stream)));
                }
            }
            else if (imageSource is UriImageSource)
            {
                handler = new UriImageSourceHandler();
            }
            else
            {
                throw new NotImplementedException();
            }

            var bitmapImage = await handler.LoadImageAsync(imageSource) as BitmapImage;

            if (bitmapImage?.UriSource != null)
            {
                using (IRandomAccessStreamWithContentType streamWithContent = await RandomAccessStreamReference.CreateFromUri(bitmapImage.UriSource).OpenReadAsync())
                {
                    return(await BitmapDecoder.CreateAsync(streamWithContent));
                }
            }

            throw new NotImplementedException();
        }