从int数组RGB数据创建PNG文件 [英] Creating PNG file from int Array of RGB data

查看:145
本文介绍了从int数组RGB数据创建PNG文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am getting the int array from png image how I will convert this to bufferdimage or creating new PNG file ?

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++;
                }
         }


推荐答案

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

You can rebuild the image manually, this is however a pretty expensive operation.

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();
}

我将你的图像数组格式化为一个对象,这是个人偏好tho(的当然,你也可以使用这个模型的int数组。请记住,您也可以随时添加alpha。

I formatted your image array into an object, this is personal preference tho (of course you could us an int array with this model as well). Keep in mind that you can always add the alpha to there as well.

这篇关于从int数组RGB数据创建PNG文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆