private CreateAsync ( [ stream ) : IAsyncOperation |
||
stream | [ | |
Résultat | IAsyncOperation |
public static async Task <OcrResult?> GetOcrResultFromRegion(Rectangle region) { Language?selectedLanguage = GetOCRLanguage(); if (selectedLanguage == null) { return(null); } Bitmap bmp = new(region.Width, region.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bmp); g.CopyFromScreen(region.Left, region.Top, 0, 0, bmp.Size, CopyPixelOperation.SourceCopy); OcrResult?ocrResult; await using (MemoryStream memory = new()) { bmp.Save(memory, ImageFormat.Bmp); memory.Position = 0; BitmapDecoder bmpDecoder = await BitmapDecoder.CreateAsync(memory.AsRandomAccessStream()); SoftwareBitmap softwareBmp = await bmpDecoder.GetSoftwareBitmapAsync(); OcrEngine ocrEngine = OcrEngine.TryCreateFromLanguage(selectedLanguage); ocrResult = await ocrEngine.RecognizeAsync(softwareBmp); } return(ocrResult); }
public async Task <IList <DetectedFace> > DetectFaces(Stream fileStream) { var stream = fileStream.AsRandomAccessStream(); var bitmapDecoder = await BitmapDecoder.CreateAsync(stream); using SoftwareBitmap bitmap = await bitmapDecoder.GetSoftwareBitmapAsync(); var bmp = FaceDetector.IsBitmapPixelFormatSupported(bitmap.BitmapPixelFormat) ? bitmap : SoftwareBitmap.Convert(bitmap, BitmapPixelFormat.Gray8); var faceDetector = await FaceDetector.CreateAsync(); var detectedFaces = await faceDetector.DetectFacesAsync(bmp); return(detectedFaces); }
public async Task <SoftwareBitmap> GetSoftwareSnapShot(int pattern) { var target = this.GetTargetWindow(); var bitmapSource = target?.GetClientBitmap(); var bitmap = new System.Drawing.Bitmap( bitmapSource.PixelWidth, bitmapSource.PixelHeight, System.Drawing.Imaging.PixelFormat.Format32bppPArgb ); var bitmapData = bitmap.LockBits( new System.Drawing.Rectangle(System.Drawing.Point.Empty, bitmap.Size), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb ); bitmapSource.CopyPixels( System.Windows.Int32Rect.Empty, bitmapData.Scan0, bitmapData.Height * bitmapData.Stride, bitmapData.Stride ); bitmap.UnlockBits(bitmapData); bitmap = trimmingBitmap(bitmap, pattern); var folder = Directory.GetCurrentDirectory(); var imageName = "ScreenCapture.bmp"; StorageFolder appFolder = await StorageFolder.GetFolderFromPathAsync(@folder); bitmap.Save(folder + "\\" + imageName, ImageFormat.Bmp); SoftwareBitmap softwareBitmap; var bmpFile = await appFolder.GetFileAsync(imageName); using (IRandomAccessStream stream = await bmpFile.OpenAsync(FileAccessMode.Read)) { BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream); softwareBitmap = await decoder.GetSoftwareBitmapAsync(); } File.Delete(folder + "\\" + imageName); return(softwareBitmap); }
/// <summary> /// 指定範囲のキャプチャ画像をSoftwareBitmap型として得る /// </summary> /// <param name="left">キャプチャ範囲の左端端x座標</param> /// <param name="top">キャプチャ範囲の左端端y座標</param> /// <param name="height">キャプチャ範囲の高さ</param> /// <param name="width">キャプチャ範囲の幅</param> /// <returns>指定された範囲のキャプチャ画像</returns> public static async Task <SoftwareBitmap> CaptureAsSoftwareBitmap(int left, int top, int height, int width) { //ビットマップの保持領域を確保 Bitmap partialCapture = new Bitmap(width, height); //描画インターフェイスの設定 Graphics draw = Graphics.FromImage(partialCapture); //画面全体をコピーする draw.CopyFromScreen( left, top, 0, 0, partialCapture.Size ); //解放 draw.Dispose(); MemoryStream memStream = new MemoryStream(); partialCapture.Save(memStream, ImageFormat.Bmp); SoftwareBitmap softwareBitmap; using (var randomAccessStream = new UwpInMemoryRandomAccessStream()) { using (var outputStream = randomAccessStream.GetOutputStreamAt(0)) using (var writer = new UwpDataWriter(outputStream)) { writer.WriteBytes(memStream.ToArray()); await writer.StoreAsync(); await outputStream.FlushAsync(); } // IRandomAccessStreamをSoftwareBitmapに変換 // (ここはUWP APIのデコーダーを使う) var decoder = await UwpBitmapDecoder.CreateAsync(randomAccessStream); softwareBitmap = await decoder.GetSoftwareBitmapAsync(UwpBitmapPixelFormat.Bgra8, UwpBitmapAlphaMode.Premultiplied); } return(softwareBitmap); }
/// <summary> /// Convert BitmapImage to SoftwareBitmap. /// </summary> /// <param name="bitmap">BitmapImage to be convereted.</param> /// <returns>Converted SoftwareBitmap instance.</returns> public static async Task <SoftwareBitmap> ToSoftwareBitmap(this BitmapImage bitmap) { try { using (var stream = await ToRandomAccessStream((MemoryStream)bitmap.StreamSource)) { // Create the decoder from the stream var decoder = await BitmapDecoder.CreateAsync(stream); // Get the SoftwareBitmap representation of the file return(await decoder.GetSoftwareBitmapAsync()); } } catch (Exception e) { MessageBox.Show(e.Message); } return(null); }
public static async Task <string> ExtractText(Bitmap bmp, System.Windows.Point?singlePoint = null) { Language?selectedLanguage = GetOCRLanguage(); if (selectedLanguage == null) { return(""); } XmlLanguage lang = XmlLanguage.GetLanguage(selectedLanguage.LanguageTag); CultureInfo culture = lang.GetEquivalentCulture(); 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(); await using (MemoryStream memory = new()) { scaledBitmap.Save(memory, ImageFormat.Bmp); memory.Position = 0; BitmapDecoder bmpDecoder = await BitmapDecoder.CreateAsync(memory.AsRandomAccessStream()); 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) { string[] textListLines = text.ToString().Split(new char[] { '\n', '\r' }); _ = 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()); } }
/// <summary> /// Convert Bitmap to SoftwareBitmap. /// Beneficial for use in UWP framework and API. /// </summary> /// <param name="bitmap">Bitmap to be converted.</param> /// <returns>Converted SoftwareBitmap instance.</returns> public static async Task <SoftwareBitmap> ToSoftwareBitmap(this Bitmap bitmap) { try { // Create new MemoryStream for original bitmap data. using (var memoryStream = new MemoryStream()) { // Save data to memory stream. bitmap.Save(memoryStream, ImageFormat.Bmp); // Reset position. //memoryStream.Position = 0; // Convert MemoryStream to RandomAccessStream. using (var randomAccessStream = await memoryStream.ToRandomAccessStream()) { // Create the decoder from the stream. var decoder = await BitmapDecoder.CreateAsync(randomAccessStream); // Get the SoftwareBitmap representation of the file. return(await decoder.GetSoftwareBitmapAsync()); } } } catch (Exception e) { MessageBox.Show(e.Message); } return(null); }