Solved

More Specific OCR Problem

Posted on 2003-11-18
5
709 Views
Last Modified: 2007-12-19
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
Comment
Question by:derekthornton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
stone123 earned 500 total points
ID: 9774756
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
 
LVL 2

Author Comment

by:derekthornton
ID: 9774803
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
 
LVL 2

Author Comment

by:derekthornton
ID: 9774889
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
 
LVL 2

Author Comment

by:derekthornton
ID: 9774895
0
 
LVL 1

Expert Comment

by:stone123
ID: 9780858
sorry, I haven't got any idea yet. Hope others could help you out.

good luck
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question