Reputation: 395
I have read an image..lets say image 'img', read its RGB values in three separate int matrices. I wrote the same image with a new name.. let's say image 'simg'. Read the RGB values of this 'simg' image in three new int matrices. When I compared the values of their Red matrices I got the different values.. For example see the following code:
// Reading two images and checking the differences
package Steg_garage;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Random;
import javax.imageio.ImageIO;
/**
*
* @author Rohit
*/
public class difference {
public static void main(String[] args)
{
BufferedImage img = null;
File f = new File("flower.jpg");
try {
img = ImageIO.read(f);
}
catch(Exception e)
{
e.printStackTrace();
}
int[] RGBarray = null;
int c = 0;
int r = 0;
int [][] alphaPixels = null;
int [][] redPixels = null;
int [][] greenPixels = null;
int [][] bluePixels =null;
c = img.getWidth();
r = img.getHeight();
RGBarray = img.getRGB(0,0,c,r,null,0,c);
alphaPixels = new int [r][c];
redPixels = new int [r][c];
greenPixels = new int [r][c];
bluePixels = new int [r][c];
int ii = 0;// to run inside seperating a loop
for(int row=0; row<r; row++)
{
for(int col=0; col<c; col++)
{
alphaPixels[row][col] = ((RGBarray[ii]>>24)&0xff);
redPixels[row][col] = ((RGBarray[ii]>>16)&0xff);
greenPixels[row][col] = ((RGBarray[ii]>>8)&0xff);
bluePixels[row][col] = (RGBarray[ii]&0xff);
ii++;
}
}
int code_length = 5;
int value = 32;
for(int row = r-1, col = 0;col < code_length ; col++ )
{
redPixels[row][col] = value++;
}
int rgba;
for(int row=0; row<r; row++)
{
for(int col=0; col<c; col++)
{
rgba = (alphaPixels[row][col] & 0xff) << 24 | (redPixels[row][col] & 0xff) << 16 | (greenPixels[row][col] & 0xff) << 8 | (bluePixels[row][col] & 0xff);
img.setRGB(col, row, rgba);
}
}
try{
ImageIO.write(img, "jpg", new File("change"+".jpg"));
}
catch(Exception e)
{
e.printStackTrace();
}
// Reading the written image/////////////
BufferedImage simg = null;
File sf = new File("change.jpg");
try {
simg = ImageIO.read(sf);
}
catch(Exception e)
{
e.printStackTrace();
}
int[] sRGBarray = null;
int sc = 0;
int sr = 0;
int [][] salphaPixels = null;
int [][] sredPixels = null;
int [][] sgreenPixels = null;
int [][] sbluePixels =null;
sc = simg.getWidth();
sr = simg.getHeight();
sRGBarray = simg.getRGB(0,0,sc,sr,null,0,sc);
salphaPixels = new int [sr][sc];
sredPixels = new int [sr][sc];
sgreenPixels = new int [sr][sc];
sbluePixels = new int [sr][sc];
int sii = 0;// to run inside seperating a loop
for(int row=0; row<sr; row++)
{
for(int col=0; col<sc; col++)
{
salphaPixels[row][col] = ((sRGBarray[sii]>>24)&0xff);
sredPixels[row][col] = ((sRGBarray[sii]>>16)&0xff);
sgreenPixels[row][col] = ((sRGBarray[sii]>>8)&0xff);
sbluePixels[row][col] = (sRGBarray[sii]&0xff);
sii++;
}
}
System.out.println(" The selected image height is " + img.getHeight()+ " and its width is " + img.getWidth());
System.out.println(" The steganographed image height is " + img.getHeight()+ " and its width is " + simg.getWidth());
int count = 0;
int tcount = 0;
for(int row=0; row<r; row++)
{
for(int col=0; col<c; col++)
{
tcount++;
if( redPixels[row][col] != sredPixels[row][col])
count++;
}
}
System.out.println(" The changed pixels are " + count);
System.out.println(" The total number of pixels are " + tcount);
}
}
I am expecting the value of the 'count' to be zero because both are the copies of the same image! But the ouput I got is " The changed pixels are 678783". How is this possible...?
Upvotes: 1
Views: 41
Reputation: 73558
Because JPEG is a lossy format. If you try it with a non-lossy format, such as PNG, there shouldn't be any differences.
Upvotes: 4