简体   繁体   中英

Microsoft OCR: Convert bitmap to pixel array for Windows Phone 8 Silverlight

I have been searching for answer for 2 days but i couldn't. That's why i'm posting it here. I followed this tutorial .

I got error bitmap.SetSource(imgStream); so i changed it to bitmap.SetSource(imgStream.AsStream);

I also got error message on this line. I'm unable to convert pixel to array. Because there is no PixelBuffer and i cannot use Pixels

var ocrResult = await ocrEngine.RecognizeAsync((uint)bitmap.PixelHeight, (uint)bitmap.PixelWidth, bitmap.PixelBuffer.ToArray());

So i searched on internet and found this link of stackoverflow.com. So I copied and pasted the following code

public static byte[] ToByteArray(this WriteableBitmap bmp)
{
   // Init buffer
   int w = bmp.PixelWidth;
   int h = bmp.PixelHeight;
   int[] p = bmp.Pixels;
   int len = p.Length;
   byte[] result = new byte[4 * w * h];

   // Copy pixels to buffer
   for (int i = 0, j = 0; i < len; i++, j += 4)
  {
      int color = p[i];
      result[j + 0] = (byte)(color >> 24); // A
      result[j + 1] = (byte)(color >> 16); // R
      result[j + 2] = (byte)(color >> 8);  // G
      result[j + 3] = (byte)(color);       // B
   }

    return result;
}

And then byte[] hello = ByteArrayChange.ToByteArray(bitmap);

var ocrResult = await ocrEngine.RecognizeAsync((uint)bitmap.PixelHeight, (uint)bitmap.PixelWidth, hello );

I run the code with Device and it gave the exception in App.Xaml.cs in Application_UnhandledException

Note: I'm developing on Windows Phone 8/8.1 (Silverlight)

MSDN上的OCR文档包含有关Silverlight应用程序的部分。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM