Tesseract-android-tools  1.0
 Todo Clases Namespaces Archivos Funciones Variables
ReadFile.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 
00025 package com.googlecode.leptonica.android;
00026 
00027 import android.graphics.Bitmap;
00028 import android.graphics.BitmapFactory;
00029 
00030 import java.io.File;
00031 
00037 public class ReadFile {
00038         static {
00039                 System.loadLibrary("lept");
00040         }
00041 
00050         public static Pix readMem(byte[] encodedData) {
00051                 if (encodedData == null)
00052                         throw new IllegalArgumentException(
00053                                         "Image data byte array must be non-null");
00054 
00055                 final BitmapFactory.Options opts = new BitmapFactory.Options();
00056                 opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
00057 
00058                 final Bitmap bmp = BitmapFactory.decodeByteArray(encodedData, 0,
00059                                 encodedData.length, opts);
00060                 final Pix pix = readBitmap(bmp);
00061 
00062                 bmp.recycle();
00063 
00064                 return pix;
00065         }
00066 
00078         public static Pix readBytes8(byte[] pixelData, int width, int height) {
00079                 if (pixelData == null)
00080                         throw new IllegalArgumentException("Byte array must be non-null");
00081                 if (width <= 0)
00082                         throw new IllegalArgumentException(
00083                                         "Image width must be greater than 0");
00084                 if (height <= 0)
00085                         throw new IllegalArgumentException(
00086                                         "Image height must be greater than 0");
00087                 if (pixelData.length < width * height)
00088                         throw new IllegalArgumentException(
00089                                         "Array length does not match dimensions");
00090 
00091                 int nativePix = nativeReadBytes8(pixelData, width, height);
00092 
00093                 if (nativePix == 0)
00094                         throw new RuntimeException("Failed to read pix from memory");
00095 
00096                 return new Pix(nativePix);
00097         }
00098 
00113         public static boolean replaceBytes8(Pix pixs, byte[] pixelData, int width,
00114                         int height) {
00115                 if (pixs == null)
00116                         throw new IllegalArgumentException("Source pix must be non-null");
00117                 if (pixelData == null)
00118                         throw new IllegalArgumentException("Byte array must be non-null");
00119                 if (width <= 0)
00120                         throw new IllegalArgumentException(
00121                                         "Image width must be greater than 0");
00122                 if (height <= 0)
00123                         throw new IllegalArgumentException(
00124                                         "Image height must be greater than 0");
00125                 if (pixelData.length < width * height)
00126                         throw new IllegalArgumentException(
00127                                         "Array length does not match dimensions");
00128                 if (pixs.getWidth() != width)
00129                         throw new IllegalArgumentException(
00130                                         "Source pix width does not match image width");
00131                 if (pixs.getHeight() != height)
00132                         throw new IllegalArgumentException(
00133                                         "Source pix width does not match image width");
00134 
00135                 return nativeReplaceBytes8(pixs.mNativePix, pixelData, width, height);
00136         }
00137 
00148         public static Pixa readFiles(File dir, String prefix) {
00149                 if (dir == null)
00150                         throw new IllegalArgumentException("Directory must be non-null");
00151                 if (!dir.exists())
00152                         throw new IllegalArgumentException("Directory does not exist");
00153                 if (!dir.canRead())
00154                         throw new IllegalArgumentException("Cannot read directory");
00155 
00156                 // TODO: Remove or fix this.
00157                 throw new RuntimeException("readFiles() is not current supported");
00158         }
00159 
00168         public static Pix readFile(File file) {
00169                 if (file == null)
00170                         throw new IllegalArgumentException("File must be non-null");
00171                 if (!file.exists())
00172                         throw new IllegalArgumentException("File does not exist");
00173                 if (!file.canRead())
00174                         throw new IllegalArgumentException("Cannot read file");
00175 
00176                 final BitmapFactory.Options opts = new BitmapFactory.Options();
00177                 opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
00178 
00179                 final Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath(),
00180                                 opts);
00181                 final Pix pix = readBitmap(bmp);
00182 
00183                 bmp.recycle();
00184 
00185                 return pix;
00186         }
00187 
00196         public static Pix readBitmap(Bitmap bmp) {
00197                 if (bmp == null)
00198                         throw new IllegalArgumentException("Bitmap must be non-null");
00199                 if (bmp.getConfig() != Bitmap.Config.ARGB_8888)
00200                         throw new IllegalArgumentException(
00201                                         "Bitmap config must be ARGB_8888");
00202 
00203                 int nativePix = nativeReadBitmap(bmp);
00204 
00205                 if (nativePix == 0)
00206                         throw new RuntimeException("Failed to read pix from bitmap");
00207 
00208                 return new Pix(nativePix);
00209         }
00210 
00211         // ***************
00212         // * NATIVE CODE *
00213         // ***************
00214 
00215         private static native int nativeReadMem(byte[] data, int size);
00216 
00217         private static native int nativeReadBytes8(byte[] data, int w, int h);
00218 
00219         private static native boolean nativeReplaceBytes8(int nativePix,
00220                         byte[] data, int w, int h);
00221 
00222         private static native int nativeReadFiles(String dirname, String prefix);
00223 
00224         private static native int nativeReadFile(String filename);
00225 
00226         private static native int nativeReadBitmap(Bitmap bitmap);
00227 }
 Todo Clases Namespaces Archivos Funciones Variables