Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>getRGB() returns the int representations of a pixel, including the alpha channel. Since a int is 4-byte (32 bits), this int will be like this:</p> <pre><code>01010101 11111111 10101010 11111111 Alpha Red Green Blue </code></pre> <p>When you add the three values, this is using the alpha channel too, so I guess this is making you get strange results. Also, adding ints this way can cause "overflow" in one of the channels. For example, if one pixel has a blue value of 255 and another one has a value of 2, the sum will have a value 1 for green and value 1 for blue.</p> <p>To extract each color channel from the int you can do this. </p> <pre><code>red = (rgb &gt;&gt; 16) &amp; 0xFF; green = (rgb &gt;&gt;8 ) &amp; 0xFF; blue = (rgb) &amp; 0xFF; </code></pre> <p>(This is what Color class does internally inside getRed(), getBlue() and getGreen() <a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/awt/Color.java#Color.getRed%28%29" rel="nofollow">http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/awt/Color.java#Color.getRed%28%29</a>)</p> <p>Then, combine the colors like this:</p> <pre><code>combinedImage.setRGB(x, y, new Color( ((image1.getRGB(x, y) &gt;&gt; 16 &amp; 0xFF) + (image1.getRGB(x, y) &gt;&gt; 16 &amp; 0xFF) + (image1.getRGB(x, y) &gt;&gt; 16 &amp; 0xFF))/3, ((image1.getRGB(x, y) &gt;&gt; 8 &amp; 0xFF) + (image1.getRGB(x, y) &gt;&gt; 8 &amp; 0xFF) + (image1.getRGB(x, y) &gt;&gt; 8 &amp; 0xFF))/3, ((image1.getRGB(x, y) &amp; 0xFF) + (image1.getRGB(x, y) &amp; 0xFF) + (image1.getRGB(x, y) &amp; 0xFF))/3). getRGB()); </code></pre> <p>Or use <code>new Color(image1.getRGB(x, y)).getRed()</code> each time for each image.</p> <pre><code>combinedImage.setRGB(x, y, new Color( (new Color(image1.getRGB(x, y)).getRed() + new Color(image2.getRGB(x, y)).getRed() + new Color(image3.getRGB(x, y)).getRed())/3, (new Color(image1.getRGB(x, y)).getGreen() + new Color(image2.getRGB(x, y)).getGreen() + new Color(image3.getRGB(x, y)).getGreen())/3, (new Color(image1.getRGB(x, y)).getBlue() + new Color(image2.getRGB(x, y)).getBlue() + new Color(image3.getRGB(x, y)).getBlue())/3). getRGB()); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload