Community

image as byte array custom image source Fine Reader 11

Is there any way to add images as byte[] to the FREngine.IEngine for OCR processing rather than giving the path of image file to the "AddImageFile" method of FREngine.FRDocument object?.

this is for Windows web service, .NET

0

Comments

7 comments

  • Avatar
    Oksana Serdyuk

    Please consider using the IFRDocument::AddImageFileFromStream method. This method opens an image file from the input stream implemented by the user, and adds the pages corresponding to the opened file to the document. For more information about this method please refer to the Help file: API Reference→Document-Related Objects→Document Organization Objects→FRDocument Object, section "Methods" and API Reference→Supplementary Objects→IReadStream.

    -1
    Comment actions Permalink
  • Avatar
    SGumaste

    Hi Oksnana, I did try using IReadStream but no luck, program goes into infinite loop. Definitely I am doing something wrong, I am pasting my code here can you please help me : am breaking into two comments because of chars restrictions public void foo() {FREngine.IEngine engine = _enginesPool.GetEngine(); FREngine.FRDocument frDocument = engine.CreateFRDocument(); byte[] myBytes = File.ReadAllBytes(argImagesPath); CustomReadStream crs = new CustomReadStream(myBytes); frDocument.AddImageFileFromStream(crs, null, null, null, "0");}

    0
    Comment actions Permalink
  • Avatar
    SGumaste

    class CustomReadStream : IReadStream { public CustomReadStream(byte[] argImageBytes) {currentPos = 0; imageBytes = new byte[argImageBytes.Length]; argImageBytes.CopyTo(imageBytes, 0);} public int Read(out Array data, int count) { data = imageBytes; buf = new byte[imageBytes.Length]; imageBytes.CopyTo(buf, 0); return imageBytes.Length; }

    0
    Comment actions Permalink
  • Avatar
    SGumaste

    public void Close() { imageBytes = null; buf = null; } uint currentPos = 0; byte[] imageBytes = null; byte[] buf = null; }

    0
    Comment actions Permalink
  • Avatar
    Oksana Serdyuk

    Hi,

    Please sorry for the delay!

    First of all, please check what FineReader Engine release you use. The case is that the implementation of the IReadStream interface was changed in our Help file to more optimal beginning from the 5th release of the program, the build number - 11.1.10.100. And now the current version of FineReader Engine 11 is the 8th release, the build number - 11.1.19.48. Please contact your Sales Manager to update your FineReader Engine package.

    Below please find our sample C# code snippet illustrating how to implement the IReadStream interface and use the IFRDocument::AddImageFileFromStream() method:

    // ...
    using System.IO; 
    // ...
    
    class CustomReadStream : FREngine.IReadStream
    {
        private MemoryStream fileBytes = null;
    
        public CustomReadStream(byte[] _fileBytes)
        {
            fileBytes = new MemoryStream(_fileBytes);
        }
    
        public void Close()
        {
            fileBytes.Close();
        }
    
        public int Read(out byte[] data, int count)
        {
            data = new byte[count];
            int readBytes = fileBytes.Read(data, 0, count);
            return readBytes;
        }
    }
    
    // ...
    
    // Create document
    FREngine.FRDocument document = engineLoader.Engine.CreateFRDocument();
    
    // Add image file to document  
    byte[] myBytes = File.ReadAllBytes(imagePath);
    CustomReadStream readStream = new CustomReadStream(myBytes);
    document.AddImageFileFromStream(readStream, null, null, null, "0");
    

    Then you can continue processing using the FRDocument object in the way you prefer.

    1
    Comment actions Permalink
  • Avatar
    Rishabh Jain1

    can you please suggest , how to implement this in JAVA 

    I have tried but failed to implement , following is my code

     

    public class AbbyyInputStream implements IReadStream {

     InputStream stream;

    byte[] bytes;

     public AbbyyInputStream(MultipartFile imageFile) throws IOException {

    super();

    this.bytes = imageFile.getBytes();

    this.stream = imageFile.getInputStream();

    }

    @Override

    public void Close() {

    try {

    stream.close();

    } catch (IOException e) {

    e.printStackTrace();

    }

    }

     @Override

    public int Read(Ref<byte[]> data, int arg1) {

     data = new Ref<byte[]>(new byte[bytes.length]);

    try {

    return stream.read(data.get(), 0, arg1);

    } catch (IOException e) {

    e.printStackTrace();

    }

    return 0;

    }

    }

     

    0
    Comment actions Permalink
  • Avatar
    Nikolay Krivchanskiy

    Hi,

     

    As you request, we provide you functional example of the same code in java:

     

    public class ReadStream implements IReadStream {

     

            private ByteInputStream fileBytes;

     

            ReadStream( byte[] fileBytes ) {

                this.fileBytes = new ByteInputStream( fileBytes, fileBytes.length );

            }

     

            @Override

            public int Read( Ref<byte[]> ref, int count ) {

                ref.set( new byte[count] );

                int result = fileBytes.read( ref.get(), 0, count );

     

                return ( result == -1 ? 0 : result );

            }

     

            @Override

            public void Close() {

                try {

                    fileBytes.close();

                } catch ( IOException e ) {

                    e.printStackTrace();

                }

            }

        }

    1
    Comment actions Permalink

Please sign in to leave a comment.