代码之家  ›  专栏  ›  技术社区  ›  A.Pissicat

从字节数组加载图像

  •  -1
  • A.Pissicat  · 技术社区  · 7 年前

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

    我有一个主图像,我把它裁剪成小图像。事实上,我正在我的磁盘上保存所有的croped图像,但它占据了太多的位置。

    是否有方法从字节数组处理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 回复  |  直到 7 年前
        1
  •  1
  •   Josue Barrios    7 年前

    试着用这个。

    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