2014-10-16 56 views
-1

我正在使用Microsoft的OCR库,并且在将BitmapImage转换为像素数组时遇到问题。将位图转换为适用于Microsoft OCR的像素阵列#

我为Windows Phone 8制作了这个应用程序,而WriteableBitmap.PixelBuffer.ToArray()不是一个选项,所以我有一个静态函数会将一个普通的BitmapImage改变成一个字节数组,以提供给OCR发动机。嗯,每次我在应用程序崩溃中喂食它。这里有什么问题?

这是我与位图转换器

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

静态类下面是在OCR方法的代码段是造成应用程序崩溃。

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

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

我在做什么错在这里? 谢谢!

+0

['RecognizeAsync'](http://msdn.microsoft.com/zh-cn/library/windowspreview.media.ocr.ocrengine.recognizeasync.aspx)表示它需要“BGRA8格式的图像字节。 “你传递一个JPG格式的字节数组。 – dbc 2014-10-16 16:14:10

+0

'WriteableBitmap'具有['Pixels'](http://msdn.microsoft.com/zh-cn/library/windows/apps/system.windows.media.imaging.writeablebitmap.pixels%28v=vs.105% 29.aspx)属性。那是你要的吗? – dbc 2014-10-16 16:17:00

+0

Dbc,我该如何让它变成BGRA8格式?而对于WriteableBitmap,它需要PixelBuffer属性,除非像素在WP8框架中是相同的。 – user2536897 2014-10-16 16:19:43

回答

3

您将图像保存为JPEG,但我相当确定OCR库接受RGB/BGRA作为输入。 那么你为什么不使用Pixels属性?它将图像表示为BGRA阵列,因此您只需将其转换为byte[]阵列。

+0

我该如何将其转换为byte []数组? – user2536897 2014-10-16 16:28:48

+0

@ user2536897你有没有试过先google? http://stackoverflow.com/a/1964892 – 2014-10-16 16:31:42

+0

谢谢!我得到它的工作! – user2536897 2014-10-16 16:35:43