- MonochromeBitmapSource src = new BufferedImageMonochromeBitmapSource(image);
- int width = src.getWidth();
- int height = src.getHeight();
-
- BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY);
- for (int i = 0; i < width; i++) {
- for (int j = 0; j < height; j++) {
- result.setRGB(i, j, src.isBlack(i,j) ? BLACK : WHITE);
+ LuminanceSource source = new BufferedImageLuminanceSource(image);
+ BinaryBitmap bitmap = new BinaryBitmap(new GlobalHistogramBinarizer(source));
+ int width = bitmap.getWidth();
+ int height = bitmap.getHeight();
+
+ BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
+ BitArray array = new BitArray(width);
+
+ if (rowSampling) {
+ for (int y = 0; y < height; y++) {
+ try {
+ array = bitmap.getBlackRow(y, array);
+ } catch (ReaderException e) {
+ // Draw rows with insufficient dynamic range in red
+ for (int x = 0; x < width; x++) {
+ result.setRGB(x, y, RED);
+ }
+ continue;
+ }
+
+ for (int x = 0; x < width; x++) {
+ result.setRGB(x, y, array.get(x) ? BLACK : WHITE);
+ }
+ }
+ } else {
+ try {
+ BitMatrix matrix = bitmap.getBlackMatrix();
+ for (int y = 0; y < height; y++) {
+ for (int x = 0; x < width; x++) {
+ result.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
+ }
+ }
+ } catch (ReaderException e) {
+