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 67
|
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.IndexColorModel;
import java.awt.image.Raster;
import java.awt.image.WritableRaster;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
public class tablepixel {
public static int byteArrayToInt(byte[] b, int offset, int nbyte) {
int value = 0;
for (int i = 0; i < 4; i++) {
int shift = (4 - 1 - i) *8;
value += (b[i + offset] & 0x000000FF) << shift;
}
return value;
}
public static int[] getPixelTable(String absolutePathFileName) {
try {
byte tabBmp[];
int rgbs[] = null;
// création d'un flux
FileInputStream fin = new FileInputStream(absolutePathFileName);
tabBmp = new byte[fin.available()];
fin.read(tabBmp);
if (tabBmp.length >0 ) {
int intLargeur = byteArrayToInt(tabBmp,18,4);
int intHauteur = byteArrayToInt(tabBmp,22,4);
int intOffsetPixel = byteArrayToInt(tabBmp,10,4);
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);
//creation du BufferedImage
BufferedImage afpImage = new BufferedImage(monoChromeModel,wr,false,null);
int w = afpImage.getWidth();
int h = afpImage.getHeight();
rgbs = new int[w*h]; /* on crée l'espace nécessaire */
//copie de chaque piskel du bufferedImage (afpImage) vers un tableau d'entiers (rgbs)
afpImage.getRGB(0,0,w,h,rgbs,0,w);
}
fin.close();
return rgbs;
} catch(IOException ie) {
System.out.println("Erreur: "+ie);
return null;
}
}
} |
Partager