alz

Java Image Processing

alz
Jan 19th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1. String process(File file, int width, int height) throws IOException {
  2.    BufferedImage img = ImageIO.read(file);
  3.    int px = img.getWidth()/width;
  4.    int py = img.getHeight()/height;
  5.    
  6.    StringBuilder bits = new StringBuilder();
  7.    for(int r = 0; r < height; r++)
  8.       for(int c = 0; c < width; c++)
  9.          bits.append(bw(img.getRGB(c*px + px/2, r*py + py/2)));
  10.    
  11.    char[] bytes = new char[bits.length()/8];
  12.    for(int i = 0; i < bytes.length; i++)
  13.       bytes[i] = (char)Integer.parseInt(bits.substring(8*i,8*i+8),2);
  14.    
  15.    return String.copyValueOf(bytes);
  16. }
  17.  
  18. int bw(int rgb){
  19.    return (rgb >>> 16 & 255) + (rgb >>> 8 & 255) + (rgb & 255) > 3*255/2 ? 0 : 1;
  20. }
Advertisement
Add Comment
Please, Sign In to add comment