Advertisement
mobiusinversion

ImageIO Pixel Example

Dec 17th, 2014
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.49 KB | None | 0 0
  1.  
  2. import org.apache.commons.lang3.StringUtils;
  3.  
  4. import javax.imageio.ImageIO;
  5. import java.awt.image.BufferedImage;
  6. import java.awt.image.DataBufferByte;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. /**
  12.  * Created by david on 12/17/14
  13.  */
  14. public class Main
  15. {
  16.     public static void main(String[] args) throws IOException
  17.     {
  18.         BufferedImage bufferedImage = ImageIO.read(Main.class.getResource("/annalisa-licciardi.png"));
  19.         int[][][] result = getPixels(bufferedImage);
  20.         for(int i = 0; i < result.length; i++)
  21.         {
  22.             for(int j = 0; j < result[i].length; j++)
  23.             {
  24.                 System.out.println(i + " " + j + " " + intArrayToString(result[i][j]));
  25.             }
  26.         }
  27.     }
  28.  
  29.     private static String intArrayToString(int[] integers)
  30.     {
  31.         List<String> output = new ArrayList<>();
  32.         for(int i = 0; i < integers.length; i ++)
  33.             output.add(Integer.toString(integers[i]));
  34.         return StringUtils.join(output, ",");
  35.     }
  36.  
  37.  
  38.     private static int[][][] getPixels(BufferedImage image) {
  39.  
  40.         final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
  41.         final int width = image.getWidth();
  42.         final int height = image.getHeight();
  43.         final boolean hasAlphaChannel = image.getAlphaRaster() != null;
  44.  
  45.         int[][][] result = new int[height][width][4];
  46.  
  47.         if (hasAlphaChannel) {
  48.             final int pixelLength = 4;
  49.             for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
  50.                 int[] pixelData = new int[4];
  51.                 pixelData[0] = (int) pixels[pixel] & 0xff;
  52.                 pixelData[1] = (int) pixels[pixel + 1] & 0xff;
  53.                 pixelData[2] = (int) pixels[pixel + 2] & 0xff;
  54.                 pixelData[3] = (int) pixels[pixel + 3] & 0xff;
  55.                 result[row][col] = pixelData;
  56.  
  57.                 /*
  58.                 int argb = 0;
  59.                 argb += (((int) pixels[pixel] & 0xff) << 24); // alpha
  60.                 argb += ((int) pixels[pixel + 1] & 0xff); // blue
  61.                 argb += (((int) pixels[pixel + 2] & 0xff) << 8); // green
  62.                 argb += (((int) pixels[pixel + 3] & 0xff) << 16); // red
  63.                 result[row][col] = argb;
  64.                 col++;
  65.                 if (col == width) {
  66.                     col = 0;
  67.                     row++;
  68.                 }
  69.                 */
  70.             }
  71.         } else {
  72.             final int pixelLength = 3;
  73.             for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
  74.                 int[] pixelData = new int[4];
  75.                 pixelData[0] = 0;
  76.                 pixelData[1] = (int) pixels[pixel] & 0xff;
  77.                 pixelData[2] = (int) pixels[pixel + 1] & 0xff;
  78.                 pixelData[3] = (int) pixels[pixel + 2] & 0xff;
  79.                 result[row][col] = pixelData;
  80.  
  81.                 /*
  82.                 int argb = 0;
  83.                 argb += -16777216; // 255 alpha
  84.                 argb += ((int) pixels[pixel] & 0xff); // blue
  85.                 argb += (((int) pixels[pixel + 1] & 0xff) << 8); // green
  86.                 argb += (((int) pixels[pixel + 2] & 0xff) << 16); // red
  87.                 result[row][col] = argb;
  88.                 col++;
  89.                 if (col == width) {
  90.                     col = 0;
  91.                     row++;
  92.                 }
  93.                 */
  94.             }
  95.         }
  96.  
  97.         return result;
  98.     }
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement