I wrote an image with this code:
BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
index = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int r = ciphered[index++];
int g = ciphered[index++];
int b = ciphered[index++];
Color newColor = new Color(r, g, b);
newImage.setRGB(j, i, newColor.getRGB());
}
}
File ouptut = new File("/Users/newbie/Desktop/encrypted.jpg");
ImageIO.write(newImage, "jpg", ouptut);
When I try to read the image ("encrypted.jpg") I get different RGB values. I read the image with the following code:
File input = new File("/Users/newbie/Desktop/encrypted.jpg");
BufferedImage image = new BufferedImage(512, 512, BufferedImage.TYPE_INT_RGB);
image = ImageIO.read(input);
int[] t = new int[width * height * 3];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
Color c = new Color(image.getRGB(j, i));
int r = c.getRed();
int g = c.getGreen();
int b = c.getBlue();
t[index++] = r;
t[index++] = g;
t[index++] = b;
}
}
I don't understand what I'm doing wrong. I just get different rgb values from the ones I've inserted.
ZipOutputStreamto a file, but you get the basic idea. You should also try usingpngofjpg, as it uses a lossless compression and would safe you a considerable amount of time and effort