Tesseract-android-tools  1.0
 Todo Clases Namespaces Archivos Funciones Variables
WriteFile.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 
00026 package com.googlecode.leptonica.android;
00027 
00028 import android.graphics.Bitmap;
00029 
00030 import java.io.File;
00031 
00036 public class WriteFile {
00037         static {
00038                 System.loadLibrary("lept");
00039         }
00040 
00042         public static final int DEFAULT_QUALITY = 85;
00043 
00045         public static final boolean DEFAULT_PROGRESSIVE = true;
00046 
00054         public static byte[] writeBytes8(Pix pixs) {
00055                 if (pixs == null)
00056                         throw new IllegalArgumentException("Source pix must be non-null");
00057 
00058                 int size = pixs.getWidth() * pixs.getHeight();
00059 
00060                 if (pixs.getDepth() != 8) {
00061                         Pix pix8 = Convert.convertTo8(pixs);
00062                         pixs.recycle();
00063                         pixs = pix8;
00064                 }
00065 
00066                 byte[] data = new byte[size];
00067 
00068                 writeBytes8(pixs, data);
00069 
00070                 return data;
00071         }
00072 
00082         public static int writeBytes8(Pix pixs, byte[] data) {
00083                 if (pixs == null)
00084                         throw new IllegalArgumentException("Source pix must be non-null");
00085 
00086                 int size = pixs.getWidth() * pixs.getHeight();
00087 
00088                 if (data.length < size)
00089                         throw new IllegalArgumentException(
00090                                         "Data array must be large enough to hold image bytes");
00091 
00092                 int bytesWritten = nativeWriteBytes8(pixs.mNativePix, data);
00093 
00094                 return bytesWritten;
00095         }
00096 
00115         public static boolean writeFiles(Pixa pixas, File path, String prefix,
00116                         int format) {
00117                 if (pixas == null)
00118                         throw new IllegalArgumentException("Source pixa must be non-null");
00119                 if (path == null)
00120                         throw new IllegalArgumentException("Destination path non-null");
00121                 if (prefix == null)
00122                         throw new IllegalArgumentException(
00123                                         "Filename prefix must be non-null");
00124 
00125                 throw new RuntimeException("writeFiles() is not currently supported");
00126         }
00127 
00138         public static byte[] writeMem(Pix pixs, int format) {
00139                 if (pixs == null)
00140                         throw new IllegalArgumentException("Source pix must be non-null");
00141 
00142                 return nativeWriteMem(pixs.mNativePix, format);
00143         }
00144 
00158         public static boolean writeImpliedFormat(Pix pixs, File file) {
00159                 return writeImpliedFormat(pixs, file, DEFAULT_QUALITY,
00160                                 DEFAULT_PROGRESSIVE);
00161         }
00162 
00187         public static boolean writeImpliedFormat(Pix pixs, File file, int quality,
00188                         boolean progressive) {
00189                 if (pixs == null)
00190                         throw new IllegalArgumentException("Source pix must be non-null");
00191                 if (file == null)
00192                         throw new IllegalArgumentException("File must be non-null");
00193 
00194                 return nativeWriteImpliedFormat(pixs.mNativePix,
00195                                 file.getAbsolutePath(), quality, progressive);
00196         }
00197 
00208         public static Bitmap writeBitmap(Pix pixs) {
00209                 if (pixs == null)
00210                         throw new IllegalArgumentException("Source pix must be non-null");
00211 
00212                 final int[] dimensions = pixs.getDimensions();
00213                 final int width = dimensions[Pix.INDEX_W];
00214                 final int height = dimensions[Pix.INDEX_H];
00215                 // final int depth = dimensions[Pix.INDEX_D];
00216 
00217                 final Bitmap.Config config = Bitmap.Config.ARGB_8888;
00218                 final Bitmap bitmap = Bitmap.createBitmap(width, height, config);
00219 
00220                 if (nativeWriteBitmap(pixs.mNativePix, bitmap)) {
00221                         return bitmap;
00222                 }
00223 
00224                 bitmap.recycle();
00225 
00226                 return null;
00227         }
00228 
00229         // ***************
00230         // * NATIVE CODE *
00231         // ***************
00232 
00233         private static native int nativeWriteBytes8(int nativePix, byte[] data);
00234 
00235         private static native boolean nativeWriteFiles(int nativePix,
00236                         String rootname, int format);
00237 
00238         private static native byte[] nativeWriteMem(int nativePix, int format);
00239 
00240         private static native boolean nativeWriteImpliedFormat(int nativePix,
00241                         String fileName, int quality, boolean progressive);
00242 
00243         private static native boolean nativeWriteBitmap(int nativePix, Bitmap bitmap);
00244 }
 Todo Clases Namespaces Archivos Funciones Variables