PageRenderTime 18ms CodeModel.GetById 9ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/ocr/ocrservice/src/com/googlecode/eyesfree/opticflow/OcrProcessor.java

http://eyes-free.googlecode.com/
Java | 91 lines | 46 code | 14 blank | 31 comment | 2 complexity | c3197fe170a33f0b63f98dc1fef6ccfb MD5 | raw file
 1/*
 2 * Copyright (C) 2011 Google Inc.
 3 *
 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 5 * use this file except in compliance with the License. You may obtain a copy of
 6 * the License at
 7 *
 8 * http://www.apache.org/licenses/LICENSE-2.0
 9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.googlecode.eyesfree.opticflow;
18
19import com.googlecode.eyesfree.env.Size;
20import com.googlecode.eyesfree.opticflow.TextTrackerProcessor.TrackedRect;
21
22import java.util.LinkedList;
23import java.util.Vector;
24
25/**
26 * Frame processor that queues OCR jobs.
27 *
28 * @author alanv@google.com (Alan Viverette)
29 */
30public class OcrProcessor extends FrameProcessor {
31    /** Text area tracker. */
32    private final TextTrackerProcessor mTracker;
33
34    /** Queue containing text areas to be OCR'ed. */
35    private final OcrQueue mOcrQueue;
36
37    /** Callback for OCR completion events. */
38    private Listener mListener;
39
40    /**
41     * Constructs a new OCR processor.
42     *
43     * @param tessdata The path containing the {@code tessdata} directory.
44     * @param language The language pack to use. Defaults to "eng" if not
45     *            available.
46     * @param tracker A text area tracker.
47     */
48    public OcrProcessor(String tessdata, String language, TextTrackerProcessor tracker) {
49        mTracker = tracker;
50        mOcrQueue = new OcrQueue(tessdata, language);
51        mOcrQueue.setListener(queueListener);
52    }
53
54    public void setListener(Listener listener) {
55        mListener = listener;
56    }
57
58    @Override
59    protected void onInit(Size size) {
60        mOcrQueue.init();
61    }
62
63    @Override
64    protected void onProcessFrame(TimestampedFrame frame) {
65        LinkedList<TrackedRect> add = mTracker.getOcrAdd();
66        LinkedList<TrackedRect> remove = mTracker.getOcrRemove();
67
68        mOcrQueue.addAll(add);
69        mOcrQueue.removeAll(remove);
70    }
71
72    @Override
73    protected Vector<String> getDebugText() {
74        Vector<String> debugText = new Vector<String>();
75        debugText.add("Queued: " + mOcrQueue.size());
76        return debugText;
77    }
78
79    public interface Listener {
80        public void onResult(String result, int[] confs);
81    }
82
83    private final OcrQueue.Listener queueListener = new OcrQueue.Listener() {
84        @Override
85        public void onResult(String result, int[] confs) {
86            if (mListener != null) {
87                mListener.onResult(result, confs);
88            }
89        }
90    };
91}