• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 720
  • Last Modified:

More Specific OCR Problem

Ok. Let me try this, it may be easier....

I've got this C++ Project (still) that is a commercial OCR Engine. I am trying to re-write it with the built in API into C#. It heavily uses pointers and other things I'm a bit unfamiliar with. I'm currently stuck on this... (This is the C++ Code - Original)

                  TOCRResults = (TOCRRESULTS *)malloc(ResultsInf);
                  cout << "TOCRResults " << TOCRResults << endl;
                  if ( TOCRResults ) {

                        // Retrieve the results

                        status = TOCRGetJobResults(JobNo, &ResultsInf, TOCRResults);
                        if ( status != TOCR_OK ) goto ABORT;
                        cout << "Num Items: " << TOCRResults->Hdr.NumItems << endl;

                        // Display the results

                        printf("\t\t");
                        for (ItemNo = 0; ItemNo < TOCRResults->Hdr.NumItems; ItemNo ++ ) {
                              if ( TOCRResults->Item[ItemNo].OCRCha == '\r' )
                                    printf("\n\t\t");
                              else
                                    printf("%c", TOCRResults->Item[ItemNo].OCRCha);
                        }
                        printf("\n");
                        free (TOCRResults);

I hope that doesn't come out messy..anyway..
my Current attempt at C# Reindition is as follows...
                  TOCRResults = new TOCRResults();
                        status = API.TOCRGetJobResults(JobNo, ref ResultsInf,TOCRResults);
                        if ( ResultsInf > 0 )
                        {

                              TOCRResults = new TOCRResults();
                              TOCRResults.IsTrue = true;            

                              Console.WriteLine("Results Inf: " + ResultsInf.ToString());
                              Console.WriteLine("Results SizeOf: " + Marshal.SizeOf(TOCRResults));
                              if ( TOCRResults.IsTrue )
                              {
                                    // Retrieve the results
                                    status = API.TOCRGetJobResults(JobNo,ref ResultsInf, TOCRResults);
                                    // Display the results
                                    TOCRResultsItem[] Item = new TOCRResultsItem[ResultsInf];
      
                                    TOCRResults.Hdr.NumItems = Item.Length;
                                    for(int temp = 0;temp <=Item.Length-1;temp++)
                                          TOCRResults.Item[temp] = Item[temp];
                              TOCRResults.Item = Item;
                                    Console.WriteLine(Item.ToString());

                                    Console.WriteLine("\t\tTest");
                                    for (ItemNo = 0; ItemNo < TOCRResults.Hdr.NumItems; ItemNo ++ )
                                    {
                                          if ( TOCRResults.Item[ItemNo].OCRCha == '\r' )
                                                Console.WriteLine("\n\t\t");
                                          else
                                          {
                                                Console.WriteLine(TOCRResults.Item[ItemNo].OCRCha);
                                                Console.WriteLine(Item[ItemNo].OCRCha.ToString());
                                          }
                                    }
                                    Console.WriteLine("\nLast Line");
                              }
                              else
                              {
                                    Console.WriteLine("Failed to allocate memory for results\n");
                              }
                        }
                        else
                        {
                              Console.WriteLine("No results found\n");
                        }


------------------

The Structs for the "TOCRResults" object or class are as follows as well..
C++
---------------------------
typedef struct tagTOCRRESULTSITEM
{
      unsigned short      StructId;
      unsigned short      OCRCha;
      float                  Confidence;
      unsigned short      XPos;
      unsigned short      YPos;
      unsigned short      XDim;
      unsigned short      YDim;
} TOCRRESULTSITEM;


typedef struct tagTOCRRESULTS
{
    TOCRRESULTSHEADER      Hdr;
    TOCRRESULTSITEM            Item[1];
} TOCRRESULTS;
-----------------------------

C# Recreation
----------------------------------------
[StructLayout(LayoutKind.Sequential)]
public struct TOCRResultsItem
{
      public int            StructId;
      public ushort      OCRCha;
      public float      Confidence;
      public ushort      XPos;
      public ushort      YPos;
      public ushort      XDim;
      public ushort      YDim;
}
[StructLayout(LayoutKind.Sequential)]
public struct TOCRResults
{
      public TOCRResultsHeader      Hdr;
      //[MarshalAs(UnmanagedType.ByValArray)]
      public TOCRResultsItem[]      Item;
      public bool IsTrue;   //attempted member for boolean operation in Main.cs file ..didn't really help anything.
      public int Null;  // this was an attempt to pass a '0' through the GetJobResults instead of the object TOCRResults, which didn't seem to bode well with C#, but worked fine in C++, it too was useless...
-------------------------------

The function/method call is as follows..

      [DllImportAttribute("TOCRdll")]
      public static extern int TOCRGetJobResults(int JobNo, ref int ResultsInf, TOCRResults Results);
------------------------

I know this is rather vague, but it's a several file Project, and I can't distribute the source code for the engine. My problem is that It simply won't return anything, the ResultsInf variable 'DOES' Work properly (Returning the number of characters in the image file I try to process) but after that, nothing seems to work right. Can anyone help me? I can provide more info if I need to.
      
}
0
derekthornton
Asked:
derekthornton
  • 3
  • 2
1 Solution
 
stone123Commented:
the ResultsInf variable works but the Results doesn't. What is the difference between the two variables?

the first one is an integer, a primative type. the other is a user-defined struct type. When your function returns a primative type, it simply copy the value. the memory format and size of int type should be the same in both native C/C++ and C#. (just my wild guess).

However, when it comes to the user-defined type, things may be different, especially when the struct is a combination of multiple types of variables. the momery format and size, in other words, the bit pattern will be different in C++ and C#.

This is just my 2 cents here. what I am saying may be wrong. I only want to bring up some thoughts.

Ok, program time.... try this:

First, declare your dll function as:
     [DllImport("TOCRdll.dll")]
     public static extern int TOCRGetJobResults(int JobNo, ref int ResultsInf, ref TOCRResults Results);

Then, call it:
     status = API.TOCRGetJobResults(JobNo, ref ResultsInf, ref TOCRResults);

Notes: there shouldn't be any necessary to have key word "ref" before the variable ResultsInf, but I just leave it.
0
 
derekthorntonAuthor Commented:
I've tried that already,actually. It doesn't change much. It's really a pickle. It may make more sense if you saw the whole thing ..but it requires the .dll library, which I can't get the code to to paste. Can I email it to you?
0
 
derekthorntonAuthor Commented:
Here. I've put the .zip file for it on this small site of mine for now.

http://www.members.cox.net/derek.thornton/OCRProject.zip

and you'll need the TOCR viewer if you decide to DL this and look at it.

http://www.transym.com/tocr13.zip

I appreciate the help you've already given, as well as the others who have contributed. I'm determined to make this project work one way or another.
0
 
derekthorntonAuthor Commented:
0
 
stone123Commented:
sorry, I haven't got any idea yet. Hope others could help you out.

good luck
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now