image.getData()返回栅格的 副本 。也许如果
image.setData(raster)在修改栅格后调用,您将看到结果。
同样,应该给setPixels一个足够大的数组,以填充栅格的所有波段(A,R,G,B)。在将像素大小增加为28 * 28 *4之前,我已经获得了数组索引超出范围的异常。
对于TYPE_INT_RGB,以下内容应产生白色图像:
public class ASD{ public static Image getImageFromArray(int[] pixels, int width, int height) { BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); WritableRaster raster = (WritableRaster) image.getData(); raster.setPixels(0, 0, width, height, pixels); image.setData(raster); return image; } public static void main(String[] args) throws IOException { Jframe jf = new Jframe(); JLabel jl = new JLabel(); //3 bands in TYPE_INT_RGB int NUM_BANDS = 3; int[] arrayimage = new int[28 * 28 * NUM_BANDS]; for (int i = 0; i < 28; i++) { for (int j = 0; j < 28; j++) { for (int band = 0; band < NUM_BANDS; band++) arrayimage[((i * 28) + j)*NUM_BANDS + band] = 255; } } ImageIcon ii = new ImageIcon(getImageFromArray(arrayimage, 28, 28)); jl.setIcon(ii); jf.add(jl); jf.pack(); jf.setVisible(true); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)