在略微類似的情況我以前Raster
與BufferedImage
。看到這個簡單的例子與灰階8位:
public static void main(String[] args) {
JFrame f = new JFrame();
f.setSize(600, 400);
f.getContentPane().setLayout(new BorderLayout());
BufferedImage bi = new BufferedImage(200, 100, BufferedImage.TYPE_BYTE_GRAY);
WritableRaster wr = bi.getRaster();
for (int col=0; col<bi.getWidth(); col++) {
for (int row=0; row<bi.getHeight(); row++) {
wr.setPixel(col, row, new int[] {(col+row)&0xff});
}
}
JLabel l = new JLabel(new ImageIcon(bi));
f.getContentPane().add(l, BorderLayout.CENTER);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setVisible(true);
}
我不記得了,BufferedImage
是否支持每像素1位。您可能想要查看一次設置多個像素的setPixels
變體。
謝謝,它的工作原理! 我用這個: '的BufferedImage圖像=新的BufferedImage(picWidth,picHeight,BufferedImage.TYPE_BYTE_GRAY); 。 image.getRaster()setDataElements(0,0,picWidth,picHeight,textureArray);' textureArray是含有灰度值的字節數組。我認爲你也可以使用它來代替WritableRaster! – tschakkkiiiii