title: How to flip BufferedImage in java
body: <p>I get RGB24 byte array and want to show it in Java.</p>
  
  <pre><code>public void getByteArray(byte byteArray[]){     
          int count1 = 0;
          byte temp1 = 0;
  
          for (int i = 0; i &lt; byteArray.length; i++) {       //The order of RGB24 is red,green and blue.Change the
              //order to blue,green and red so that java can use TYPE_3BYTE_BGR to recognize it
              if (count1 == 0) {
                  temp1 = byteArray[i];  
                  count1++;
              } else if(count1 == 1) {
                  //do nothing
                  count1++;
              } else if(count1 == 2) {
                  byteArray[i - 2] = byteArray[i];
                  byteArray[i] = temp1;
                  count1=0;
              }
          }
          image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
          image.getWritableTile(0, 0).setDataElements(0, 0, width, height, byteArray);
  
          mainPanel.repaint();
  </code></pre>
  
  <p>However,the effect is not conform to my requirement and it is strange.
  <img src="https://i.stack.imgur.com/ep1EU.jpg" alt="enter image description here"></p>
  
  <p>How can I flip the BufferedImage to the correct direction like this?
  <img src="https://i.stack.imgur.com/YZ0N8.jpg" alt="enter image description here"></p>