简体   繁体   English

对两个图像进行异或后,从getRGB和setRGB获得不同的值

[英]Getting different values from getRGB and setRGB after XORing two images

I want to XOR two images pixel by pixel. 我想对两个图像逐像素进行XOR。 I am using the following code. 我正在使用以下代码。

 import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.*;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.util.Scanner;
import java.security.*;
import java.awt.AlphaComposite;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

public class sefi 
{
    public static void main(String[] args) throws Exception 
    {   
        encdec ed = new encdec();

        String plainimagename = args[0];
        String keyfilename = args[1];
        String choice = args[2];

        BufferedImage bikey = ImageIO.read(new File(keyfilename));
        BufferedImage biplain = ImageIO.read(new File(plainimagename));

        BufferedImage resizedImage = new BufferedImage(biplain.getWidth(), biplain.getHeight(), BufferedImage.TYPE_INT_RGB);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(bikey, 0, 0, biplain.getWidth(), biplain.getHeight(), null);
    g.dispose();
        ImageIO.write(resizedImage, "jpg", new File("resizeimage.jpg"));

        if(choice.equals("enc"))
        {
            ed.encrypt(resizedImage,biplain);   
        }
        else if(choice.equals("dec"))
        {
            ed.decrypt(resizedImage,biplain);
        }
    }     
}

class encdec
{
    public void encrypt(BufferedImage bikey, BufferedImage biplain) throws Exception
    {
        BufferedImage xoredimage = xor(bikey, biplain);
        File xored = new File("xored.jpg");
        ImageIO.write(xoredimage, "JPEG", xored);
    }

    public void decrypt(BufferedImage bikey, BufferedImage biplain) throws Exception
    {
        BufferedImage xoredimage = xor(bikey, biplain);
        File xored = new File("newplain.jpg");
        ImageIO.write(xoredimage, "JPEG", xored);
    }
    private BufferedImage xor(BufferedImage image1, BufferedImage image2) throws Exception
    {
        BufferedImage outputimage = new BufferedImage(image1.getWidth(), image1.getHeight(), BufferedImage.TYPE_INT_RGB);
        for (int y = 0; y < image1.getHeight(); y++) 
        {
            for (int x = 0; x < image1.getWidth(); x++) 
            {
                outputimage.setRGB(x,y,((image1.getRGB(x, y))^(image2.getRGB(x, y))));
                System.out.println("one:" + image1.getRGB(x, y) + "\ttwo:" + image2.getRGB(x, y) + "\txor:" + ((image1.getRGB(x, y))^(image2.getRGB(x, y))));
                System.out.println("one:" + Integer.toBinaryString(image1.getRGB(x, y)) + "\ttwo:" + Integer.toBinaryString(image2.getRGB(x, y)) + "\txor:" + Integer.toBinaryString((image1.getRGB(x, y)^image2.getRGB(x, y))));
            }
        }

        return outputimage;
    }
}

First time I run this code where image1 is a 4-pixel colored image and image2 is a 4-pixel white image, I get the output as: input: #java sefi white.jpg key.jpg enc 第一次运行此代码,其中image1是4像素彩色图像,而image2是4像素白色图像,我得到的输出为:输入: #java sefi white.jpg key.jpg enc

one:-201053 two:-1  xor:201052
one:11111111111111001110111010100011    two:11111111111111111111111111111111    xor:110001000101011100
one:-265579 two:-1  xor:265578
one:11111111111110111111001010010101    two:11111111111111111111111111111111    xor:1000000110101101010
one:-664247 two:-1  xor:664246
one:11111111111101011101110101001001    two:11111111111111111111111111111111    xor:10100010001010110110
one:-925624 two:-1  xor:925623
one:11111111111100011110000001001000    two:11111111111111111111111111111111    xor:11100001111110110111

Next time I run with image1 as the xored image file and image 2 as the 4-pixel colored file, which should give me the original white image as output. 下次,我将image1作为异化图像文件,将image 2作为4像素彩色文件运行,这将为我提供原始的白色图像作为输出。 But I get this as output instead: Input: #java sefi xored.jpg key.jpg dec 但是我得到的是输出:输入: #java sefi xored.jpg key.jpg dec

one:-1  two:-16773753   xor:16773752
one:11111111111111111111111111111111    two:11111111000000000000110110000111    xor:111111111111001001111000
one:-1  two:-16773753   xor:16773752
one:11111111111111111111111111111111    two:11111111000000000000110110000111    xor:111111111111001001111000
one:-1  two:-15786601   xor:15786600
one:11111111111111111111111111111111    two:11111111000011110001110110010111    xor:111100001110001001101000
one:-1  two:-15786601   xor:15786600
one:11111111111111111111111111111111    two:11111111000011110001110110010111    xor:111100001110001001101000

If you look at the output we can see that the colors of the xored image from first time has changed. 如果您查看输出,则可以看到第一次的异化图像的颜色已更改。 I am not able to understand why I am getting different color value for the same image file. 我无法理解为什么我为同一图像文件获得不同的颜色值。

There's something wrong with your image selection. 您的图片选择有问题。 If you were selecting the RGB for the generated image then the first pixel would be 110001000101011100 and not 11111111000000000000110110000111. 如果您为生成的图像选择RGB,则第一个像素将是110001000101011100,而不是11111111000000000000110110000111。

So my advice is for you to check if you are using the correct images on the second step. 因此,我的建议是让您检查第二步中使用的图像是否正确。

Your code looks ok, although you'd have to paste the whole code for me to have a better idea. 您的代码看起来还不错,尽管您必须粘贴整个代码才能让我有一个更好的主意。

Found the Answer. 找到答案。 It is because I am using JPEG images. 这是因为我正在使用JPEG图像。 JPEG compresses raw image, but when it decompress it does not guarantee to produce the exact same colors as it was before compression. JPEG压缩原始图像,但是在解压缩时,不能保证产生与压缩前完全相同的颜色。 Thus the different color values before and after. 因此,前后的颜色值不同。 When I used bmp images, I got the same colors before and after. 使用bmp图像时,前后颜色相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM