1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
public class AfficheBmp extends JComponent {
static BufferedImage afpImage;
static JFrame frame;
static int byteArrayToInt (byte[] tabByte, int borneDebut, int nombreByte) {
//converti un ensemble de byte consécutif dans un tableau vers un int
System.out.println("nombreByte : "+nombreByte);
System.out.println("borneDebut : "+borneDebut);
System.out.println("tabByte lenght : "+tabByte.length);
ByteBuffer byteBuffer = ByteBuffer.wrap(tabByte,borneDebut,nombreByte);
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
System.out.println("Bytes buffer lenght : "+byteBuffer);
int intRetour = byteBuffer.getInt();
return (intRetour);
}
public static void main(String[] args) {
// Stream to read file
AfficheBmp toto = new AfficheBmp ();
FileInputStream fin;
byte[] tabBmp;
try
{
// Open an input stream
fin = new FileInputStream ("output2.bmp");
tabBmp = new byte [fin.available()];
fin.read(tabBmp);
// Close our input stream
fin.close();
if (tabBmp.length >0 ) {
int intLargeur = byteArrayToInt(tabBmp,18,4);
int intHauteur = byteArrayToInt(tabBmp,22,4);
int intOffsetPixel = byteArrayToInt(tabBmp,10,4);
System.out.println("largeur : "+intLargeur);
System.out.println("Hauteur : "+intHauteur);
System.out.println("Offset : "+intOffsetPixel);
ColorModel monoChromeModel = new IndexColorModel(1,2,
new byte[] {(byte)0, (byte)255},
new byte[] {(byte)0, (byte)255},
new byte[] {(byte)0, (byte)255});
ByteBuffer BufferTabPixel = ByteBuffer.allocate (tabBmp.length-intOffsetPixel);
byte[] tabPixel = BufferTabPixel.array();
DataBuffer db = new DataBufferByte(tabPixel,tabPixel.length);
WritableRaster wr = Raster.createPackedRaster(db,intLargeur,intHauteur,1,null);
afpImage = new BufferedImage (monoChromeModel,wr,false,null);
Image monImage = Toolkit.getDefaultToolkit().createImage(afpImage.getSource());
frame = new JFrame ("Affiche BMP");
frame.setVisible(true);
frame.setSize(300,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImageIcon tempIcon = new ImageIcon (monImage);
JLabel labelImage = new JLabel (tempIcon);
//pane.add(monImage);
frame.getContentPane().add (new JScrollPane(labelImage));
}
}
// Catches any error conditions
catch (IOException e)
{
System.err.println ("Unable to read from file");
System.exit(-1);
}
}
} |
Partager