This week's book giveaway is in the Raspberry Pi forum.
We're giving away four copies of Getting started with Java on the Raspberry Pi and have Frank DelPorte on-line!
See this thread for details.
Win a copy of Getting started with Java on the Raspberry Pi this week in the Raspberry Pi forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Liutauras Vilda
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Piet Souris

Steganography on an Image file

 
Greenhorn
Posts: 4
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exception in thread "main" java.lang.IllegalArgumentException: image == null!
       at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(Unknown Source)
       at javax.imageio.ImageIO.getWriter(Unknown Source)
       at javax.imageio.ImageIO.write(Unknown Source)
       at y.main(y.java:36)


I am getting this error as i try to modify bytes of an image and generate a new one.

I am trying to hide a string message by converting it to binary and adding it to last two bits of the image bytes (LSB Steganography).

When a message is typed it gives above error, but if no message is written it doesn't show the error and makes a copy of the image.

I am a beginner at this, any kind of help would be appreciated.

 
Saloon Keeper
Posts: 7520
172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apparently "bImageFromConvert" is null - if it is, the code shouldn't try to write it to a file. The javadocs of ImageIO.read state the conditions under which it returns null - make sure those aren't met.
 
Marshal
Posts: 78427
374
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...and welcome to the Ranch
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm no expert on image formats so may be wrong here but I don't think you can just append a few bytes to the end of the file and still have a valid PNG formatted file which may be why ImageIO.read() is returning null.
PNG does support metadata though so you could  try to add your data as metadata (Beware: metadata can be deleted by some image editing programs so if the image is opened and saved in a program that doesn't support PNG metadata the data may well be lost). If you want to hide the text in the actual image then you probably need to read the PNG specification to find out how the file is formatted and how you can modify/append bytes and maintain a valid file format.

For adding metadata see this stackoverflow thread
 
Tim Moores
Saloon Keeper
Posts: 7520
172
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:I'm no expert on image formats so may be wrong here but I don't think you can just append a few bytes to the end of the file and still have a valid PNG formatted file which may be why ImageIO.read() is returning null.


I don't think there's any appending going on (the array being written has the same size as the array being read), but "arr" contains the entire file -including metadata- rather than just the image data. You should read the image data into a BufferedImage using ImageIO.read, and then do the bit manipulations on the Raster obtained via getRaster(). When you're done, copy the data back into the BufferedImage via setData(Raster), and save it via ImageIO.write.
 
Ranch Hand
Posts: 146
4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the purpose of the StringToByte() method?  Why not directly code the desired bytes:
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:

Tony Docherty wrote:I'm no expert on image formats so may be wrong here but I don't think you can just append a few bytes to the end of the file and still have a valid PNG formatted file which may be why ImageIO.read() is returning null.


I don't think there's any appending going on (the array being written has the same size as the array being read), but "arr" contains the entire file -including metadata- rather than just the image data. You should read the image data into a BufferedImage using ImageIO.read, and then do the bit manipulations on the Raster obtained via getRaster(). When you're done, copy the data back into the BufferedImage via setData(Raster), and save it via ImageIO.write.


Sorry my mistake, I didn't read the OP's post correctly. Your suggestion is correct. PNG files have a header section so you can't just overwrite the file wherever you feel like but you should be able overwrite the data in the Raster section as you suggested
 
Lovlin Thakkar
Greenhorn
Posts: 4
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norman Radder wrote:What is the purpose of the StringToByte() method?  Why not directly code the desired bytes:



Yeah, true.  Don't know how did I forget this.
 
Lovlin Thakkar
Greenhorn
Posts: 4
Notepad
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Moores wrote:

Tony Docherty wrote:I'm no expert on image formats so may be wrong here but I don't think you can just append a few bytes to the end of the file and still have a valid PNG formatted file which may be why ImageIO.read() is returning null.


I don't think there's any appending going on (the array being written has the same size as the array being read), but "arr" contains the entire file -including metadata- rather than just the image data. You should read the image data into a BufferedImage using ImageIO.read, and then do the bit manipulations on the Raster obtained via getRaster(). When you're done, copy the data back into the BufferedImage via setData(Raster), and save it via ImageIO.write.





I've done this to get raster data from file f and then write the changed array to the bufferedimage. I don't know how to write an array to a raster or to a databufferbyte and cannot find on the internet either. Can you please help me with this?
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, not sure what your problem is.
The code shown will convert the byte array to a BufferedImage. It's then trying to write the BufferredImage to a file which will fail as the File object is null.
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
Low Tech Laboratory
https://www.kickstarter.com/projects/paulwheaton/low-tech-0
reply
    Bookmark Topic Watch Topic
  • New Topic