diff --git a/androidtest/src/com/google/zxing/client/androidtest/BenchmarkActivity.java b/androidtest/src/com/google/zxing/client/androidtest/BenchmarkActivity.java index 3b4103653..8b13186e2 100755 --- a/androidtest/src/com/google/zxing/client/androidtest/BenchmarkActivity.java +++ b/androidtest/src/com/google/zxing/client/androidtest/BenchmarkActivity.java @@ -32,7 +32,7 @@ public final class BenchmarkActivity extends Activity { private Button runBenchmarkButton; private TextView textView; - private BenchmarkThread benchmarkThread; + private Thread benchmarkThread; @Override public void onCreate(Bundle icicle) { @@ -53,7 +53,7 @@ public final class BenchmarkActivity extends Activity { if (benchmarkThread == null) { runBenchmarkButton.setEnabled(false); textView.setText(R.string.benchmark_running); - benchmarkThread = new BenchmarkThread(BenchmarkActivity.this, PATH); + benchmarkThread = new Thread(new BenchmarkThread(BenchmarkActivity.this, PATH)); benchmarkThread.start(); } } diff --git a/androidtest/src/com/google/zxing/client/androidtest/BenchmarkThread.java b/androidtest/src/com/google/zxing/client/androidtest/BenchmarkThread.java index 8591c4867..7f5522791 100755 --- a/androidtest/src/com/google/zxing/client/androidtest/BenchmarkThread.java +++ b/androidtest/src/com/google/zxing/client/androidtest/BenchmarkThread.java @@ -16,8 +16,11 @@ package com.google.zxing.client.androidtest; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import com.google.zxing.BinaryBitmap; import com.google.zxing.MultiFormatReader; +import com.google.zxing.RGBLuminanceSource; import com.google.zxing.ReaderException; import com.google.zxing.Result; import com.google.zxing.common.HybridBinarizer; @@ -27,12 +30,11 @@ import android.os.Message; import android.util.Log; import java.io.File; -import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -final class BenchmarkThread extends Thread { +final class BenchmarkThread implements Runnable { private static final String TAG = BenchmarkThread.class.getSimpleName(); private static final int RUNS = 10; @@ -78,14 +80,20 @@ final class BenchmarkThread extends Thread { } private BenchmarkItem decode(String path) { - RGBLuminanceSource source; - try { - source = new RGBLuminanceSource(path); - } catch (FileNotFoundException e) { - Log.e(TAG, e.toString()); + + Bitmap imageBitmap = BitmapFactory.decodeFile(path); + if (imageBitmap == null) { + Log.e(TAG, "Couldn't open " + path); return null; } + int width = imageBitmap.getWidth(); + int height = imageBitmap.getHeight(); + int[] pixels = new int[width * height]; + imageBitmap.getPixels(pixels, 0, width, 0, 0, width, height); + + RGBLuminanceSource source = new RGBLuminanceSource(width, height, pixels); + BenchmarkItem item = new BenchmarkItem(path, RUNS); for (int x = 0; x < RUNS; x++) { boolean success; diff --git a/androidtest/src/com/google/zxing/client/androidtest/RGBLuminanceSource.java b/core/src/com/google/zxing/RGBLuminanceSource.java similarity index 67% rename from androidtest/src/com/google/zxing/client/androidtest/RGBLuminanceSource.java rename to core/src/com/google/zxing/RGBLuminanceSource.java index 5adaff87a..b6899e5b9 100644 --- a/androidtest/src/com/google/zxing/client/androidtest/RGBLuminanceSource.java +++ b/core/src/com/google/zxing/RGBLuminanceSource.java @@ -14,18 +14,11 @@ * limitations under the License. */ -package com.google.zxing.client.androidtest; - -import com.google.zxing.LuminanceSource; - -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; - -import java.io.FileNotFoundException; +package com.google.zxing; /** * This class is used to help decode images from files which arrive as RGB data from - * Android bitmaps. It does not support cropping or rotation. + * an ARGB pixel array. It does not support cropping or rotation. * * @author dswitkin@google.com (Daniel Switkin) */ @@ -33,18 +26,8 @@ public final class RGBLuminanceSource extends LuminanceSource { private final byte[] luminances; - public RGBLuminanceSource(String path) throws FileNotFoundException { - this(loadBitmap(path)); - } - - public RGBLuminanceSource(Bitmap bitmap) { - super(bitmap.getWidth(), bitmap.getHeight()); - - int width = bitmap.getWidth(); - int height = bitmap.getHeight(); - int[] pixels = new int[width * height]; - bitmap.getPixels(pixels, 0, width, 0, 0, width, height); - + public RGBLuminanceSource(int width, int height, int[] pixels) { + super(width, height); // In order to measure pure decoding speed, we convert the entire image to a greyscale array // up front, which is the same as the Y channel of the YUVLuminanceSource in the real app. luminances = new byte[width * height]; @@ -80,19 +63,13 @@ public final class RGBLuminanceSource extends LuminanceSource { return row; } - // Since this class does not support cropping, the underlying byte array already contains - // exactly what the caller is asking for, so give it to them without a copy. + /** + * Since this class does not support cropping, the underlying byte array already contains + * exactly what the caller is asking for, so give it to them without a copy. + */ @Override public byte[] getMatrix() { return luminances; } - private static Bitmap loadBitmap(String path) throws FileNotFoundException { - Bitmap bitmap = BitmapFactory.decodeFile(path); - if (bitmap == null) { - throw new FileNotFoundException("Couldn't open " + path); - } - return bitmap; - } - }