繁体   English   中英

Microsoft OCR:将位图转换为Windows Phone 8 Silverlight的像素阵列

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

我一直在寻找答案2天,但不能。 这就是为什么我在这里发布它。 我遵循了本教程

我收到错误bitmap.SetSource(imgStream); 所以我将其更改为bitmap.SetSource(imgStream.AsStream);

我在这条线上也收到错误消息。 我无法将像素转换为数组。 因为没有PixelBuffer ,所以我无法使用Pixels

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

所以我在互联网上搜索,发现了这个stackoverflow.com的链接 所以我复制并粘贴了以下代码

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;
}

然后byte[] hello = ByteArrayChange.ToByteArray(bitmap);

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

我使用Device运行代码,并在Application_UnhandledException App.Xaml.cs中给出了异常

注意:我正在Windows Phone 8 / 8.1(Silverlight)上进行开发

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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