-1

I'm working with Microsoft's OCR library and am having problems converting the BitmapImage to a pixel array.

I'm making this application for Windows Phone 8, and WriteableBitmap.PixelBuffer.ToArray() isn't an option so I have a static function that'll change a normal BitmapImage into a byte array to feed into the OCR engine.

Well, every time I feed it in the application crashes. What's wrong here?

Here is my static class with the bitmap converter

 public static class ByteArrayChange
    {
        public static byte[] ConvertToBytes(this BitmapImage bitmapImage)
        {
            byte[] data = null;
            using (MemoryStream stream = new MemoryStream())
            {
                WriteableBitmap wBitmap = new WriteableBitmap(bitmapImage);
                wBitmap.SaveJpeg(stream, wBitmap.PixelWidth, wBitmap.PixelHeight, 0, 100);
                stream.Seek(0, SeekOrigin.Begin);
                data = stream.GetBuffer();
            }

            return data;
        }
    }

Here is the piece of code in the OCR method that's causing the application to crash.

 byte[] pa = ByteArrayChange.ConvertToBytes(bitmap);

            //Here Is he problem 
            var ocrResult = await ocrEngine.RecognizeAsync((uint)bitmap.PixelHeight, (uint)bitmap.PixelWidth, pa);

What am I doing wrong here? Thanks!

Wezley
  • 413
  • 1
  • 3
  • 19
  • [`RecognizeAsync`](http://msdn.microsoft.com/en-us/library/windowspreview.media.ocr.ocrengine.recognizeasync.aspx) says it takes "The bytes of the image in BGRA8 format." You're passing a byte array with JPG format. – dbc Oct 16 '14 at 16:14
  • `WriteableBitmap` has a [`Pixels`](http://msdn.microsoft.com/en-us/library/windows/apps/system.windows.media.imaging.writeablebitmap.pixels%28v=vs.105%29.aspx) property. Is that what you want? – dbc Oct 16 '14 at 16:17
  • Dbc, how do I make it turn them into BGRA8 format? and for WriteableBitmap it needs the PixelBuffer property, unless Pixels is the same thing in the WP8 framework. – Wezley Oct 16 '14 at 16:19
  • Did you get your answer? If yes then help me please. Im working with Windows Phone 8 and followed this [link](http://stackoverflow.com/questions/1958470/silverlight-image-to-byte/1964892#1964892) But it didn't work for me. – Saad Anees Dec 26 '14 at 12:31

1 Answers1

3

You're saving your image as JPEG, but I'm fairly certain that OCR library accept RGB/BGRA as an input. So why don't you use Pixels property? It represents image as BGRA array, so the only thing you need is to convert it to byte[] array.

Eldar Dordzhiev
  • 5,105
  • 2
  • 22
  • 26