Interactive Ink

No error message to run, but do not identify callbacks.

  

public class MyScriptUtil implements IRenderTarget, IFontMetricsProvider {
    final long NO_TIMESTAMP = -1;
    final float NO_PRESSURE = 0.0f;
    final int NO_POINTER_ID = -1;
    private Context context;
    private Engine engine;
    private Renderer renderer;
    private Editor editor;
    private ContentPackage contentPackage;
    private ContentPart contentPart;
    private DisplayMetrics displayMetrics;

    private MyScriptUtil() {
    }

    private static class SingleHolder {
        private static final MyScriptUtil INSTANCE = new MyScriptUtil();
    }

    public static MyScriptUtil getInstance() {
        return SingleHolder.INSTANCE;
    }

    public Engine getEngine() {
        return engine;
    }

    public void init(Context context) {
        this.context = context;
        engine = Engine.create(MyCertificate.getBytes());
        displayMetrics = context.getResources().getDisplayMetrics();
        configRecognition();
        configEngine();
        setViewSize();
        configContent(); //配置识别内容区域
        initListener(); //识别监听
    }

    /**
     * 配置识别内容
     */
    public void configRecognition() {
        //获取Engine中资源配置类
        Configuration conf = engine.getConfiguration();
        //设置识别所用资源
        conf.setStringArray("configuration-manager.search-path",
                new String[]{"zip://" + context.getPackageCodePath() + "!/assets/conf"});
        // set the temporary directory
        String tempDir = context.getFilesDir().getPath() + File.separator + "tmp";
        conf.setString("content-package.temp-folder", tempDir);
        // 设置数字识别浮点精度为两位
//        conf.setNumber("math.solver.fractional-part-digits", 2);
        // 设置识别文本类型
//        conf.setString("lang", "math");
//        conf.setString("text.configuration.bundle", "en_US");
    }

    /**
     * 配置Engine
     */
    public void configEngine() {
//        Configuration conf = engine.getConfiguration();
//        float verticalMarginPX = context.getResources().getDimension(R.dimen.vertical_margin);
//        float horizontalMarginPX = context.getResources().getDimension(R.dimen.horizontal_margin);
//        float verticalMarginMM = 25.4f * verticalMarginPX / displayMetrics.ydpi;
//        float horizontalMarginMM = 25.4f * horizontalMarginPX / displayMetrics.xdpi;
//        conf.setNumber("text.margin.top", verticalMarginMM);
//        conf.setNumber("text.margin.left", horizontalMarginMM);
//        conf.setNumber("text.margin.right", horizontalMarginMM);
//        conf.setNumber("math.margin.top", verticalMarginMM);
//        conf.setNumber("math.margin.bottom", verticalMarginMM);
//        conf.setNumber("math.margin.left", horizontalMarginMM);
//        conf.setNumber("math.margin.right", horizontalMarginMM);
        renderer = engine.createRenderer(displayMetrics.xdpi, displayMetrics.ydpi, this);
        editor = engine.createEditor(renderer);
        editor.setFontMetricsProvider(this);
    }

    public void setViewSize() {
        editor.setViewSize(displayMetrics.widthPixels, displayMetrics.heightPixels);
    }

    public void configContent() {
        String packageName = "File1.iink";
        File file = new File(context.getFilesDir(), packageName);
        try {
            contentPackage = engine.createPackage(file);
            contentPart = contentPackage.createPart("Text Document"); // Choose type of content (possible values are: "Text Document", "Text", "Diagram", "Math", and "Drawing")
            new Handler().post(new Runnable() {
                @Override
                public void run() {
                    editor.setPart(contentPart);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void initListener() {
        editor.addListener(new IEditorListener() {
            @Override
            public void partChanging(Editor editor, ContentPart contentPart, ContentPart contentPart1) {
                Log.e("test_myscript", "partChanging");
            }

            @Override
            public void partChanged(Editor editor) {
                Log.e("test_myscript", "partChanged");
            }

            @Override
            public void contentChanged(Editor editor, String[] blockIds) {
                Log.e("test_myscript", "contentChanged"+ Arrays.toString(blockIds));
            }

            @Override
            public void onError(Editor editor, String blockId, String message) {
                Log.e("test_myscript", "onError:" + message);
            }
        });
    }

    /**
     * 转换
     */
    public void convertResult() {
        Log.e("test_myscript", "convertResult:开始识别");
//        editor.convert(editor.getRootBlock(), ConversionState.DIGITAL_PUBLISH);
        editor.convert(null, editor.getSupportedTargetConversionStates(null)[0]);
    }

    /**
     * 实时转换点
     */
    public void convertPoint(NotePoint notePoint) {
        switch (notePoint.getPointType()) {
            case PointType.TYPE_DOWN:
                editor.pointerDown(notePoint.getPX(), notePoint.getPY(), NO_TIMESTAMP, NO_PRESSURE, PointerType.PEN, NO_POINTER_ID);
                break;
            case PointType.TYPE_MOVE:
                editor.pointerMove(notePoint.getPX(), notePoint.getPY(), NO_TIMESTAMP, NO_PRESSURE, PointerType.PEN, NO_POINTER_ID);
                break;
            case PointType.TYPE_UP:
                editor.pointerUp(notePoint.getPX(), notePoint.getPY(), NO_TIMESTAMP, NO_PRESSURE, PointerType.PEN, NO_POINTER_ID);
                break;
        }
    }

    /**
     * 实时转换线
     */
    public void convertStroke(NoteStroke noteStroke) {
        PointerEvent[] pointerEvents = new PointerEvent[noteStroke.getNotePointList().size()];
        for (int i = 0; i < noteStroke.getNotePointList().size(); i++) {
            NotePoint notePoint = noteStroke.getNotePointList().get(i);
            switch (notePoint.getPointType()) {
                case PointType.TYPE_DOWN:
                    pointerEvents[i] = new PointerEvent(PointerEventType.DOWN, notePoint.getPX(),
                            notePoint.getPY(), NO_TIMESTAMP, NO_PRESSURE, PointerType.PEN, NO_POINTER_ID);
                    break;
                case PointType.TYPE_MOVE:
                    pointerEvents[i] = new PointerEvent(PointerEventType.MOVE, notePoint.getPX(),
                            notePoint.getPY(), NO_TIMESTAMP, NO_PRESSURE, PointerType.PEN, NO_POINTER_ID);
                    break;
                case PointType.TYPE_UP:
                    pointerEvents[i] = new PointerEvent(PointerEventType.UP, notePoint.getPX(),
                            notePoint.getPY(), NO_TIMESTAMP, NO_PRESSURE, PointerType.PEN, NO_POINTER_ID);
                    break;
            }
        }
        editor.pointerEvents(pointerEvents, true);
        Log.e("test_myscript", "convertStroke:" + Arrays.toString(pointerEvents));
    }

    @Override
    public void invalidate(Renderer renderer, EnumSet<LayerType> enumSet) {

    }

    @Override
    public void invalidate(Renderer renderer, int i, int i1, int i2, int i3, EnumSet<LayerType> enumSet) {

    }

    @Override
    public Rectangle[] getCharacterBoundingBoxes(Text text, TextSpan[] textSpans) {
        Log.e("test_myscript", "getCharacterBoundingBoxes:" + text);
        return new Rectangle[0];
    }

    @Override
    public float getFontSizePx(Style style) {
        Log.e("test_myscript", "getFontSizePx:" + style.getFontStyle());
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, style.getFontSize(), displayMetrics);
    }
}

 

 

1 Comment

Dear Lei Holmes,

Based on the code, everything seems correct... you should debug step by step and see if you happen to have a NULL element (e.g. editor, renderer, engine...)

With the current information, we cannot  help your further...

Best regards,

Olivier
Login or Signup to post a comment