Java Applet使用本地扫描程序不工作,没有错误

时间:2014-05-18 11:16:09

标签: java eclipse applet native-code

我是Java的新手,但我需要制作一个扫描仪(内置的ocr)向浏览器输出一些内容。我还没有访问浏览器,但是当我将其作为应用程序运行时,代码可以运行。 使用扫描仪,我收到了Java代码,使扫描仪拍照,然后从中读取并将其输出回控制台。我添加了几行来使它成为applet:

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();
    }
}

我使用Eclipse作为IDE。现在我的目标是简单地让扫描仪“闪烁”。我知道输出是在控制台上,我不会在applet中看到任何内容,但它应该仍然闪烁。

当我将此代码作为应用程序运行时,它可以工作。扫描仪拍摄照片然后将其读取的内容输出到控制台。

当我将此代码作为Applet运行时,applet启动并且什么都不做。它只是停留在那里没有任何错误(至少Eclipse正在向我展示)。

我读到我应该允许applet访问,所以我编辑了:

c:\program files\java\jre8\lib\security\java.policy

并在最后添加:

grant {
        permission java.security.AllPermission;
};

哪个应该允许applet完全访问。但是,没有任何变化 - 小程序仍然启动并且什么都不做。

有人能指出我正确的方向吗?当我将它作为applet运行时,为什么代码不起作用?

0 个答案:

没有答案