Tesseract-android-tools  1.0
 Todo Clases Namespaces Archivos Funciones Variables
TessBaseAPI.java
Ir a la documentación de este archivo.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
00015  */
00016 
00046 package com.googlecode.tesseract.android;
00047 
00048 import com.googlecode.leptonica.android.Pix;
00049 import com.googlecode.leptonica.android.Pixa;
00050 import com.googlecode.leptonica.android.ReadFile;
00051 
00052 import android.graphics.Bitmap;
00053 import android.graphics.Rect;
00054 
00055 import java.io.File;
00056 
00063 public class TessBaseAPI {
00067         private int mNativeData;
00068 
00069         static {
00070                 System.loadLibrary("lept");
00071                 System.loadLibrary("tess");
00072 
00073                 nativeClassInit();
00074         }
00075 
00080         public static final class PageSegMode {
00082                 public static final int PSM_OSD_ONLY = 0;
00083 
00088                 public static final int PSM_AUTO_OSD = 1;
00089 
00091                 public static final int PSM_AUTO_ONLY = 2;
00092 
00094                 public static final int PSM_AUTO = 3;
00095 
00097                 public static final int PSM_SINGLE_COLUMN = 4;
00098 
00100                 public static final int PSM_SINGLE_BLOCK_VERT_TEXT = 5;
00101 
00103                 public static final int PSM_SINGLE_BLOCK = 6;
00104 
00106                 public static final int PSM_SINGLE_LINE = 7;
00107 
00109                 public static final int PSM_SINGLE_WORD = 8;
00110 
00112                 public static final int PSM_CIRCLE_WORD = 9;
00113 
00115                 public static final int PSM_SINGLE_CHAR = 10;
00116 
00118                 public static final int PSM_COUNT = 11;
00119         }
00120 
00130         public static final class PageIteratorLevel {
00132                 public static final int RIL_BLOCK = 0;
00133 
00135                 public static final int RIL_PARA = 1;
00136 
00138                 public static final int RIL_TEXTLINE = 2;
00139 
00141                 public static final int RIL_WORD = 3;
00142 
00144                 public static final int RIL_SYMBOL = 4;
00145         };
00146 
00148         public static final int AVS_FASTEST = 0;
00149 
00151         public static final int AVS_MOST_ACCURATE = 100;
00152 
00154         public static final String VAR_CHAR_WHITELIST = "tessedit_char_whitelist";
00155 
00157         public static final String VAR_CHAR_BLACKLIST = "tessedit_char_blacklist";
00158 
00160         public static final String VAR_ACCURACYVSPEED = "tessedit_accuracyvspeed";
00161 
00165         public TessBaseAPI() {
00166                 nativeConstruct();
00167         }
00168 
00173         @Override
00174         protected void finalize() throws Throwable {
00175                 try {
00176                         nativeFinalize();
00177                 } finally {
00178                         super.finalize();
00179                 }
00180         }
00181 
00216         public boolean init(String datapath, String language) {
00217                 if (datapath == null) {
00218                         throw new IllegalArgumentException("Data path must not be null!");
00219                 }
00220                 if (!datapath.endsWith(File.separator)) {
00221                         datapath += File.separator;
00222                 }
00223 
00224                 File tessdata = new File(datapath + "tessdata");
00225                 if (!tessdata.exists() || !tessdata.isDirectory()) {
00226                         throw new IllegalArgumentException(
00227                                         "Data path must contain subfolder tessdata!");
00228                 }
00229 
00230                 return nativeInit(datapath, language);
00231         }
00232 
00239         public void clear() {
00240                 nativeClear();
00241         }
00242 
00251         public void end() {
00252                 nativeEnd();
00253         }
00254 
00272         public boolean setVariable(String var, String value) {
00273                 return nativeSetVariable(var, value);
00274         }
00275 
00283         public void setPageSegMode(int mode) {
00284                 nativeSetPageSegMode(mode);
00285         }
00286 
00294         public void setDebug(boolean enabled) {
00295                 nativeSetDebug(enabled);
00296         }
00297 
00306         public void setRectangle(Rect rect) {
00307                 setRectangle(rect.left, rect.top, rect.width(), rect.height());
00308         }
00309 
00324         public void setRectangle(int left, int top, int width, int height) {
00325                 nativeSetRectangle(left, top, width, height);
00326         }
00327 
00334         public void setImage(File file) {
00335                 Pix image = ReadFile.readFile(file);
00336 
00337                 if (image == null) {
00338                         throw new RuntimeException("Failed to read image file");
00339                 }
00340 
00341                 nativeSetImagePix(image.getNativePix());
00342         }
00343 
00352         public void setImage(Bitmap bmp) {
00353                 Pix image = ReadFile.readBitmap(bmp);
00354 
00355                 if (image == null) {
00356                         throw new RuntimeException("Failed to read bitmap");
00357                 }
00358 
00359                 nativeSetImagePix(image.getNativePix());
00360         }
00361 
00371         public void setImage(Pix image) {
00372                 nativeSetImagePix(image.getNativePix());
00373         }
00374 
00394         public void setImage(byte[] imagedata, int width, int height, int bpp,
00395                         int bpl) {
00396                 nativeSetImageBytes(imagedata, width, height, bpp, bpl);
00397         }
00398 
00405         public String getUTF8Text() {
00406                 // Trim because the text will have extra line breaks at the end
00407                 String text = nativeGetUTF8Text();
00408 
00409                 return text.trim();
00410         }
00411 
00412         public Pixa getRegions() {
00413                 int pixa = nativeGetRegions();
00414 
00415                 if (pixa == 0) {
00416                         return null;
00417                 }
00418 
00419                 return new Pixa(pixa, 0, 0);
00420         }
00421 
00422         public Pixa getWords() {
00423                 int pixa = nativeGetWords();
00424 
00425                 if (pixa == 0) {
00426                         return null;
00427                 }
00428 
00429                 return new Pixa(pixa, 0, 0);
00430         }
00431 
00437         public int meanConfidence() {
00438                 return nativeMeanConfidence();
00439         }
00440 
00449         public int[] wordConfidences() {
00450                 int[] conf = nativeWordConfidences();
00451 
00452                 // We shouldn't return null confidences
00453                 if (conf == null) {
00454                         conf = new int[0];
00455                 }
00456 
00457                 return conf;
00458         }
00459 
00460         public ResultIterator getResultIterator() {
00461                 int nativeResultIterator = nativeGetResultIterator();
00462 
00463                 if (nativeResultIterator == 0) {
00464                         return null;
00465                 }
00466 
00467                 return new ResultIterator(nativeResultIterator);
00468         }
00469 
00470         // ******************
00471         // * Native methods *
00472         // ******************
00473 
00477         private static native void nativeClassInit();
00478 
00482         private native void nativeConstruct();
00483 
00487         private native void nativeFinalize();
00488 
00489         private native boolean nativeInit(String datapath, String language);
00490 
00491         private native void nativeClear();
00492 
00493         private native void nativeEnd();
00494 
00495         private native void nativeSetImageBytes(byte[] imagedata, int width,
00496                         int height, int bpp, int bpl);
00497 
00498         private native void nativeSetImagePix(int nativePix);
00499 
00500         private native void nativeSetRectangle(int left, int top, int width,
00501                         int height);
00502 
00503         private native String nativeGetUTF8Text();
00504 
00505         private native int nativeGetRegions();
00506 
00507         private native int nativeGetWords();
00508 
00509         private native int nativeMeanConfidence();
00510 
00511         private native int[] nativeWordConfidences();
00512 
00513         private native boolean nativeSetVariable(String var, String value);
00514 
00515         private native void nativeSetDebug(boolean debug);
00516 
00517         private native void nativeSetPageSegMode(int mode);
00518 
00519         private native int nativeGetResultIterator();
00520 
00521 }
 Todo Clases Namespaces Archivos Funciones Variables