Skip to content
This repository has been archived by the owner on Mar 17, 2022. It is now read-only.

Commit

Permalink
Don't crash when image loading fails for readFile/readMem/readBitmap
Browse files Browse the repository at this point in the history
  • Loading branch information
rmtheis committed May 13, 2015
1 parent 485992f commit 6e0d209
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions tess-two/src/com/googlecode/leptonica/android/ReadFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import java.io.File;

Expand All @@ -31,6 +32,8 @@ public class ReadFile {
System.loadLibrary("lept");
}

private static final String LOG_TAG = ReadFile.class.getSimpleName();

/**
* Creates a 32bpp Pix object from encoded data. Supported formats are BMP
* and JPEG.
Expand All @@ -39,8 +42,10 @@ public class ReadFile {
* @return a 32bpp Pix object
*/
public static Pix readMem(byte[] encodedData) {
if (encodedData == null)
throw new IllegalArgumentException("Image data byte array must be non-null");
if (encodedData == null) {
Log.e(LOG_TAG, "Image data byte array must be non-null");
return null;
}

final BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
Expand Down Expand Up @@ -137,17 +142,27 @@ public static Pixa readFiles(File dir, String prefix) {
* @return a Pix object
*/
public static Pix readFile(File file) {
if (file == null)
throw new IllegalArgumentException("File must be non-null");
if (!file.exists())
throw new IllegalArgumentException("File does not exist");
if (!file.canRead())
throw new IllegalArgumentException("Cannot read file");
if (file == null) {
Log.e(LOG_TAG, "File must be non-null");
return null;
}
if (!file.exists()) {
Log.e(LOG_TAG, "File does not exist");
return null;
}
if (!file.canRead()) {
Log.e(LOG_TAG, "Cannot read file");
return null;
}

final BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;

final Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath(), opts);
if (bmp == null) {
Log.e(LOG_TAG, "Cannot decode bitmap");
return null;
}
final Pix pix = readBitmap(bmp);

bmp.recycle();
Expand All @@ -163,15 +178,21 @@ public static Pix readFile(File file) {
* @return a Pix object
*/
public static Pix readBitmap(Bitmap bmp) {
if (bmp == null)
throw new IllegalArgumentException("Bitmap must be non-null");
if (bmp.getConfig() != Bitmap.Config.ARGB_8888)
throw new IllegalArgumentException("Bitmap config must be ARGB_8888");
if (bmp == null) {
Log.e(LOG_TAG, "Bitmap must be non-null");
return null;
}
if (bmp.getConfig() != Bitmap.Config.ARGB_8888) {
Log.e(LOG_TAG, "Bitmap config must be ARGB_8888");
return null;
}

long nativePix = nativeReadBitmap(bmp);

if (nativePix == 0)
throw new RuntimeException("Failed to read pix from bitmap");
if (nativePix == 0) {
Log.e(LOG_TAG, "Failed to read pix from bitmap");
return null;
}

return new Pix(nativePix);
}
Expand Down

0 comments on commit 6e0d209

Please sign in to comment.