53 | * Notes: 54 | *
61 | * Notes: 62 | *
true
on success
141 | */
142 | public boolean getGeometry(int[] geometry) {
143 | if (geometry.length < 4) {
144 | throw new IllegalArgumentException("Geometry array must be at least 4 elements long");
145 | }
146 |
147 | return nativeGetGeometry(mNativeBox, geometry);
148 | }
149 |
150 | /**
151 | * Releases resources and frees any memory associated with this Box.
152 | */
153 | public void recycle() {
154 | if (!mRecycled) {
155 | nativeDestroy(mNativeBox);
156 |
157 | mRecycled = true;
158 | }
159 | }
160 |
161 | @Override
162 | protected void finalize() throws Throwable {
163 | recycle();
164 |
165 | super.finalize();
166 | }
167 |
168 | // ***************
169 | // * NATIVE CODE *
170 | // ***************
171 |
172 | private static native int nativeCreate(int x, int y, int w, int h);
173 | private static native int nativeGetX(int nativeBox);
174 | private static native int nativeGetY(int nativeBox);
175 | private static native int nativeGetWidth(int nativeBox);
176 | private static native int nativeGetHeight(int nativeBox);
177 | private static native void nativeDestroy(int nativeBox);
178 | private static native boolean nativeGetGeometry(int nativeBox, int[] geometry);
179 | }
180 |
--------------------------------------------------------------------------------
/tesseract-android-tools/src/com/googlecode/leptonica/android/Constants.java:
--------------------------------------------------------------------------------
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 |
17 | package com.googlecode.leptonica.android;
18 |
19 | /**
20 | * Leptonica constants.
21 | *
22 | * @author alanv@google.com (Alan Viverette)
23 | */
24 | public class Constants {
25 | /*-------------------------------------------------------------------------*
26 | * Access and storage flags *
27 | *-------------------------------------------------------------------------*/
28 | /*
29 | * For Pix, Box, Pta and Numa, there are 3 standard methods for handling the
30 | * retrieval or insertion of a struct: (1) direct insertion (Don't do this
31 | * if there is another handle somewhere to this same struct!) (2) copy
32 | * (Always safe, sets up a refcount of 1 on the new object. Can be
33 | * undesirable if very large, such as an image or an array of images.) (3)
34 | * clone (Makes another handle to the same struct, and bumps the refcount up
35 | * by 1. Safe to do unless you're changing data through one of the handles
36 | * but don't want those changes to be seen by the other handle.) For Pixa
37 | * and Boxa, which are structs that hold an array of clonable structs, there
38 | * is an additional method: (4) copy-clone (Makes a new higher-level struct
39 | * with a refcount of 1, but clones all the structs in the array.) Unlike
40 | * the other structs, when retrieving a string from an Sarray, you are
41 | * allowed to get a handle without a copy or clone (i.e., that you don't
42 | * own!). You must not free or insert such a string! Specifically, for an
43 | * Sarray, the copyflag for retrieval is either: TRUE (or 1 or L_COPY) or
44 | * FALSE (or 0 or L_NOCOPY) For insertion, the copyflag is either: TRUE (or
45 | * 1 or L_COPY) or FALSE (or 0 or L_INSERT) Note that L_COPY is always 1,
46 | * and L_INSERT and L_NOCOPY are always 0.
47 | */
48 |
49 | /* Stuff it in; no copy, clone or copy-clone */
50 | public static final int L_INSERT = 0;
51 |
52 | /* Make/use a copy of the object */
53 | public static final int L_COPY = 1;
54 |
55 | /* Make/use clone (ref count) of the object */
56 | public static final int L_CLONE = 2;
57 |
58 | /*
59 | * Make a new object and fill with with clones of each object in the
60 | * array(s)
61 | */
62 | public static final int L_COPY_CLONE = 3;
63 |
64 | /*--------------------------------------------------------------------------*
65 | * Sort flags *
66 | *--------------------------------------------------------------------------*/
67 |
68 | /* Sort in increasing order */
69 | public static final int L_SORT_INCREASING = 1;
70 |
71 | /* Sort in decreasing order */
72 | public static final int L_SORT_DECREASING = 2;
73 |
74 | /* Sort box or c.c. by horiz location */
75 | public static final int L_SORT_BY_X = 3;
76 |
77 | /* Sort box or c.c. by vert location */
78 | public static final int L_SORT_BY_Y = 4;
79 |
80 | /* Sort box or c.c. by width */
81 | public static final int L_SORT_BY_WIDTH = 5;
82 |
83 | /* Sort box or c.c. by height */
84 | public static final int L_SORT_BY_HEIGHT = 6;
85 |
86 | /* Sort box or c.c. by min dimension */
87 | public static final int L_SORT_BY_MIN_DIMENSION = 7;
88 |
89 | /* Sort box or c.c. by max dimension */
90 | public static final int L_SORT_BY_MAX_DIMENSION = 8;
91 |
92 | /* Sort box or c.c. by perimeter */
93 | public static final int L_SORT_BY_PERIMETER = 9;
94 |
95 | /* Sort box or c.c. by area */
96 | public static final int L_SORT_BY_AREA = 10;
97 |
98 | /* Sort box or c.c. by width/height ratio */
99 | public static final int L_SORT_BY_ASPECT_RATIO = 11;
100 |
101 | /* ------------------ Image file format types -------------- */
102 | /*
103 | * The IFF_DEFAULT flag is used to write the file out in the same (input)
104 | * file format that the pix was read from. If the pix was not read from
105 | * file, the input format field will be IFF_UNKNOWN and the output file
106 | * format will be chosen to be compressed and lossless; namely, IFF_TIFF_G4
107 | * for d = 1 and IFF_PNG for everything else. IFF_JP2 is for jpeg2000, which
108 | * is not supported in leptonica.
109 | */
110 |
111 | public static final int IFF_UNKNOWN = 0;
112 |
113 | public static final int IFF_BMP = 1;
114 |
115 | public static final int IFF_JFIF_JPEG = 2;
116 |
117 | public static final int IFF_PNG = 3;
118 |
119 | public static final int IFF_TIFF = 4;
120 |
121 | public static final int IFF_TIFF_PACKBITS = 5;
122 |
123 | public static final int IFF_TIFF_RLE = 6;
124 |
125 | public static final int IFF_TIFF_G3 = 7;
126 |
127 | public static final int IFF_TIFF_G4 = 8;
128 |
129 | public static final int IFF_TIFF_LZW = 9;
130 |
131 | public static final int IFF_TIFF_ZIP = 10;
132 |
133 | public static final int IFF_PNM = 11;
134 |
135 | public static final int IFF_PS = 12;
136 |
137 | public static final int IFF_GIF = 13;
138 |
139 | public static final int IFF_JP2 = 14;
140 |
141 | public static final int IFF_DEFAULT = 15;
142 |
143 | public static final int IFF_SPIX = 16;
144 | }
145 |
--------------------------------------------------------------------------------
/tesseract-android-tools/src/com/googlecode/leptonica/android/Convert.java:
--------------------------------------------------------------------------------
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 |
17 | package com.googlecode.leptonica.android;
18 |
19 | /**
20 | * Image bit-depth conversion methods.
21 | *
22 | * @author alanv@google.com (Alan Viverette)
23 | */
24 | public class Convert {
25 | static {
26 | System.loadLibrary("lept");
27 | }
28 |
29 | /**
30 | * Converts an image of any bit depth to 8-bit grayscale.
31 | *
32 | * @param pixs Source pix of any bit-depth.
33 | * @return a new Pix image or null
on error
34 | */
35 | public static Pix convertTo8(Pix pixs) {
36 | if (pixs == null)
37 | throw new IllegalArgumentException("Source pix must be non-null");
38 |
39 | int nativePix = nativeConvertTo8(pixs.mNativePix);
40 |
41 | if (nativePix == 0)
42 | throw new RuntimeException("Failed to natively convert pix");
43 |
44 | return new Pix(nativePix);
45 | }
46 |
47 | // ***************
48 | // * NATIVE CODE *
49 | // ***************
50 |
51 | private static native int nativeConvertTo8(int nativePix);
52 | }
53 |
--------------------------------------------------------------------------------
/tesseract-android-tools/src/com/googlecode/leptonica/android/Enhance.java:
--------------------------------------------------------------------------------
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 |
17 | package com.googlecode.leptonica.android;
18 |
19 | /**
20 | * Image sharpening methods.
21 | *
22 | * @author alanv@google.com (Alan Viverette)
23 | */
24 | public class Enhance {
25 | static {
26 | System.loadLibrary("lept");
27 | }
28 |
29 | /**
30 | * Performs unsharp masking (edge enhancement).
31 | * 32 | * Notes: 33 | *
halfwidth
parameter for these is
36 | * (size - 1)/2; i.e., 1, 2, 3, etc.fract
parameter is typically taken in the range: 0.2
38 | * < fract
< 0.7null
on
101 | * failure
102 | */
103 | public int[] getDimensions() {
104 | int[] dimensions = new int[4];
105 |
106 | if (getDimensions(dimensions)) {
107 | return dimensions;
108 | }
109 |
110 | return null;
111 | }
112 |
113 | /**
114 | * Fills an array with this image's dimensions. The array must be at least 3
115 | * elements long.
116 | *
117 | * @param dimensions An integer array with at least three elements.
118 | * @return true
on success
119 | */
120 | public boolean getDimensions(int[] dimensions) {
121 | return nativeGetDimensions(mNativePix, dimensions);
122 | }
123 |
124 | /**
125 | * Returns a clone of this Pix. This does NOT create a separate copy, just a
126 | * new pointer that can be recycled without affecting other clones.
127 | *
128 | * @return a clone (shallow copy) of the Pix
129 | */
130 | @Override
131 | public Pix clone() {
132 | int nativePix = nativeClone(mNativePix);
133 |
134 | if (nativePix == 0) {
135 | throw new OutOfMemoryError();
136 | }
137 |
138 | return new Pix(nativePix);
139 | }
140 |
141 | /**
142 | * Returns a deep copy of this Pix that can be modified without affecting
143 | * the original Pix.
144 | *
145 | * @return a copy of the Pix
146 | */
147 | public Pix copy() {
148 | int nativePix = nativeCopy(mNativePix);
149 |
150 | if (nativePix == 0) {
151 | throw new OutOfMemoryError();
152 | }
153 |
154 | return new Pix(nativePix);
155 | }
156 |
157 | /**
158 | * Inverts this Pix in-place.
159 | *
160 | * @return true
on success
161 | */
162 | public boolean invert() {
163 | return nativeInvert(mNativePix);
164 | }
165 |
166 | /**
167 | * Releases resources and frees any memory associated with this Pix. You may
168 | * not modify or access the pix after calling this method.
169 | */
170 | public void recycle() {
171 | if (!mRecycled) {
172 | nativeDestroy(mNativePix);
173 |
174 | mRecycled = true;
175 | }
176 | }
177 |
178 | @Override
179 | protected void finalize() throws Throwable {
180 | recycle();
181 |
182 | super.finalize();
183 | }
184 |
185 | /**
186 | * Creates a new Pix from raw Pix data obtained from getData().
187 | *
188 | * @param pixData Raw pix data obtained from getData().
189 | * @param width The width of the original Pix.
190 | * @param height The height of the original Pix.
191 | * @param depth The bit-depth of the original Pix.
192 | * @return a new Pix or null
on error
193 | */
194 | public static Pix createFromPix(byte[] pixData, int width, int height, int depth) {
195 | int nativePix = nativeCreateFromData(pixData, width, height, depth);
196 |
197 | if (nativePix == 0) {
198 | throw new OutOfMemoryError();
199 | }
200 |
201 | return new Pix(nativePix);
202 | }
203 |
204 | /**
205 | * Returns a Rect with the width and height of this Pix.
206 | *
207 | * @return a Rect with the width and height of this Pix
208 | */
209 | public Rect getRect() {
210 | int w = getWidth();
211 | int h = getHeight();
212 |
213 | return new Rect(0, 0, w, h);
214 | }
215 |
216 | /**
217 | * Returns the width of this Pix.
218 | *
219 | * @return the width of this Pix
220 | */
221 | public int getWidth() {
222 | return nativeGetWidth(mNativePix);
223 | }
224 |
225 | /**
226 | * Returns the height of this Pix.
227 | *
228 | * @return the height of this Pix
229 | */
230 | public int getHeight() {
231 | return nativeGetHeight(mNativePix);
232 | }
233 |
234 | /**
235 | * Returns the depth of this Pix.
236 | *
237 | * @return the depth of this Pix
238 | */
239 | public int getDepth() {
240 | return nativeGetDepth(mNativePix);
241 | }
242 |
243 | /**
244 | * Returns the {@link android.graphics.Color} at the specified location.
245 | *
246 | * @param x The x coordinate (0...width-1) of the pixel to return.
247 | * @param y The y coordinate (0...height-1) of the pixel to return.
248 | * @return The argb {@link android.graphics.Color} at the specified
249 | * coordinate.
250 | * @throws IllegalArgumentException If x, y exceeds the image bounds.
251 | */
252 | public int getPixel(int x, int y) {
253 | if (x < 0 || x >= getWidth()) {
254 | throw new IllegalArgumentException("Supplied x coordinate exceeds image bounds");
255 | } else if (y < 0 || y >= getHeight()) {
256 | throw new IllegalArgumentException("Supplied x coordinate exceeds image bounds");
257 | }
258 |
259 | return nativeGetPixel(mNativePix, x, y);
260 | }
261 |
262 | /**
263 | * Sets the {@link android.graphics.Color} at the specified location.
264 | *
265 | * @param x The x coordinate (0...width-1) of the pixel to set.
266 | * @param y The y coordinate (0...height-1) of the pixel to set.
267 | * @param color The argb {@link android.graphics.Color} to set at the
268 | * specified coordinate.
269 | * @throws IllegalArgumentException If x, y exceeds the image bounds.
270 | */
271 | public void setPixel(int x, int y, int color) {
272 | if (x < 0 || x >= getWidth()) {
273 | throw new IllegalArgumentException("Supplied x coordinate exceeds image bounds");
274 | } else if (y < 0 || y >= getHeight()) {
275 | throw new IllegalArgumentException("Supplied x coordinate exceeds image bounds");
276 | }
277 |
278 | nativeSetPixel(mNativePix, x, y, color);
279 | }
280 |
281 | // ***************
282 | // * NATIVE CODE *
283 | // ***************
284 |
285 | private static native int nativeCreatePix(int w, int h, int d);
286 | private static native int nativeCreateFromData(byte[] data, int w, int h, int d);
287 | private static native boolean nativeGetData(int nativePix, byte[] data);
288 | private static native int nativeGetDataSize(int nativePix);
289 | private static native int nativeClone(int nativePix);
290 | private static native int nativeCopy(int nativePix);
291 | private static native boolean nativeInvert(int nativePix);
292 | private static native void nativeDestroy(int nativePix);
293 | private static native boolean nativeGetDimensions(int nativePix, int[] dimensions);
294 | private static native int nativeGetWidth(int nativePix);
295 | private static native int nativeGetHeight(int nativePix);
296 | private static native int nativeGetDepth(int nativePix);
297 | private static native int nativeGetPixel(int nativePix, int x, int y);
298 | private static native void nativeSetPixel(int nativePix, int x, int y, int color);
299 | }
300 |
--------------------------------------------------------------------------------
/tesseract-android-tools/src/com/googlecode/leptonica/android/ReadFile.java:
--------------------------------------------------------------------------------
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 |
17 | package com.googlecode.leptonica.android;
18 |
19 | import android.graphics.Bitmap;
20 | import android.graphics.BitmapFactory;
21 |
22 | import java.io.File;
23 |
24 | /**
25 | * Image input and output methods.
26 | *
27 | * @author alanv@google.com (Alan Viverette)
28 | */
29 | public class ReadFile {
30 | static {
31 | System.loadLibrary("lept");
32 | }
33 |
34 | /**
35 | * Creates a 32bpp Pix object from encoded data. Supported formats are BMP
36 | * and JPEG.
37 | *
38 | * @param encodedData JPEG or BMP encoded byte data.
39 | * @return a 32bpp Pix object
40 | */
41 | public static Pix readMem(byte[] encodedData) {
42 | if (encodedData == null)
43 | throw new IllegalArgumentException("Image data byte array must be non-null");
44 |
45 | final BitmapFactory.Options opts = new BitmapFactory.Options();
46 | opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
47 |
48 | final Bitmap bmp = BitmapFactory.decodeByteArray(encodedData, 0, encodedData.length,
49 | opts);
50 | final Pix pix = readBitmap(bmp);
51 |
52 | bmp.recycle();
53 |
54 | return pix;
55 | }
56 |
57 | /**
58 | * Creates an 8bpp Pix object from raw 8bpp grayscale pixels.
59 | *
60 | * @param pixelData 8bpp grayscale pixel data.
61 | * @param width The width of the input image.
62 | * @param height The height of the input image.
63 | * @return an 8bpp Pix object
64 | */
65 | public static Pix readBytes8(byte[] pixelData, int width, int height) {
66 | if (pixelData == null)
67 | throw new IllegalArgumentException("Byte array must be non-null");
68 | if (width <= 0)
69 | throw new IllegalArgumentException("Image width must be greater than 0");
70 | if (height <= 0)
71 | throw new IllegalArgumentException("Image height must be greater than 0");
72 | if (pixelData.length < width * height)
73 | throw new IllegalArgumentException("Array length does not match dimensions");
74 |
75 | int nativePix = nativeReadBytes8(pixelData, width, height);
76 |
77 | if (nativePix == 0)
78 | throw new RuntimeException("Failed to read pix from memory");
79 |
80 | return new Pix(nativePix);
81 | }
82 |
83 | /**
84 | * Replaces the bytes in an 8bpp Pix object with raw grayscale 8bpp pixels.
85 | * Width and height be identical to the input Pix.
86 | *
87 | * @param pixs The Pix whose bytes will be replaced.
88 | * @param pixelData 8bpp grayscale pixel data.
89 | * @param width The width of the input image.
90 | * @param height The height of the input image.
91 | * @return an 8bpp Pix object
92 | */
93 | public static boolean replaceBytes8(Pix pixs, byte[] pixelData, int width, int height) {
94 | if (pixs == null)
95 | throw new IllegalArgumentException("Source pix must be non-null");
96 | if (pixelData == null)
97 | throw new IllegalArgumentException("Byte array must be non-null");
98 | if (width <= 0)
99 | throw new IllegalArgumentException("Image width must be greater than 0");
100 | if (height <= 0)
101 | throw new IllegalArgumentException("Image height must be greater than 0");
102 | if (pixelData.length < width * height)
103 | throw new IllegalArgumentException("Array length does not match dimensions");
104 | if (pixs.getWidth() != width)
105 | throw new IllegalArgumentException("Source pix width does not match image width");
106 | if (pixs.getHeight() != height)
107 | throw new IllegalArgumentException("Source pix width does not match image width");
108 |
109 | return nativeReplaceBytes8(pixs.mNativePix, pixelData, width, height);
110 | }
111 |
112 | /**
113 | * Creates a Pixa object from encoded files in a directory. Supported
114 | * formats are BMP and JPEG.
115 | *
116 | * @param dir The directory containing the files.
117 | * @param prefix The prefix of the files to load into a Pixa.
118 | * @return a Pixa object containing one Pix for each file
119 | */
120 | public static Pixa readFiles(File dir, String prefix) {
121 | if (dir == null)
122 | throw new IllegalArgumentException("Directory must be non-null");
123 | if (!dir.exists())
124 | throw new IllegalArgumentException("Directory does not exist");
125 | if (!dir.canRead())
126 | throw new IllegalArgumentException("Cannot read directory");
127 |
128 | // TODO: Remove or fix this.
129 | throw new RuntimeException("readFiles() is not current supported");
130 | }
131 |
132 | /**
133 | * Creates a Pix object from encoded file data. Supported formats are BMP
134 | * and JPEG.
135 | *
136 | * @param file The JPEG or BMP-encoded file to read in as a Pix.
137 | * @return a Pix object
138 | */
139 | public static Pix readFile(File file) {
140 | if (file == null)
141 | throw new IllegalArgumentException("File must be non-null");
142 | if (!file.exists())
143 | throw new IllegalArgumentException("File does not exist");
144 | if (!file.canRead())
145 | throw new IllegalArgumentException("Cannot read file");
146 |
147 | final BitmapFactory.Options opts = new BitmapFactory.Options();
148 | opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
149 |
150 | final Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath(), opts);
151 | final Pix pix = readBitmap(bmp);
152 |
153 | bmp.recycle();
154 |
155 | return pix;
156 | }
157 |
158 | /**
159 | * Creates a Pix object from Bitmap data. Currently supports only
160 | * ARGB_8888-formatted bitmaps.
161 | *
162 | * @param bmp The Bitmap object to convert to a Pix.
163 | * @return a Pix object
164 | */
165 | public static Pix readBitmap(Bitmap bmp) {
166 | if (bmp == null)
167 | throw new IllegalArgumentException("Bitmap must be non-null");
168 | if (bmp.getConfig() != Bitmap.Config.ARGB_8888)
169 | throw new IllegalArgumentException("Bitmap config must be ARGB_8888");
170 |
171 | int nativePix = nativeReadBitmap(bmp);
172 |
173 | if (nativePix == 0)
174 | throw new RuntimeException("Failed to read pix from bitmap");
175 |
176 | return new Pix(nativePix);
177 | }
178 |
179 | // ***************
180 | // * NATIVE CODE *
181 | // ***************
182 |
183 | private static native int nativeReadMem(byte[] data, int size);
184 |
185 | private static native int nativeReadBytes8(byte[] data, int w, int h);
186 |
187 | private static native boolean nativeReplaceBytes8(int nativePix, byte[] data, int w, int h);
188 |
189 | private static native int nativeReadFiles(String dirname, String prefix);
190 |
191 | private static native int nativeReadFile(String filename);
192 |
193 | private static native int nativeReadBitmap(Bitmap bitmap);
194 | }
195 |
--------------------------------------------------------------------------------
/tesseract-android-tools/src/com/googlecode/leptonica/android/Rotate.java:
--------------------------------------------------------------------------------
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 |
17 | package com.googlecode.leptonica.android;
18 |
19 | /**
20 | * @author alanv@google.com (Alan Viverette)
21 | */
22 | public class Rotate {
23 | static {
24 | System.loadLibrary("lept");
25 | }
26 |
27 | // Rotation default
28 |
29 | /** Default rotation quality is high. */
30 | public static final boolean ROTATE_QUALITY = true;
31 |
32 | /**
33 | * Performs rotation using the default parameters.
34 | *
35 | * @param pixs The source pix.
36 | * @param degrees The number of degrees to rotate; clockwise is positive.
37 | * @return the rotated source image
38 | */
39 | public static Pix rotate(Pix pixs, float degrees) {
40 | return rotate(pixs, degrees, false);
41 | }
42 |
43 | /**
44 | * Performs rotation with resizing using the default parameters.
45 | *
46 | * @param pixs The source pix.
47 | * @param degrees The number of degrees to rotate; clockwise is positive.
48 | * @param quality Whether to use high-quality rotation.
49 | * @return the rotated source image
50 | */
51 | public static Pix rotate(Pix pixs, float degrees, boolean quality) {
52 | return rotate(pixs, degrees, quality, true);
53 | }
54 |
55 | /**
56 | * Performs basic image rotation about the center.
57 | * 58 | * Notes: 59 | *
61 | * Notes: 62 | *
88 | * Output file names will take the format
126 | * Uses default quality and progressive encoding settings.
127 | *
128 | * @param pixs Source image.
129 | * @param file The file to write.
130 | * @return
140 | * Notes:
141 | *
45 | * NOTE that {@link PageIteratorLevel#RIL_SYMBOL} will skip non-text blocks,
46 | * but all other {@link PageIteratorLevel} level values will visit each
47 | * non-text block once. Think of non text blocks as containing a single
48 | * para, with a single line, with a single imaginary word.
49 | *
50 | * Calls to {@link #next} with different levels may be freely intermixed.
51 | *
52 | * This function iterates words in right-to-left scripts correctly, if the
53 | * appropriate language has been loaded into Tesseract.
54 | *
55 | * @param level the page iterator level. See {@link PageIteratorLevel}.
56 | * @return {@code false} if the end of the page was reached, {@code true}
57 | * otherwise.
58 | */
59 | public boolean next(int level) {
60 | return nativeNext(mNativePageIterator, level);
61 | }
62 |
63 | private static native void nativeBegin(int nativeIterator);
64 | private static native boolean nativeNext(int nativeIterator, int level);
65 | }
66 |
--------------------------------------------------------------------------------
/tesseract-android-tools/src/com/googlecode/tesseract/android/ResultIterator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 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 |
17 | package com.googlecode.tesseract.android;
18 |
19 | import com.googlecode.tesseract.android.TessBaseAPI.PageIteratorLevel;
20 |
21 | /**
22 | * Java interface for the ResultIterator. Does not implement all available JNI
23 | * methods, but does implement enough to be useful. Comments are adapted from
24 | * original Tesseract source.
25 | *
26 | * @author alanv@google.com (Alan Viverette)
27 | */
28 | public class ResultIterator extends PageIterator {
29 | static {
30 | System.loadLibrary("lept");
31 | System.loadLibrary("tess");
32 | }
33 |
34 | /** Pointer to native result iterator. */
35 | private final int mNativeResultIterator;
36 |
37 | /* package */ResultIterator(int nativeResultIterator) {
38 | super(nativeResultIterator);
39 |
40 | mNativeResultIterator = nativeResultIterator;
41 | }
42 |
43 | /**
44 | * Returns the text string for the current object at the given level.
45 | *
46 | * @param level the page iterator level. See {@link PageIteratorLevel}.
47 | * @return the text string for the current object at the given level.
48 | */
49 | public String getUTF8Text(int level) {
50 | return nativeGetUTF8Text(mNativeResultIterator, level);
51 | }
52 |
53 | /**
54 | * Returns the mean confidence of the current object at the given level. The
55 | * number should be interpreted as a percent probability (0-100).
56 | *
57 | * @param level the page iterator level. See {@link PageIteratorLevel}.
58 | * @return the mean confidence of the current object at the given level.
59 | */
60 | public float confidence(int level) {
61 | return nativeConfidence(mNativeResultIterator, level);
62 | }
63 |
64 | private static native String nativeGetUTF8Text(int nativeResultIterator, int level);
65 | private static native float nativeConfidence(int nativeResultIterator, int level);
66 | }
67 |
--------------------------------------------------------------------------------
true
on success
95 | */
96 | public static boolean writeFiles(Pixa pixas, File path, String prefix, int format) {
97 | if (pixas == null)
98 | throw new IllegalArgumentException("Source pixa must be non-null");
99 | if (path == null)
100 | throw new IllegalArgumentException("Destination path non-null");
101 | if (prefix == null)
102 | throw new IllegalArgumentException("Filename prefix must be non-null");
103 |
104 | throw new RuntimeException("writeFiles() is not currently supported");
105 | }
106 |
107 | /**
108 | * Write a Pix to a byte array using the specified encoding from
109 | * Constants.IFF_*.
110 | *
111 | * @param pixs The source image.
112 | * @param format A format from Constants.IFF_*.
113 | * @return a byte array containing encoded bytes
114 | */
115 | public static byte[] writeMem(Pix pixs, int format) {
116 | if (pixs == null)
117 | throw new IllegalArgumentException("Source pix must be non-null");
118 |
119 | return nativeWriteMem(pixs.mNativePix, format);
120 | }
121 |
122 | /**
123 | * Writes a Pix to file using the file extension as the output format;
124 | * supported formats are .jpg or .jpeg for JPEG and .bmp for bitmap.
125 | * true
on success
131 | */
132 | public static boolean writeImpliedFormat(Pix pixs, File file) {
133 | return writeImpliedFormat(pixs, file, DEFAULT_QUALITY, DEFAULT_PROGRESSIVE);
134 | }
135 |
136 | /**
137 | * Writes a Pix to file using the file extension as the output format;
138 | * supported formats are .jpg or .jpeg for JPEG and .bmp for bitmap.
139 | *
142 | *
146 | *
147 | * @param pixs Source image.
148 | * @param file The file to write.
149 | * @param quality (Only for lossy formats) Quality between 1 - 100, 0 for
150 | * default.
151 | * @param progressive (Only for JPEG) Whether to encode as progressive.
152 | * @return true
on success
153 | */
154 | public static boolean writeImpliedFormat(
155 | Pix pixs, File file, int quality, boolean progressive) {
156 | if (pixs == null)
157 | throw new IllegalArgumentException("Source pix must be non-null");
158 | if (file == null)
159 | throw new IllegalArgumentException("File must be non-null");
160 |
161 | return nativeWriteImpliedFormat(
162 | pixs.mNativePix, file.getAbsolutePath(), quality, progressive);
163 | }
164 |
165 | /**
166 | * Writes a Pix to an Android Bitmap object. The output Bitmap will always
167 | * be in ARGB_8888 format, but the input Pixs may be any bit-depth.
168 | *
169 | * @param pixs The source image.
170 | * @return a Bitmap containing a copy of the source image, or null
171 | *
on failure
172 | */
173 | public static Bitmap writeBitmap(Pix pixs) {
174 | if (pixs == null)
175 | throw new IllegalArgumentException("Source pix must be non-null");
176 |
177 | final int[] dimensions = pixs.getDimensions();
178 | final int width = dimensions[Pix.INDEX_W];
179 | final int height = dimensions[Pix.INDEX_H];
180 | //final int depth = dimensions[Pix.INDEX_D];
181 |
182 | final Bitmap.Config config = Bitmap.Config.ARGB_8888;
183 | final Bitmap bitmap = Bitmap.createBitmap(width, height, config);
184 |
185 | if (nativeWriteBitmap(pixs.mNativePix, bitmap)) {
186 | return bitmap;
187 | }
188 |
189 | bitmap.recycle();
190 |
191 | return null;
192 | }
193 |
194 | // ***************
195 | // * NATIVE CODE *
196 | // ***************
197 |
198 | private static native int nativeWriteBytes8(int nativePix, byte[] data);
199 |
200 | private static native boolean nativeWriteFiles(int nativePix, String rootname, int format);
201 |
202 | private static native byte[] nativeWriteMem(int nativePix, int format);
203 |
204 | private static native boolean nativeWriteImpliedFormat(
205 | int nativePix, String fileName, int quality, boolean progressive);
206 |
207 | private static native boolean nativeWriteBitmap(int nativePix, Bitmap bitmap);
208 | }
209 |
--------------------------------------------------------------------------------
/tesseract-android-tools/src/com/googlecode/tesseract/android/PageIterator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2012 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 |
17 | package com.googlecode.tesseract.android;
18 |
19 | import com.googlecode.tesseract.android.TessBaseAPI.PageIteratorLevel;
20 |
21 | public class PageIterator {
22 | static {
23 | System.loadLibrary("lept");
24 | System.loadLibrary("tess");
25 | }
26 |
27 | /** Pointer to native page iterator. */
28 | private final int mNativePageIterator;
29 |
30 | /* package */PageIterator(int nativePageIterator) {
31 | mNativePageIterator = nativePageIterator;
32 | }
33 |
34 | /**
35 | * Resets the iterator to point to the start of the page.
36 | */
37 | public void begin() {
38 | nativeBegin(mNativePageIterator);
39 | }
40 |
41 | /**
42 | * Moves to the start of the next object at the given level in the page
43 | * hierarchy, and returns false if the end of the page was reached.
44 | *