-6

I was trying to convert a ".jpg" image to binary and then change its binary value to hide some data. But couldn't find anything. Any ideas anyone?

sogyals429
  • 357
  • 1
  • 2
  • 11
  • How do expect us to answer this? – f1sh Sep 11 '18 at 09:34
  • what do u mean? – sogyals429 Sep 11 '18 at 09:34
  • 1
    Heads up. Don't do this. Read the image with the image reader class/function of your language to load up the pixels and modify those. The binary data of the jpeg format in no way does it represent those pixel values and you'd corrupt the medium if you were to modify them directly. – Reti43 Sep 11 '18 at 09:42

1 Answers1

0

If I understand the question correctly, you want to get the single bytes of the jpg-file, which can be read with a DataInputStream:

File imageFile;
DataInputStream dis = new DataInputStream(new FileInputStream(imageFile));

int input = dis.read();

dis.close();

input then holds the first byte of the file, if you invoke read again (before dis.close()), you can read the subsequent bytes. Next, you would have to manipulate them and finally, you can write them to this or another file with a DataOutputStream that works just like the corresponding input stream. Just do NOT forget to close the streams after you are done reading or writing, so that system resources are freed and the files are closed. Otherwise the written data could be lost.

  • What i was trying to do is get all the pixels and store them in an array and then get pixels within a range and modify the binary values. – sogyals429 Sep 11 '18 at 09:55
  • In this case, read the file as an image with `ImageIO`, which returns a `BufferedImage` and use the `getRGB()` and `setRGB()` methods of this class to read and modify single pixels. When finished, you can write the `BufferedImage` with `ImageIO` again. –  Sep 11 '18 at 10:03
  • ya but i need to first store all the pixel values in an array right? how could i do that? – sogyals429 Sep 11 '18 at 10:07
  • You do not have to store the pixels. You can edit the `BufferedImage` directly pixel by pixel and then store it again. –  Sep 12 '18 at 16:44