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?
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).
BufferedImage buffer = ImageIO. read(new File(file)); to Image i.e in the format something like : Image image = ImageIO.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With