Search code examples
c#tesseract

Load image from byte array


I'm using tesseract to read string from images. I have a class containing images, width/height and matrix points (in a byte array, images are in grayscale).

I have a main image and I crop it into little images. Actually i'm saving all croped image in my disk but it take so many place.

Is there a way to process tesseract engine from a byte array?

There is my code:

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

Solution

  • try to use this.

    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