0

I was just wondering what the best way to read pixels from a BufferedImage. I want the pixels contained in a int[].

Here is my code right now:

try {
        BufferedImage img = ImageIO.read(new File("C:\\Users\\Hardish\\Pictures\\test.png"));
        BufferedImage intimg = Util.convertBufferedImage(img, BufferedImage.TYPE_INT_RGB);
        int[] pixels = ((DataBufferInt)intimg.getRaster().getDataBuffer()).getData();
        int co = Util.randomColor(255).getRGB();
        if(co < 0)
            co = -co;
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < pixels.length; i++){
            Pixel px = new Pixel(Util.convertPixel(i, img.getWidth()), pixels[i]);
            sb.append(px.toCompactString(true));
            pixels[i] += co;
        }
        ImageIO.write(intimg, "jpg", new FileOutputStream("C:\\Users\\Hardish\\Pictures\\test.jpg"));
        SaveHandler.saveCompressed(sb.toString(), "C:\\Users\\Hardish\\Pictures\\test.dat");
        SaveHandler.saveString(sb.toString(), "C:\\Users\\Hardish\\Pictures\\test.sdat");
        String pixDat = (String)(SaveHandler.readCompressed("C:\\Users\\Hardish\\Pictures\\test.dat"));
        String[] pixs = pixDat.split(Pixel.PIXEL_SEPARATOR);
        int[] pixelsRead = new int[pixs.length];
        for(int i = 0; i < pixelsRead.length; i++){
            pixelsRead[i] = Pixel.parseCompactPixel(pixs[i], true).val;
        }
        BufferedImage img2 = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB);
        int[] pixelsW = ((DataBufferInt)img2.getRaster().getDataBuffer()).getData();
        for(int i = 0; i < pixelsW.length; i++){
            pixelsW[i] = pixelsRead[i];
        }
        ImageIO.write(img2, "jpg", new FileOutputStream("C:\\Users\\Hardish\\Pictures\\testRead.jpg"));
    } catch (IOException e) {
        e.printStackTrace();
    }

Here is the original: enter image description here

Here is the change the pixels output: enter image description here

Here is the read from file output: enter image description here

Cœur
  • 37,241
  • 25
  • 195
  • 267
APerson
  • 702
  • 6
  • 10
  • Did you have a specific problem with the way you're doing it now? – Dark Falcon Oct 11 '13 at 19:31
  • If you look at the image 1 and 3 they aren't the exact same. I want it to be the exact same down to the pixel. – APerson Oct 11 '13 at 19:36
  • 3
    You will never get exact pixels when using JPEG. It is a lossy image format. If you want an exact match, write out a lossless format, such as PNG. – Dark Falcon Oct 11 '13 at 19:38
  • 1
    It's better to read data buffered(read line by line), and process the big files in a parallel way, maybe [this example](http://arashmd.blogspot.com/2013/07/java-thread-example.html#rccig) may help. –  Oct 11 '13 at 19:39
  • Ok, i'll try that. The reason I used it is because it is more compressed than png. – APerson Oct 11 '13 at 19:39
  • Ok, that seemed to work for me. – APerson Oct 11 '13 at 19:41
  • possible duplicate of [Java - get pixel array from image](http://stackoverflow.com/questions/6524196/java-get-pixel-array-from-image) – karlphillip Dec 24 '13 at 16:45
  • You can [minimize the loss with JPEG by calling `compress()` on a `Bitmap` and pass 100 in the 2nd parameter](http://stackoverflow.com/a/11828196/176769), but even then it will have some some degree of loss. – karlphillip Dec 24 '13 at 19:55

0 Answers0