我一直在寻找答案 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 );
我使用设备运行代码,它在App.Xaml.cs
中给出了异常Application_UnhandledException
注意:我正在Windows Phone 8/8.1(Silverlight(上进行开发
MSDN 上的 OCR 文档包含有关 Silverlight 应用程序的部分。