Developpement/23DEV1.1/TPS2/TP01/FluxDOctet/Image/TestImage.java

71 lines
1.8 KiB
Java

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class TestImage extends JPanel
{
private static final int WIDTH = 768;
private static final int HEIGHT = 1024;
private static final String FILE_PATH = "image.bin";
private BufferedImage image;
public TestImage()
{
try
{
loadImage();
}
catch (IOException e)
{
e.printStackTrace();
}
}
private void loadImage() throws IOException
{
FileInputStream fileInputStream = new FileInputStream(FILE_PATH);
DataInputStream dataInputStream = new DataInputStream(fileInputStream);
byte[] imageData = new byte[WIDTH * HEIGHT * 3];
// Lecture des données de l'image
dataInputStream.readFully(imageData);
dataInputStream.close();
// Création d'une BufferedImage
image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
// Remplissage de l'image à partir des données lues
int index = 0; for (int y = 0; y < HEIGHT; y++)
{
for (int x = 0; x < WIDTH; x++)
{
int r = imageData[index++] & 0xFF;
// Rouge
int g = imageData[index++] & 0xFF;
// Vert
int b = imageData[index++] & 0xFF;
// Bleu
int rgb = (r << 16) | (g << 8) | b;
image.setRGB(x, y, rgb);
}
}
}
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(() -> {
JFrame frame = new JFrame("Image Display");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new ImageDisplay());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}