Solved

More Specific OCR Problem

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 - SQL Timeout 9 51
How can I do Unit Testing with Session ? 1 23
pressing download button in c# 7 36
Visual C# Windows Project - Clone Form 4 26
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

730 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