Results 1 to 2 of 2
  1. #1
    DreamWave is offline Member
    Join Date
    May 2014
    Posts
    1
    Rep Power
    0

    Question Codefor OCR scanner not working as an applet

    0 down vote favorite


    I am a newbie in Java but I need to make a scanner (that has a built in ocr) output some content to a browser. I haven't gotten to the browser yet, but the code works when I run it as an application. With the scanner, I received Java code that makes the scanner take a picture, then read from it and output it back to the console. I added a few lines to make it an applet:

    Java Code:
    import gx.*;
    import pr.*;
    import java.applet.*;
    
    public class DocScan extends Applet
    {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        static
        {
            try
            {
                System.loadLibrary("jgx");
                System.loadLibrary("jpr");
            }
            catch (UnsatisfiedLinkError e)
            {
                System.err.println("Native code library failed to load." + e);
                System.exit(1);
            }
        }
        public static void main(String argv[])
        {
            Lib lib = new Lib();
            String text;
            String[] ec ={ "Ok", "Warning", "Error" };
    
            try
            {
                /* Opening the PR system */
                lib.FunctionStart("Opening system files");
                PassportReader pr = new PassportReader();   /* Object for the PR system */
                lib.FunctionEnd();
    
                /* Validity check */
                if (!pr.IsValid())
                {
                    lib.Error("Failed to initialize!");
                    lib.PrintStat();
                }
    
                /* Connecting to the first device */
                lib.FunctionStart("Connecting to the first device");
                pr.UseDevice(0, jpr.PR_UMODE_FULL_CONTROL);
                lib.FunctionEnd();
    
                /* Using the device */
                while (!lib.KbHit())
                {
                    lib.ProcessStart("Processing document");
    
                    try
                    {
                        /* Capturing images */
                        lib.FunctionStart("Capturing images");
                        pr.Capture();
                        lib.FunctionEnd();
    
                        /* Getting document data */
                        lib.FunctionStart("Recognizing.");
                        prDoc doc = pr.Recognize(0);
                        lib.FunctionEnd();
    
                        if (!doc.IsValid()) lib.WriteLine("No data found.");
                        else
                        {
                            /* Displaying document type */
                            lib.WriteLine("Document type: " + doc.Code() + ", status: " + ec[doc.Status() / 100]);
    
                            /* Get some fixed fields and displaying them */
                            text = doc.Field(jpr.PR_DF_NAME);
                            if (!text.equals("")) lib.WriteLine("NAME \"" + text + "\" [" + ec[doc.FieldStatus(jpr.PR_DF_NAME) / 100] + "]");
    
                            text = doc.Field(jpr.PR_DF_DOCUMENT_NUMBER);
                            if (!text.equals("")) lib.WriteLine("DOCUMENT NUMBER \"" + text + "\" [" + ec[doc.FieldStatus(jpr.PR_DF_DOCUMENT_NUMBER) / 100] + "]");
    
                            text = doc.Field(jpr.PR_DF_EXPIRY_DATE);
                            if (!text.equals("")) lib.WriteLine("EXPIRY DATE \"" + text + "\" [" + ec[doc.FieldStatus(jpr.PR_DF_EXPIRY_DATE) / 100] + "]");
    
                            /* Searching for fields and displaying them */
                            gxVariant pdoc = doc.ToVariant();
                            gxVariant fieldlist = new gxVariant();
                            pdoc.GetChild(fieldlist, jgx.GX_VARIANT_BY_ID, jpr.PRV_FIELDLIST, 0);
                            int nitems = fieldlist.GetNItems();
                            for (int i = 0; i < nitems; i++)
                            {
                                gxVariant field = new gxVariant();
                                fieldlist.GetItem(field, jgx.GX_VARIANT_BY_INDEX, 0, i);
                                int field_code = field.GetInt();
                                text = doc.Field(field_code);
                                if (!text.equals("")) lib.WriteLine("[" + field_code + "] \"" + text + "\" [" + ec[doc.FieldStatus(field_code) / 100] + "]");
    
                                if (field_code >= jpr.PR_DF_FORMATTED) continue;
                                try
                                {
                                    gxImage img = doc.FieldImage(field_code);
                                    if (img.IsValid()) img.Save(field_code + ".jpg", jgx.GX_JPEG);
                                }
                                catch (RuntimeException e)
                                {
                                    lib.DisplExcp(e);
                                }
                            }
                        }
                    }
                    catch (RuntimeException e)
                    {
                        lib.DisplExcp(e);
                    }
    
                    lib.ProcessEnd();
                    lib.WaitForSec(3);
                }
    
                /* Closing the device */
                lib.FunctionStart("Closing the device");
                pr.CloseDevice();
                lib.FunctionEnd();
    
            }
            catch (RuntimeException e)
            {
                lib.DisplExcp(e);
            }
    
            lib.PrintStat();
        }
    }

    I am using Eclipse as an IDE. Right now my goal is to simply make the scanner "flash". I know that the output is to the console and I will not see anything from it in an applet, but it should still flash.

    When I run this code as an application, it works. The scanner takes a picture and then it outputs what it has read to the console.

    When I run this code as an Applet, the applet starts and does nothing. It just stays there with no errors of any kind (at least that's what Eclipse is showing me).

    I read that I should allow the applet accesss, so I edited:

    Java Code:
    c:\program files\java\jre8\lib\security\java.policy
    and added this at the end:

    Java Code:
    grant {
            permission java.security.AllPermission;
    };
    Which should allow applets full access. However, there is no change - the applet still launches and does nothing.

    Can anyone point me in the right direction? Why is the code not working when I run it as an applet?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    29

    Default Re: Codefor OCR scanner not working as an applet

    Everything is in that main( ... ) method, while an Applet doesn't even have a main( ... ) method; I can imagine that it doesn't work ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. applet working in applet viewer, not browser
    By plasticfood in forum Java Applets
    Replies: 4
    Last Post: 06-24-2011, 06:47 PM
  2. My Applet Not working
    By mftbrothers in forum Java Applets
    Replies: 3
    Last Post: 03-31-2011, 09:03 AM
  3. Replies: 0
    Last Post: 03-07-2011, 09:02 PM
  4. Replies: 5
    Last Post: 11-27-2010, 03:49 AM
  5. Scanner Not working
    By Xystus777 in forum New To Java
    Replies: 5
    Last Post: 03-18-2009, 12:22 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •