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.
How can I flip the BufferedImage to the correct direction like this?
Maybe you can use AffineTransform.
you only have to draw the bufferedImage in negative width or negative height in drawImage method thats all
You could flip the image like this:
Here is the code to flip the image at any angle
If you are using paintComponent() method of swing.
With
Just flip the sx1 with sx2
TADA! Its done.
This could be good reference for: drawImage() method
There are 3 options: (EDIT ->: At least, there have been 3 options, until you edited the question <-)
The difference is shown in this image:
Based on the image that you posted, I assume that you want to flip the image vertically. This can be done pixel by pixel, or (when it should be done efficiently) with an
AffineTransformOp
or by directly painting the image using a transformedGraphics2D
.