Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- String process(File file, int width, int height) throws IOException {
- BufferedImage img = ImageIO.read(file);
- int px = img.getWidth()/width;
- int py = img.getHeight()/height;
- StringBuilder bits = new StringBuilder();
- for(int r = 0; r < height; r++)
- for(int c = 0; c < width; c++)
- bits.append(bw(img.getRGB(c*px + px/2, r*py + py/2)));
- char[] bytes = new char[bits.length()/8];
- for(int i = 0; i < bytes.length; i++)
- bytes[i] = (char)Integer.parseInt(bits.substring(8*i,8*i+8),2);
- return String.copyValueOf(bytes);
- }
- int bw(int rgb){
- return (rgb >>> 16 & 255) + (rgb >>> 8 & 255) + (rgb & 255) > 3*255/2 ? 0 : 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment