/// <summary>
        /// Loads an image from the file system and calls the ImageLoaded XAML control
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void ImageLoadButton_Click(object sender, RoutedEventArgs e)
        {
            FileOpenPicker openPicker = new FileOpenPicker();

            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            openPicker.FileTypeFilter.Add(".jpg");
            openPicker.FileTypeFilter.Add(".png");
            openPicker.FileTypeFilter.Add(".gif");
            openPicker.FileTypeFilter.Add(".bmp");

            StorageFile file = await openPicker.PickSingleFileAsync();

            if (file != null)
            {
                var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);

                Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);

                _LocalPersistentObject.bitmapProcessingImage = BitmapFactory.New((int)decoder.PixelWidth, (int)decoder.PixelHeight);
                _LocalPersistentObject.bitmapProcessingImage.SetSource(stream);

                _LocalPersistentObject.originalLoadedImage = _LocalPersistentObject.bitmapProcessingImage;
            }

            MainImageFrame.Navigate(typeof(ImageLoadedView), _LocalPersistentObject);
        }
Ejemplo n.º 2
0
        private async System.Threading.Tasks.Task <StorageFile> saveImageToFileAsync(RandomAccessStreamReference img)
        {
            var imgstream = await img.OpenReadAsync();

            BitmapImage bitmap = new BitmapImage();

            bitmap.SetSource(imgstream);
            Windows.UI.Xaml.Media.Imaging.WriteableBitmap src = new Windows.UI.Xaml.Media.Imaging.WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight);
            src.SetSource(imgstream);
            Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(imgstream);

            Windows.Graphics.Imaging.PixelDataProvider pxprd = await decoder.GetPixelDataAsync(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, Windows.Graphics.Imaging.BitmapAlphaMode.Straight, new Windows.Graphics.Imaging.BitmapTransform(), Windows.Graphics.Imaging.ExifOrientationMode.RespectExifOrientation, Windows.Graphics.Imaging.ColorManagementMode.DoNotColorManage);

            byte[]        buffer = pxprd.DetachPixelData();
            StorageFolder folder = ApplicationData.Current.LocalFolder;
            StorageFile   file   = await folder.CreateFileAsync(img.GetHashCode() + "clipboardSaved" + ".png",
                                                                CreationCollisionOption.ReplaceExisting);

            using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite)) {
                var encoder = await Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(Windows.Graphics.Imaging.BitmapEncoder.PngEncoderId, fileStream);

                encoder.SetPixelData(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, Windows.Graphics.Imaging.BitmapAlphaMode.Straight, decoder.PixelWidth, decoder.PixelHeight, decoder.DpiX, decoder.DpiY, buffer);
                await encoder.FlushAsync();
            }
            return(file);
        }
Ejemplo n.º 3
0
        public static async Task <UwpSoftwareBitmap> ConvertFrom(BitmapFrame sourceBitmap)
        {
            // BitmapFrameをBMP形式のバイト配列に変換
            byte[] bitmapBytes;
            var    encoder = new BmpBitmapEncoder(); // .NET用のエンコーダーを使う

            encoder.Frames.Add(sourceBitmap);
            using (var memoryStream = new MemoryStream())
            {
                encoder.Save(memoryStream);
                bitmapBytes = memoryStream.ToArray();
            }

            // バイト配列をUWPのIRandomAccessStreamに変換
            using (var randomAccessStream = new UwpInMemoryRandomAccessStream())
            {
                using (var outputStream = randomAccessStream.GetOutputStreamAt(0))
                    using (var writer = new UwpDataWriter(outputStream))
                    {
                        writer.WriteBytes(bitmapBytes);
                        await writer.StoreAsync();

                        await outputStream.FlushAsync();
                    }

                // IRandomAccessStreamをSoftwareBitmapに変換
                // (UWP APIのデコーダー)
                var decoder = await UwpBitmapDecoder.CreateAsync(randomAccessStream);

                var softwareBitmap
                    = await decoder.GetSoftwareBitmapAsync(UwpBitmapPixelFormat.Bgra8, UwpBitmapAlphaMode.Premultiplied);

                return(softwareBitmap);
            }
        }
Ejemplo n.º 4
0
        private async void TakePhoto_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                photo = await captureUI.CaptureFileAsync(CameraCaptureUIMode.Photo);

                if (photo == null)
                {
                    return;
                }
                else
                {
                    imageStream = await photo.OpenAsync(FileAccessMode.Read);

                    Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(imageStream);

                    Windows.Graphics.Imaging.SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();

                    Windows.Graphics.Imaging.SoftwareBitmap softwareBitmapBRG = Windows.Graphics.Imaging.SoftwareBitmap.Convert(softwareBitmap, Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8,
                                                                                                                                Windows.Graphics.Imaging.BitmapAlphaMode.Premultiplied);
                    SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
                    await bitmapSource.SetBitmapAsync(softwareBitmapBRG);

                    image.Source = bitmapSource;
                }
            }
            catch
            {
                output.Text = "Error carnal";
            }
        }
Ejemplo n.º 5
0
        async Task <OcrResult> RecognizeBitmapAsync(Bitmap b)
        {
            // Need to marshall from Drawing.Bitmap to UWP SoftwareBitmap
            using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
            {
                b.Save(stream.AsStream(), System.Drawing.Imaging.ImageFormat.Bmp);                //choose the specific image format by your own bitmap source
                Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);

                Windows.Graphics.Imaging.SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();

                return(await engine.RecognizeAsync(softwareBitmap));
            }
        }
Ejemplo n.º 6
0
        public async Task<string> clipboard(DataPackageView con)
        {
            string str = string.Empty;
            //文本
            if (con.Contains(StandardDataFormats.Text))
            {
                str = await con.GetTextAsync();
                return str;
            }

            //图片
            if (con.Contains(StandardDataFormats.Bitmap))
            {
                RandomAccessStreamReference img = await con.GetBitmapAsync();
                var imgstream = await img.OpenReadAsync();
                Windows.UI.Xaml.Media.Imaging.BitmapImage bitmap = new Windows.UI.Xaml.Media.Imaging.BitmapImage();
                bitmap.SetSource(imgstream);

                Windows.UI.Xaml.Media.Imaging.WriteableBitmap src = new Windows.UI.Xaml.Media.Imaging.WriteableBitmap(bitmap.PixelWidth, bitmap.PixelHeight);
                src.SetSource(imgstream);

                Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(imgstream);
                Windows.Graphics.Imaging.PixelDataProvider pxprd = await decoder.GetPixelDataAsync(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, Windows.Graphics.Imaging.BitmapAlphaMode.Straight, new Windows.Graphics.Imaging.BitmapTransform(), Windows.Graphics.Imaging.ExifOrientationMode.RespectExifOrientation, Windows.Graphics.Imaging.ColorManagementMode.DoNotColorManage);
                byte[] buffer = pxprd.DetachPixelData();

                str = "image";
                StorageFolder folder = await _folder.GetFolderAsync(str);

                StorageFile file = await folder.CreateFileAsync(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + (ran.Next() % 10000).ToString() + ".png", CreationCollisionOption.GenerateUniqueName);

                using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var encoder = await Windows.Graphics.Imaging.BitmapEncoder.CreateAsync(Windows.Graphics.Imaging.BitmapEncoder.PngEncoderId, fileStream);
                    encoder.SetPixelData(Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8, Windows.Graphics.Imaging.BitmapAlphaMode.Straight, decoder.PixelWidth, decoder.PixelHeight, decoder.DpiX, decoder.DpiY, buffer);
                    await encoder.FlushAsync();

                    str = $"![这里写图片描述](image/{file.Name})\n";
                }
            }

            //文件
            if (con.Contains(StandardDataFormats.StorageItems))
            {
                var filelist = await con.GetStorageItemsAsync();
                StorageFile file = filelist.OfType<StorageFile>().First();
                return await imgfolder(file);
            }

            return str;
        }
Ejemplo n.º 7
0
        static byte[] GetBitmap(DataPackageView dpv)
        {
            var task = Task <byte[]> .Run(async() =>
            {
                var reference = await dpv.GetBitmapAsync() as Windows.Storage.Streams.RandomAccessStreamReference;
                //var random = (Windows.Storage.Streams.IRandomAccessStream)reference.OpenReadAsync();
                var random = await reference.OpenReadAsync();
                Windows.Graphics.Imaging.BitmapDecoder decoder       = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(random);
                Windows.Graphics.Imaging.PixelDataProvider pixelData = await decoder.GetPixelDataAsync();
                return(pixelData.DetachPixelData());
            });

            return(task.Result);
        }
        /// <summary>
        /// Returns a cropped image
        /// </summary>
        /// <returns></returns>
        public async Task<WriteableBitmap> GetCropppedImageBitmap()
        {
            WriteableBitmap bmp;

            //Saves the cropped image to a stream.
            using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
            {
                await CroppedImage.SaveAsync(stream, BitmapFileFormat.Bmp);
                stream.Seek(0);

                Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);
                bmp =  BitmapFactory.New((int)decoder.PixelWidth, (int)decoder.PixelHeight);
                bmp.SetSource(stream);
            }
            return bmp;
        }
Ejemplo n.º 9
0
        private static async Task <Bitmap> CreateFromStream(IRandomAccessStream stream)
        {
            var decoder = await BitmapDecoder.CreateAsync(BitmapDecoder.PngDecoderId, stream);

            var frame = await decoder.GetFrameAsync(0);

            var pixelData = await frame.GetPixelDataAsync();

            var bytes = pixelData.DetachPixelData();

            return(new Bitmap
            {
                Bytes = bytes,
                Width = (int)frame.PixelWidth,
                Height = (int)frame.PixelHeight,
            });
        }
Ejemplo n.º 10
0
        public async Task <string> ExtractText(Bitmap bmp, string languageCode, System.Windows.Point?singlePoint = null)
        {
            if (!GlobalizationPreferences.Languages.Contains(languageCode))
            {
                throw new ArgumentOutOfRangeException($"{languageCode} is not installed.");
            }

            StringBuilder text = new StringBuilder();

            await using (MemoryStream memory = new MemoryStream())
            {
                bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
                memory.Position = 0;
                BitmapDecoder bmpDecoder = await BitmapDecoder.CreateAsync(memory.AsRandomAccessStream());

                Windows.Graphics.Imaging.SoftwareBitmap softwareBmp = await bmpDecoder.GetSoftwareBitmapAsync();

                OcrEngine ocrEngine = OcrEngine.TryCreateFromLanguage(new Language(languageCode));
                OcrResult ocrResult = await ocrEngine.RecognizeAsync(softwareBmp);

                if (singlePoint == null)
                {
                    foreach (OcrLine line in ocrResult.Lines)
                    {
                        text.AppendLine(line.Text);
                    }
                }
                else
                {
                    Windows.Foundation.Point fPoint = new Windows.Foundation.Point(singlePoint.Value.X, singlePoint.Value.Y);
                    foreach (OcrLine ocrLine in ocrResult.Lines)
                    {
                        foreach (OcrWord ocrWord in ocrLine.Words)
                        {
                            if (ocrWord.BoundingRect.Contains(fPoint))
                            {
                                text.Append(ocrWord.Text);
                            }
                        }
                    }
                }
            }

            return(text.ToString());
        }
Ejemplo n.º 11
0
        private async void GetPhotoButton_Click(object sender, RoutedEventArgs e)
        {
            // This is to select a file to open.
            //Windows.Storage.Pickers.FileOpenPicker openPicker = new Windows.Storage.Pickers.FileOpenPicker();
            openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            openPicker.ViewMode = PickerViewMode.Thumbnail;

            // Filter to include a sample subset of file types.
            openPicker.FileTypeFilter.Clear();
            openPicker.FileTypeFilter.Add(".bmp");
            openPicker.FileTypeFilter.Add(".png");
            openPicker.FileTypeFilter.Add(".jpeg");
            openPicker.FileTypeFilter.Add(".jpg");

            // Open the file picker .
            photo = await openPicker.PickSingleFileAsync();

            // The file is null if user cancels the file picker.
            if (photo != null)
            {
                // Here we open a stream for the selected file.
                // The 'using' block ensures the stream is disposed
                // after the image is loaded.
                imageStream = await photo.OpenAsync(FileAccessMode.Read);

                Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(imageStream);

                Windows.Graphics.Imaging.SoftwareBitmap softwareBitmap = await decoder.GetSoftwareBitmapAsync();

                Windows.Graphics.Imaging.SoftwareBitmap softwareBitmapBRG = Windows.Graphics.Imaging.SoftwareBitmap.Convert(softwareBitmap, Windows.Graphics.Imaging.BitmapPixelFormat.Bgra8,
                                                                                                                            Windows.Graphics.Imaging.BitmapAlphaMode.Premultiplied);
                SoftwareBitmapSource bitmapSource = new SoftwareBitmapSource();
                await bitmapSource.SetBitmapAsync(softwareBitmapBRG);

                image.Source = bitmapSource;
            }
        }
Ejemplo n.º 12
0
        public async Task <string> ExtractText(Bitmap bmp, string languageCode, System.Windows.Point?singlePoint = null)
        {
            Language        selectedLanguage = new Language(languageCode);
            List <Language> possibleOCRLangs = OcrEngine.AvailableRecognizerLanguages.ToList();

            if (possibleOCRLangs.Count < 1)
            {
                throw new ArgumentOutOfRangeException($"No possible OCR languages are installed.");
            }

            if (possibleOCRLangs.Where(l => l.LanguageTag == selectedLanguage.LanguageTag).Count() < 1)
            {
                List <Language> similarLanguages = possibleOCRLangs.Where(la => la.AbbreviatedName == selectedLanguage.AbbreviatedName).ToList();
                if (similarLanguages.Count() > 0)
                {
                    selectedLanguage = similarLanguages.FirstOrDefault();
                }
                else
                {
                    selectedLanguage = possibleOCRLangs.FirstOrDefault();
                }
            }

            bool scaleBMP = true;

            if (singlePoint != null ||
                bmp.Width * 1.5 > OcrEngine.MaxImageDimension)
            {
                scaleBMP = false;
            }

            Bitmap scaledBitmap;

            if (scaleBMP)
            {
                scaledBitmap = ScaleBitmapUniform(bmp, 1.5);
            }
            else
            {
                scaledBitmap = ScaleBitmapUniform(bmp, 1.0);
            }

            StringBuilder text = new StringBuilder();

            XmlLanguage lang    = XmlLanguage.GetLanguage(languageCode);
            CultureInfo culture = lang.GetEquivalentCulture();

            await using (MemoryStream memory = new MemoryStream())
            {
                scaledBitmap.Save(memory, ImageFormat.Bmp);
                memory.Position = 0;
                BitmapDecoder bmpDecoder = await BitmapDecoder.CreateAsync(memory.AsRandomAccessStream());

                Windows.Graphics.Imaging.SoftwareBitmap softwareBmp = await bmpDecoder.GetSoftwareBitmapAsync();

                OcrEngine ocrEngine = OcrEngine.TryCreateFromLanguage(selectedLanguage);
                OcrResult ocrResult = await ocrEngine.RecognizeAsync(softwareBmp);

                if (singlePoint == null)
                {
                    foreach (OcrLine line in ocrResult.Lines)
                    {
                        text.AppendLine(line.Text);
                    }
                }
                else
                {
                    Windows.Foundation.Point fPoint = new Windows.Foundation.Point(singlePoint.Value.X, singlePoint.Value.Y);
                    foreach (OcrLine ocrLine in ocrResult.Lines)
                    {
                        foreach (OcrWord ocrWord in ocrLine.Words)
                        {
                            if (ocrWord.BoundingRect.Contains(fPoint))
                            {
                                text.Append(ocrWord.Text);
                            }
                        }
                    }
                }
            }
            if (culture.TextInfo.IsRightToLeft)
            {
                List <string> textListLines = text.ToString().Split(new char[] { '\n', '\r' }).ToList();

                text.Clear();
                foreach (string textLine in textListLines)
                {
                    List <string> wordArray = textLine.Split().ToList();
                    wordArray.Reverse();
                    text.Append(string.Join(' ', wordArray));

                    if (textLine.Length > 0)
                    {
                        text.Append('\n');
                    }
                }
                return(text.ToString());
            }
            else
            {
                return(text.ToString());
            }
        }