Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从RGB数据的int数组创建PNG文件_Java_Image - Fatal编程技术网

Java 从RGB数据的int数组创建PNG文件

Java 从RGB数据的int数组创建PNG文件,java,image,Java,Image,我从png图像中获取int数组,如何将其转换为bufferdimage或创建新的png文件 int[] pixel = new int[w1*h1]; int i = 0; for (int xx = 0; xx < h1; xx++) { for (int yy = 0; yy < w1; yy++) { pixel[i] = img.getRGB(yy, xx);

我从png图像中获取int数组,如何将其转换为bufferdimage或创建新的png文件

int[] pixel = new int[w1*h1];
        int i = 0;
        for (int xx = 0; xx < h1; xx++) {
            for (int yy = 0; yy < w1; yy++) {
                        pixel[i] = img.getRGB(yy, xx);
                        i++;
                }
         }
int[]像素=新的int[w1*h1];
int i=0;
对于(int xx=0;xx
试试这个类,它可以获取表示像素数据的字节数组来构建图像对象,然后以特定格式将其写出

try {
    BufferedImage bufferedImage = ImageIO.read(new ByteArrayInputStream(yourBytes));
    ImageIO.write(bufferedImage, "png", new File("out.png"));
} catch (IOException e) {
    e.printStackTrace();
}

您可以手动重建图像,但是这是一个非常昂贵的操作

BufferedImage image = new BufferedImage(64, 64, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();

for(int i = 0; i < pixels.size(); i++)
{
    g.setColor(new java.awt.Color(pixels.get(i).getRed(), pixels.get(i).getGreen(), pixels.get(i).getBlue()));
    g.fillRect(pixels.get(i).getxPos(), pixels.get(i).getyPos(), 1, 1);
}

try 
{
    ImageIO.write(image, "PNG", new File("imageName.png"))
} 

catch(IOException error) 
{
    error.printStackTrace();
}
BufferedImage image=新的BufferedImage(64,64,BufferedImage.TYPE_INT_RGB);
Graphics g=image.getGraphics();
对于(int i=0;i

我把你的图像数组格式化成一个对象,这是我个人的喜好(当然你也可以用这个模型给我们一个int数组)。请记住,您也可以随时将alpha添加到其中。

如果您有一个压缩RGB值的整数数组,这是将其保存到文件的java代码:

int width = 100;
int height = 100;
int[] rgbs = buildRaster(width, height);

DataBuffer rgbData = new DataBufferInt(rgbs, rgbs.length);

WritableRaster raster = Raster.createPackedRaster(rgbData, width, height, width,
    new int[]{0xff0000, 0xff00, 0xff},
    null);

ColorModel colorModel = new DirectColorModel(24, 0xff0000, 0xff00, 0xff);

BufferedImage img = new BufferedImage(colorModel, raster, false, null);

String fname = "/tmp/whatI.png";
ImageIO.write(img, "png", new File(fname));
System.out.println("wrote to "+fname);

数组
0xff0000、0xff00、0xff
的原因是RGB字节在最低有效字节中用蓝色填充。如果将int打包为不同的,请更改该数组。

线程“main”java.lang.IllegalArgumentException中I have int array not byteException的可能重复:image==null!在javax.imageio.ImageTypeSpecifier.CreateFromRenderImage(未知源)在javax.imageio.imageio.getWriter(未知源)在javax.imageio.imageio.write(未知源)为什么需要int数组?RGB值从0到255。因为我的png图像的2个第一个像素int@ELMEKKIAnouar我不明白这与为什么需要将RGB值存储为int有什么关系。