从字节数组加载图像

时间:2018-07-09 13:06:55

标签: c# tesseract

我正在使用tesseract从图像中读取字符串。我有一个包含图像,宽度/高度和矩阵点的类(在字节数组中,图像为灰度)。

我有一个主图像,然后将其裁剪为小图像。实际上,我将所有裁剪后的图像保存在磁盘中,但是它占据了很多地方。

是否可以通过字节数组处理tesseract引擎?

有我的代码:

class MyImage
{
    public String Name;
    public int Width;
    public int Height;
    public Byte[] Matrix;

    public MyImage Crop(int x, int y, int w, int h);
    public void PrintToFile(String path);
}

String ReadImage(MyImage img, int x, int y, int w, int h)
{
    MyImage cropImg = img.Crop(x,y,w,h);
    String path = cropImg.Name;
    cropImg.PrintToFile(path);

    TesseractEngine engine = new TesseractEngine(".", "eng", EngineMode.TesseractAndCube);

    String ExtractedText;
    using (Page page = engine.Process(new System.Drawing.Bitmap(path)))
    {
        ExtractedText = page.GetText();
    }

    return ExtractedText;
}

1 个答案:

答案 0 :(得分:1)

尝试使用它。

public Image byteArrayToImage(byte[] byteArrayIn)
{
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);
     return returnImage;
}

Image to Byte Array and Byte Array to Image Converter Class