Solved

More Specific OCR Problem

Posted on 2003-11-18
5
703 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

929 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now