Inheritance: IBitmapDecoder, IBitmapFrame
		/// <summary>
		/// Use BitmapTransform to define the region to crop, and then get the pixel data in the region.
		/// If you want to get the pixel data of a scaled image, set the scaledWidth and scaledHeight
		/// of the scaled image.
		/// </summary>
		/// <returns></returns>
		async static private Task<byte[]> GetPixelData(BitmapDecoder decoder, uint startPointX, uint startPointY,
			uint width, uint height, uint scaledWidth, uint scaledHeight)
		{

			BitmapTransform transform = new BitmapTransform();
			BitmapBounds bounds = new BitmapBounds();
			bounds.X = startPointX;
			bounds.Y = startPointY;
			bounds.Height = height;
			bounds.Width = width;
			transform.Bounds = bounds;

			transform.ScaledWidth = scaledWidth;
			transform.ScaledHeight = scaledHeight;

			// Get the cropped pixels within the bounds of transform.
			PixelDataProvider pix = await decoder.GetPixelDataAsync(
				BitmapPixelFormat.Bgra8,
				BitmapAlphaMode.Straight,
				transform,
				ExifOrientationMode.IgnoreExifOrientation,
				ColorManagementMode.ColorManageToSRgb);
			byte[] pixels = pix.DetachPixelData();
			return pixels;
		}
Esempio n. 2
0
        public static async Task <string> GetFullText2(string pathToImg)
        {
            Bitmap bmp    = new Bitmap(pathToImg);
            int    width  = bmp.Width;
            int    height = bmp.Height;


            using (Graphics g = Graphics.FromImage(bmp))
            {
                // copy rectangle from screen (doesn't include cursor)

                using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
                {
                    //These steps to get to a SoftwareBitmap are aweful!
                    bmp.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);

                    SoftwareBitmap bitmap = await decoder.GetSoftwareBitmapAsync();

                    OcrResult ocrResult = await _ocrEngine.RecognizeAsync(bitmap);

                    for (int i = 0; i < ocrResult.Lines.Count; i++)
                    {
                        OcrLine line = ocrResult.Lines[i];
                    }
                }
            }
            return(string.Empty);
        }
Esempio n. 3
0
        private async void OnRecognize(object sender, RoutedEventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();

            if (dialog.ShowDialog() == true)
            {
                string fileName            = dialog.FileName;
                var    selectedStorageFile = await StorageFile.GetFileFromPathAsync(dialog.FileName);

                SoftwareBitmap softwareBitmap;

                using (IRandomAccessStream stream = await selectedStorageFile.OpenAsync(FileAccessMode.Read))
                {
                    // Create the decoder from the stream
                    Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);

                    // Get the SoftwareBitmap representation of the file in BGRA8 format
                    softwareBitmap = await decoder.GetSoftwareBitmapAsync();

                    softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
                }

                // Display the image
                //SoftwareBitmapSource imageSource = new SoftwareBitmapSource();
                //await imageSource.SetBitmapAsync(softwareBitmap);

                //PreviewImage.Source = imageSource;

                PreviewImage.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri(fileName));

                // Encapsulate the image in the WinML image type (VideoFrame) to be bound and evaluated
                VideoFrame inputImage = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap);
                await EvaluateVideoFrameAsync(inputImage);
            }
        }
        private async void OnCaptureButton_Click(Object sender, RoutedEventArgs e)
        {
            Visibility = Visibility.Hidden;
            try
            {
                CaptureWindow captureWindow = new CaptureWindow();
                Boolean       result        = captureWindow.ShowDialog().Value;

                if (result)
                { // The region is captured. Run OCR and Translate
                    MemoryStream ms;
                    var          bitmapSource = CopyScreen(captureWindow.Rect, out ms);

                    captureImage.Source = bitmapSource;
                    Visibility          = Visibility.Visible;
                    initialized         = true;

                    InMemoryRandomAccessStream inMemoryStream = new InMemoryRandomAccessStream();
                    ms.Position = 0;
                    Byte[] byteArray = ms.GetBuffer();
                    inMemoryStream.AsStream().Write(byteArray, 0, byteArray.Length);
                    inMemoryStream.AsStream().Flush();

                    Windows.Graphics.Imaging.BitmapDecoder bitmapDecoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(inMemoryStream);

                    SoftwareBitmap softwareBitmap = await bitmapDecoder.GetSoftwareBitmapAsync().AsTask().ConfigureAwait(true);

                    captureImage.Tag = softwareBitmap; // for later use

                    await RunOcrAndTranslate();
                }
            }
            catch { }
            Visibility = Visibility.Visible;
        }
Esempio n. 5
0
        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);
        }
Esempio n. 6
0
        public static async Task <string> GetFullTextFromImage(Image img)
        {
            try
            {
                StringBuilder b = new StringBuilder();

                using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
                {
                    img.Save(stream.AsStream(), System.Drawing.Imaging.ImageFormat.Bmp);
                    //These steps to get to a SoftwareBitmap are aweful!
                    Windows.Graphics.Imaging.BitmapDecoder decoder = await Windows.Graphics.Imaging.BitmapDecoder.CreateAsync(stream);

                    SoftwareBitmap bitmap = await decoder.GetSoftwareBitmapAsync();

                    OcrResult ocrResult = await _ocrEngine.RecognizeAsync(bitmap);

                    for (int i = 0; i < ocrResult.Lines.Count; i++)
                    {
                        b.Append(ocrResult.Lines[i].Text);
                        b.Append(" ");
                    }
                }
                return(b.ToString());
            }catch (Exception ex)
            {
                Logger.Error(ex.ToString());
            }
            return(string.Empty);
        }
Esempio n. 7
0
        public async Task <SoftwareBitmap> CreateSoftwareBitmap(Windows.Storage.StorageFile file, BitmapImage bitmap)
        {
            Windows.Storage.Streams.IRandomAccessStream random = await Windows.Storage.Streams.RandomAccessStreamReference.CreateFromFile(file).OpenReadAsync();

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

            var swBitmap = new SoftwareBitmap(BitmapPixelFormat.Rgba8, bitmap.PixelWidth, bitmap.PixelHeight);

            return(swBitmap = await decoder.GetSoftwareBitmapAsync());
        }
Esempio n. 8
0
        public static async Task <byte[]> ImageToByteArray(Uri uri)
        {
            Windows.Storage.Streams.IRandomAccessStream random = await Windows.Storage.Streams.RandomAccessStreamReference.CreateFromUri(uri).OpenReadAsync();

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

            Windows.Graphics.Imaging.PixelDataProvider pixelData = await decoder.GetPixelDataAsync();

            return(pixelData.DetachPixelData());
        }
Esempio n. 9
0
        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);
        }
Esempio n. 10
0
        private async Task SetImageSourceAsync(BitmapDecoder decoder, byte[] bitmapData)
        {
            using (IRandomAccessStream outputStream = new InMemoryRandomAccessStream())
            {
                BitmapEncoder encoder = await BitmapEncoder.CreateForTranscodingAsync(outputStream, decoder);
                encoder.SetPixelData(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, decoder.PixelWidth, 
                    decoder.PixelHeight, decoder.DpiX, decoder.DpiY, bitmapData);
                await encoder.FlushAsync();

                
                BitmapImage bitmapImage = new BitmapImage();
                await bitmapImage.SetSourceAsync(outputStream);

                _image.Source = bitmapImage;
            }
        }
Esempio n. 11
0
        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);
        }
Esempio n. 12
0
    /// <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);
    }
Esempio n. 13
0
        /// <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);
        }
		async public static Task<WriteableBitmap> GetCroppedBitmapAsync(BitmapDecoder decoder,
			Point startPoint, Size corpSize, double scale)
		{
			if (double.IsNaN(scale) || double.IsInfinity(scale))
			{
				scale = 1;
			}

			// Convert start point and size to integer.
			uint startPointX = (uint)Math.Floor(startPoint.X * scale);
			uint startPointY = (uint)Math.Floor(startPoint.Y * scale);
			uint height = (uint)Math.Floor(corpSize.Height * scale);
			uint width = (uint)Math.Floor(corpSize.Width * scale);

			// The scaledSize of original image.
			uint scaledWidth = (uint)Math.Floor(decoder.PixelWidth * scale);
			uint scaledHeight = (uint)Math.Floor(decoder.PixelHeight * scale);


			// Refine the start point and the size. 
			if (startPointX + width > scaledWidth)
			{
				startPointX = scaledWidth - width;
			}

			if (startPointY + height > scaledHeight)
			{
				startPointY = scaledHeight - height;
			}

			// Get the cropped pixels.
			byte[] pixels = await GetPixelData(decoder, startPointX, startPointY, width, height,
				scaledWidth, scaledHeight);

			// Stream the bytes into a WriteableBitmap
			WriteableBitmap cropBmp = new WriteableBitmap((int)width, (int)height);
			Stream pixStream = cropBmp.PixelBuffer.AsStream();
			pixStream.Write(pixels, 0, (int)(width * height * 4));

			return cropBmp;
		}
Esempio n. 15
0
 private void Dispose(bool disposing)
 {
     if (disposing)
     {
         this.Stop();
         _isInitialized = false;
         _hasCanvasResources = false;
         _bitmapDecoder = null;
         if (_frameProperties != null)
         {
             foreach (var item in _frameProperties)
             {
                 item.ReleasePixels();
             }
             _frameProperties.Clear();
             _frameProperties = null;
         }
         _canvasImageSource = null;
         _accumulationRenderTarget = null;
         _animationTimer = null;
     }
 }
            public async Task<ImageSource> InitializeAsync(IRandomAccessStream streamSource)
            {
                var bitmapDecoder = await BitmapDecoder.CreateAsync(BitmapDecoder.GifDecoderId, streamSource);
                var imageProperties = await RetrieveImagePropertiesAsync(bitmapDecoder);
                var frameProperties = new List<FrameProperties>();

                for (var i = 0u; i < bitmapDecoder.FrameCount; i++)
                {
                    var bitmapFrame = await bitmapDecoder.GetFrameAsync(i);
                    frameProperties.Add(await RetrieveFramePropertiesAsync(bitmapFrame));
                }

                _frameProperties = frameProperties;
                _bitmapDecoder = bitmapDecoder;
                _imageProperties = imageProperties;

                CreateCanvasResources();

                _isInitialized = true;
                return _canvasImageSource;
            }
            public async Task<ImageSource> InitializeAsync(IRandomAccessStream streamSource)
            {
                var bitmapDecoder = await BitmapDecoder.CreateAsync(BitmapDecoder.GifDecoderId, streamSource);
                var imageProperties = await RetrieveImagePropertiesAsync(bitmapDecoder);

                _frameProperties = new FrameProperties?[bitmapDecoder.FrameCount];
                _bitmapDecoder = bitmapDecoder;
                _imageProperties = imageProperties;

                CreateCanvasResources();

                _isInitialized = true;
                return _canvasImageSource;
            }
Esempio n. 18
0
 private void Dispose(bool disposing)
 {
     if (disposing)
     {
         this.Stop();
         _isInitialized = false;
         _hasCanvasResources = false;
         _bitmapDecoder = null;
         _frameProperties?.Clear();
         _frameProperties = null;
         _canvasImageSource = null;
         _accumulationRenderTarget = null;
         _animationTimer = null;
     }
 }
        /// <summary>
        /// Computes a BitmapTransform to downscale the source image if it's too large. 
        /// </summary>
        /// <remarks>
        /// Performance of the FaceDetector degrades significantly with large images, and in most cases it's best to downscale
        /// the source bitmaps if they're too large before passing them into FaceDetector. Remember through, your application's performance needs will vary.
        /// </remarks>
        /// <param name="sourceDecoder">Source image decoder</param>
        /// <returns>A BitmapTransform object holding scaling values if source image is too large</returns>
        private BitmapTransform ComputeScalingTransformForSourceImage(BitmapDecoder sourceDecoder)
        {
            BitmapTransform transform = new BitmapTransform();

            if (sourceDecoder.PixelHeight > this.sourceImageHeightLimit)
            {
                float scalingFactor = (float)this.sourceImageHeightLimit / (float)sourceDecoder.PixelHeight;

                transform.ScaledWidth = (uint)Math.Floor(sourceDecoder.PixelWidth * scalingFactor);
                transform.ScaledHeight = (uint)Math.Floor(sourceDecoder.PixelHeight * scalingFactor);
            }

            return transform;
        }
Esempio n. 20
0
 public async Task<byte[]> DecodeAsync(BitmapDecoder bitmapDecoder)
 {
     if (_isDecode)
     {
         return this.Pixels;
     }
     var frame = await bitmapDecoder.GetFrameAsync(FrameIndex).AsTask().ConfigureAwait(false);
     var pixelData = await frame.GetPixelDataAsync(
         BitmapPixelFormat.Bgra8,
         BitmapAlphaMode.Premultiplied,
         new BitmapTransform(),
         ExifOrientationMode.IgnoreExifOrientation,
         ColorManagementMode.DoNotColorManage
         ).AsTask().ConfigureAwait(false);
     this.Pixels = pixelData.DetachPixelData();
     _isDecode = true;
     return Pixels;
 }
Esempio n. 21
0
 protected async Task<byte[]> getPixelData(BitmapDecoder decoder)
 {
     PixelDataProvider pixelProvider = await decoder.GetPixelDataAsync(
         BitmapPixelFormat.Rgba8,
         BitmapAlphaMode.Straight,
         new BitmapTransform(),
         ExifOrientationMode.RespectExifOrientation,
         ColorManagementMode.ColorManageToSRgb
         );
     return pixelProvider.DetachPixelData();
 }
Esempio n. 22
0
 private static bool IsGifImage(BitmapDecoder decoder)
 {
     foreach (var mimeType in decoder.DecoderInformation.MimeTypes)
     {
         if (mimeType.Equals("image/gif", StringComparison.OrdinalIgnoreCase))
         {
             return true;
         }
     }
     return false;
 }
            private static async Task<ImageProperties> RetrieveImagePropertiesAsync(BitmapDecoder bitmapDecoder)
            {
                // Properties not currently supported: background color, pixel aspect ratio.
                const string widthProperty = "/logscrdesc/Width";
                const string heightProperty = "/logscrdesc/Height";
                const string applicationProperty = "/appext/application";
                const string dataProperty = "/appext/data";

                var propertiesView = bitmapDecoder.BitmapContainerProperties;
                var requiredProperties = new[] { widthProperty, heightProperty };
                var properties = await propertiesView.GetPropertiesAsync(requiredProperties);

                var pixelWidth = (ushort)properties[widthProperty].Value;
                var pixelHeight = (ushort)properties[heightProperty].Value;

                var loopCount = 0; // Repeat forever by default
                var isAnimated = true;

                try
                {
                    var extensionProperties = new[] { applicationProperty, dataProperty };
                    properties = await propertiesView.GetPropertiesAsync(extensionProperties);

                    if (properties.ContainsKey(applicationProperty) &&
                        properties[applicationProperty].Type == PropertyType.UInt8Array)
                    {
                        var bytes = (byte[])properties[applicationProperty].Value;
                        var applicationName = Encoding.UTF8.GetString(bytes, 0, bytes.Length);

                        if (applicationName == "NETSCAPE2.0" || applicationName == "ANIMEXTS1.0")
                        {
                            if (properties.ContainsKey(dataProperty) && properties[dataProperty].Type == PropertyType.UInt8Array)
                            {
                                //  The data is in the following format: 
                                //  byte 0: extsize (must be > 1) 
                                //  byte 1: loopType (1 == animated gif) 
                                //  byte 2: loop count (least significant byte) 
                                //  byte 3: loop count (most significant byte) 
                                //  byte 4: set to zero 

                                var data = (byte[])properties[dataProperty].Value;
                                loopCount = data[2] | data[3] << 8;
                                isAnimated = data[1] == 1;
                            }
                        }
                    }
                }
                catch
                {
                    // These properties are not required, so it's okay to ignore failure.
                }

                return new ImageProperties(pixelWidth, pixelHeight, isAnimated, loopCount);
            }
Esempio n. 24
0
		private async static Task<byte[]> ImageToScaledPixels(BitmapDecoder decoder, uint newWidth)
		{
			using (var encoderStream = new InMemoryRandomAccessStream())
			{
				var encoder = await BitmapEncoder.CreateForTranscodingAsync(encoderStream, decoder);
				var scaleFactor = decoder.PixelWidth / (double)newWidth;
				uint newHeight = (uint)(decoder.PixelHeight / scaleFactor);
				encoder.BitmapTransform.ScaledWidth = newWidth;
				encoder.BitmapTransform.ScaledHeight = newHeight;

				await encoder.FlushAsync();
				byte[] pixels = new byte[encoderStream.Size];
				await encoderStream.ReadAsync(pixels.AsBuffer(), (uint)pixels.Length, InputStreamOptions.None);

				return pixels;
			}
		}
Esempio n. 25
0
        private static async Task ResizeImageAsync(BitmapDecoder decoder, StorageFile targetFile, Size finalSize)
        {
            await _semaphore.WaitAsync();

            try
            {
                using (var fileStream = await targetFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var encoder = await BitmapEncoder.CreateForTranscodingAsync(fileStream, decoder);
                    encoder.BitmapTransform.ScaledWidth = (uint)finalSize.Width;
                    encoder.BitmapTransform.ScaledHeight = (uint)finalSize.Height;
                    encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Fant;
                    await encoder.FlushAsync();
                }
            }
            catch
            {
                throw;
            }
            finally
            {
                _semaphore.Release();
            }
        }
Esempio n. 26
0
        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());
            }
        }
        public async Task LoadImageAsync(IRandomAccessStreamWithContentType stream)
        {
            var decoder = await BitmapDecoder.CreateAsync(BitmapDecoder.GifDecoderId, stream);
            var imageProperties = await _gifPropertiesHelper.RetrieveImagePropertiesAsync(decoder);
            var frameProperties = new List<FrameProperties>();

            for (var i = 0; i < decoder.FrameCount; i++)
            {
                var bitmapFrame = await decoder.GetFrameAsync((uint)i);
                frameProperties.Add(await _gifPropertiesHelper.RetrieveFramePropertiesAsync(bitmapFrame, i));
            }

            _decoder = decoder;
            _imageProperties = imageProperties;
            _frameProperties = frameProperties;

            CreateImageBuffer();
        }
        private async void loadnew()
        {

            Assembly assembly = typeof(Clr_Pckr).GetTypeInfo().Assembly;
            //Stream imgStream =  assembly.GetManifestResourceStream("Color_Picker.Assets.c.png");


            InMemoryRandomAccessStream res = new InMemoryRandomAccessStream();

            using (var imgstream = assembly.GetManifestResourceStream("Dev.MartijnHoogendoorn.Inking.Behavior.Assets.c.png"))
            {
                await imgstream.CopyToAsync(res.AsStreamForWrite());
            }

           BitmapImage bmp  = new BitmapImage();
           

                                 
           
                using (IRandomAccessStream fileStream =res)// await( await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/c.png"))).OpenAsync(FileAccessMode.Read))
                {
                    BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);

                    fileStream.Seek(0);


                    bmp.SetSource(fileStream);
                    ColorImg.Source = bmp;


                    BitmapTransform bt = new BitmapTransform();

                    bt.ScaledHeight = 100;
                    bt.ScaledWidth = 100;



                    PixelDataProvider pixelData = await decoder.GetPixelDataAsync(
                        BitmapPixelFormat.Bgra8,
                        BitmapAlphaMode.Straight,
                       bt,
                        ExifOrientationMode.IgnoreExifOrientation,
                        ColorManagementMode.DoNotColorManage);
                    bd = decoder;

                    //byte[] sourcePixels = pixelData.DetachPixelData();
                    tempBuffer = pixelData.DetachPixelData();
                }
           

        }
Esempio n. 29
0
        public async Task<ImageSource> InitializeAsync(CoreDispatcher dispatcher,
            IRandomAccessStream streamSource, CancellationTokenSource cancellationTokenSource)
        {

            var inMemoryStream = new InMemoryRandomAccessStream();
            var copyAction = RandomAccessStream.CopyAndCloseAsync(
                           streamSource.GetInputStreamAt(0L),
                           inMemoryStream.GetOutputStreamAt(0L));
            await copyAction.AsTask(cancellationTokenSource.Token);

            var bitmapDecoder = await BitmapDecoder.
                CreateAsync(BitmapDecoder.GifDecoderId, inMemoryStream).AsTask(cancellationTokenSource.Token).ConfigureAwait(false);
            var imageProperties = await RetrieveImagePropertiesAsync(bitmapDecoder);
            var frameProperties = new List<FrameProperties>();
            for (var i = 0u; i < bitmapDecoder.FrameCount; i++)
            {
                var bitmapFrame = await bitmapDecoder.GetFrameAsync(i).AsTask(cancellationTokenSource.Token).ConfigureAwait(false); ;
                frameProperties.Add(await RetrieveFramePropertiesAsync(i, bitmapFrame));
            }

            _frameProperties = frameProperties;
            _bitmapDecoder = bitmapDecoder;
            _imageProperties = imageProperties;
            _dispatcher = dispatcher;

            await _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
             {
                 CreateCanvasResources();
             });

            _isInitialized = true;
            return _canvasImageSource;
        }
Esempio n. 30
0
 /// <summary>
 /// Use BitmapTransform to define the region to crop, and then get the pixel data in the region
 /// </summary>
 /// <returns></returns>
 async static private Task<byte[]> GetPixelData(BitmapDecoder decoder, uint startPointX, uint startPointY,
     uint width, uint height)
 {
     return await GetPixelData(decoder,startPointX, startPointY, width, height,
         decoder.PixelWidth,decoder.PixelHeight);
 }
Esempio n. 31
0
        public async Task<ImagePackage> InitializeAsync(CoreDispatcher dispatcher, Image image, Uri uriSource,
            IRandomAccessStream streamSource, CancellationTokenSource cancellationTokenSource)
        {
   
            var bitmapDecoder = ImagingCache.Get<BitmapDecoder>(uriSource);
            if (bitmapDecoder == null)
            {
                var inMemoryStream = new InMemoryRandomAccessStream();
                var copyAction = RandomAccessStream.CopyAndCloseAsync(
                               streamSource.GetInputStreamAt(0L),
                               inMemoryStream.GetOutputStreamAt(0L));
                await copyAction.AsTask(cancellationTokenSource.Token);
                bitmapDecoder = await BitmapDecoder.CreateAsync(BitmapDecoder.GifDecoderId, inMemoryStream);
                ImagingCache.Add(uriSource, bitmapDecoder);
            }

            var imageProperties = await RetrieveImagePropertiesAsync(bitmapDecoder);
            var frameProperties = new List<FrameProperties>();

            for (var i = 0u; i < bitmapDecoder.FrameCount; i++)
            {
                var bitmapFrame = await bitmapDecoder.GetFrameAsync(i).AsTask(cancellationTokenSource.Token).ConfigureAwait(false); ;
                frameProperties.Add(await RetrieveFramePropertiesAsync(i, bitmapFrame));
            }

            _frameProperties = frameProperties;
            _bitmapDecoder = bitmapDecoder;
            _imageProperties = imageProperties;
            _dispatcher = dispatcher;

            await _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
             {
                 CreateCanvasResources();
                 var uri = image.Tag as Uri;
                 if (uri == uriSource)
                 {
                     image.Source = _canvasImageSource;
                 }
             });

            _isInitialized = true;
            return new ImagePackage(this, _canvasImageSource, _imageProperties.PixelWidth, _imageProperties.PixelHeight); ;
        }
Esempio n. 32
-1
        /// <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);
        }