Created
April 17, 2017 09:05
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.zxing.Binarizer; | |
import com.google.zxing.LuminanceSource; | |
import com.google.zxing.RGBLuminanceSource; | |
import com.google.zxing.BinaryBitmap; | |
import com.google.zxing.common.BitMatrix; | |
import com.google.zxing.common.HybridBinarizer; | |
import java.io.InputStream; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import javax.imageio.ImageIO; | |
import java.awt.image.WritableRaster; | |
import java.awt.image.DataBufferByte; | |
import java.io.ByteArrayInputStream; | |
public class Test { | |
public static void main( String[] args ) { | |
File imgPath = new File( args[0] ); | |
try { | |
BufferedImage bufferedImage = ImageIO.read(imgPath); | |
byte[] pixels = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData(); | |
LuminanceSource source = new RGBLuminanceSource(bufferedImage.getWidth(), bufferedImage.getHeight(), getPixelArray(bufferedImage)); | |
BinaryBitmap bitmap = new BinaryBitmap( new HybridBinarizer( source )); | |
BitMatrix bitMatrix = bitmap.getBlackMatrix(); | |
for (int row=0; row<bitMatrix.getHeight(); row++) { | |
for (int col=0; col<bitMatrix.getWidth(); col++) { | |
for (int channels=0; channels<3; channels++) { | |
int idx = (col + bitMatrix.getWidth() * row)*3 + channels; | |
if (bitMatrix.get(col, row)) { | |
pixels[idx] = 0; | |
} else { | |
pixels[idx] = -1; | |
} | |
} | |
} | |
} | |
ImageIO.write(bufferedImage, "jpg", new File("binary_output.jpg")); | |
} catch (Throwable t) { | |
System.out.println(t.getMessage()); | |
} | |
} | |
private static int[] getPixelArray(BufferedImage image) { | |
final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); | |
final int width = image.getWidth(); | |
final int height = image.getHeight(); | |
final boolean hasAlphaChannel = image.getAlphaRaster() != null; | |
int[] result = new int[height*width]; | |
if (hasAlphaChannel) { | |
final int pixelLength = 4; | |
for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) { | |
int idx = col + row*width; | |
int argb = 0; | |
argb += (((int) pixels[pixel] & 0xff) << 24); // alpha | |
argb += ((int) pixels[pixel + 1] & 0xff); // blue | |
argb += (((int) pixels[pixel + 2] & 0xff) << 8); // green | |
argb += (((int) pixels[pixel + 3] & 0xff) << 16); // red | |
result[idx] = argb; | |
col++; | |
if (col == width) { | |
col = 0; | |
row++; | |
} | |
} | |
} else { | |
final int pixelLength = 3; | |
for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) { | |
int argb = 0; | |
int idx = col + row*width; | |
argb += -16777216; // 255 alpha | |
argb += ((int) pixels[pixel] & 0xff); // blue | |
argb += (((int) pixels[pixel + 1] & 0xff) << 8); // green | |
argb += (((int) pixels[pixel + 2] & 0xff) << 16); // red | |
result[idx] = argb; | |
col++; | |
if (col == width) { | |
col = 0; | |
row++; | |
} | |
} | |
} | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment