我通过setRGB设置像素值,但是保存图像后,getRGB获得不同的值,如何获得相同的值?

时间:2019-01-21 23:32:10

标签: java image encryption rgb rgba

保存图像后从getRGB获取不同的像素值。

代码如下。

public  BufferedImage formImage(List<Integer>  l, int w, int len)
{
    // l = list of zeros and ones

    BufferedImage dst = new BufferedImage(w, len, 
    BufferedImage.TYPE_4BYTE_ABGR);
    dst.getGraphics().drawImage(img, 0, 0, null);
    int p = 0;
    for (int i = 0, k = 0; i < l.size(); ++k)
    {
        p = 0;
        for (int j = 31; j > -1; --j, ++i)
        {
            p |= (l.get(i) << j); 
        }
        p_out.add(p);
        Color col = new Color(p, true);
        dst.setRGB(k % w, k / w, (new Color(col.getRed(), col.getGreen(), 
        col.getBlue(), col.getAlpha())).getRGB());
        p = dst.getRGB(k % w, k / w);
        //System.out.println("index = " + k + " val = " + p);
    }
    return dst;
}
// in main

BufferedImage test = b.formImage(L2, width, height);

try
{ 

    f1 = new 
    File("E:\\JavaProjects\\BinaryImage\\src\\binaryimage\\out1.bmp");
    if (ImageIO.write(test, "BMP", f1))
    {
        img2 = ImageIO.read(f);
        for (y = 0; y < height; ++y)
        {
            for (x = 0; x < width; ++x)
            {
                L5.add(img2.getRGB(x, y));
            }
        }
        for (int i = 0; i < width * height; ++i)
        {
            if (L5.get(i) != p_out.get(i))
                System.out.println(L5.get(i) + "!=" + p_out.get(i) + " 
            index = " + i);
        }
    }
}
catch(IOException e) 
{ 
    System.out.println(e); 
}

-2631722!=-5329294索引= 0 -3092231!=-2631681索引= 1 -2697567!=-5460897索引= 2 -3158018!=-2697584索引= 3 -3421276!=-5394976索引= 4 -3224018 !=-3421372索引= 5 -4408203!=-5131908索引= 6 -3223980!=-4408081索引= 7 -5592397!=-4802928索引= 8 -3289830!=-5592476索引= 9 -5921283!=-4737165索引= 10 -3421185!=-5921335索引= 11 -5658265!=-4605572索引= 12 -3421223!=-5658251索引= 13 -5394994!=-4473949索引= 14

我想读取与插入图像相同的像素值。

0 个答案:

没有答案