Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to flip BufferedImage in java

I get RGB24 byte array and want to show it in Java.

public void getByteArray(byte byteArray[]){     
        int count1 = 0;
        byte temp1 = 0;

        for (int i = 0; i < 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();

However,the effect is not conform to my requirement and it is strange. enter image description here

How can I flip the BufferedImage to the correct direction like this? enter image description here

like image 913
Eugene Avatar asked May 04 '14 15:05

Eugene


People also ask

What does BufferedImage mean?

A BufferedImage is comprised of a ColorModel and a Raster of image data. The number and types of bands in the SampleModel of the Raster must match the number and types required by the ColorModel to represent its color and alpha components. All BufferedImage objects have an upper left corner coordinate of (0, 0).

How do I change an image to BufferedImage?

BufferedImage buffer = ImageIO. read(new File(file)); to Image i.e in the format something like : Image image = ImageIO.

What is the difference between image and BufferedImage in Java?

Image is an abstract class. You can't instantiate Image directly. BufferedImage is a descendant, and you can instantiate that one. So, if you understand abstract classes and inheritance, you'll understand when to use each.


1 Answers

you only have to draw the bufferedImage in negative width or negative height in drawImage method thats all

//flip horizontally
g.drawImage(bufferedImage , x,y,-width,height,null);

//flip vertically
g.drawImage(bufferedImage , x,y,width,-height,null);
like image 55
Jesse Avatar answered Sep 20 '22 15:09

Jesse