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); } }
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, }); }
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()); }
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()); } }