Question

Write a Java program that can embed and extract a simple text message into a chosen...

Write a Java program that can embed and extract a simple text message into a chosen PNG image. The program should also allow extraction of the message from the stegotext image. In particular, the program should do the following:

1. Prompts user to enter a choice of PNG image to hide a text message.

2. Prompts user to enter the text message that is to be hidden.

3. Create a modified image after hiding the text message (as explained above) and save the image as a new PNG picture.

4. Display the hidden message when a PNG image is supplied. [For testing purpose, the user may choose to use a different image file that already has a message hidden into it following the above scheme.]

-------------------------------------

A sample Java code (PixelData.java) is provided to give an idea how to get R, G, B values of a pixel in a PNG image. You can build on that, but you need to explore BufferedImage class, Color class, ImageIO package in JavaFX, getRGB(), setRGB() methods etc.from Java API.

-------------------------------------

Here's the PixelData.java code:

import java.util.*;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;

public class PixelBits {

public static void main(String[] args) {
  
Scanner input = new Scanner(System.in);
System.out.print("Enter the input image file name (with .png extension): ");
String file = input.next();

BufferedImage img;

try {
img = ImageIO.read(new File(file));
// There is a method ImageIO.write() to create a
// picture file from a BufferedImage object
  
int h = img.getHeight();
int w = img.getWidth();
int totalpixel = h*w;
System.out.println("\n Image height = " + h + " and Image width = " + w);
System.out.println(" Total number of pixels = " + totalpixel);
  
System.out.println("\n In each pixel you have 3 components, R-G-B.\n Hence you can embed 3 bits in each pixel.");
System.out.println("\n Total number of bits that can be embedded in the picture = " + totalpixel*3);
System.out.println("\n Total number of ASCII characters that can be embedded = " + (totalpixel*3)/8);
  
// store each pixel's RGB values
int[][] pixelData = new int[h*w][3];
int[] rgb;

int numberOfPixels = 0;
for(int i = 0; i < h; i++){
for(int j = 0; j < w; j++){
rgb = PixelData(img, j, i);

for(int k = 0; k < rgb.length; k++){
pixelData[numberOfPixels][k] = rgb[k];
}

numberOfPixels++;
}
}

} catch (IOException e) {
e.printStackTrace();
}

}

private static int[] PixelData(BufferedImage img, int x, int y) {
int argb = img.getRGB(x, y);
  
int[] rgb = new int[3];
rgb[0] = (argb >> 16) & 0xff; //Red
rgb[1] = (argb >> 8) & 0xff; // Green
rgb[2] = (argb) & 0xff; // Blue

// 0xff is Hex for 255 and used as a mask to get last 8 bits of a bit string

// Remove the comment from the next line if you want to display RGB values for all pixels.
// Caution: There can be too many pixels to display. So use it if absolutely necessary.

//System.out.println("Red: " + rgb[0] + ", Green: " + rgb[1] + ", Blue: " + rgb[2]);
return rgb;
}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.awt.Component;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.util.Scanner;

public class Steganography extends Component {

public static void main(String[] foo) {
new JavaWalkBufferedImageTest1();
   Scanner user_input = new Scanner(System.in);
   System.out.println("Hidden Message");
   String hiddenmsg;
   hiddenmsg=user_input.next();
   byte[] bytes = hiddenmsg.getBytes();
   StringBuilder binary = new StringBuilder();
   for (byte b : bytes)
   {
       int val = b;
       for (int i = 0; i < 8; i++)
       {
           binary.append((val & 128) == 0 ? 0 : 1);
           val <<= 1;
       }
   }
   System.out.println("'" + hiddenmsg + "' to binary: " + binary);
}
  
public void printPixelARGB(int pixel) {
int red = (pixel >> 16) & 0xff;
int green = (pixel >> 8) & 0xff;
int blue = (pixel) & 0xff;
   String strBinaryNumber1 = Integer.toBinaryString(red);
   String strBinaryNumber2 = Integer.toBinaryString(green);
   String strBinaryNumber3 = Integer.toBinaryString(blue);
System.out.println("RGB (Decimal Format):" + red + ", " + green + ", " + blue);
   System.out.println("RGB (Binary Format):"+strBinaryNumber1+","+strBinaryNumber2+","+strBinaryNumber3);
   System.out.println("Last bit of RGB:" +strBinaryNumber1.substring(strBinaryNumber1.length()-1) + "," + strBinaryNumber2.substring(strBinaryNumber2.length()-1) + "," +strBinaryNumber3.substring(strBinaryNumber3.length()-1));
}
  
  

private void marchThroughImage(BufferedImage image) {
int w = image.getWidth();
int h = image.getHeight();

for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
int pixel = image.getRGB(j, i);
printPixelARGB(pixel);
System.out.println("");
}
}
}

public Steganography() {
try {
// get the BufferedImage, using the ImageIO class
   System.out.println("Enter the name of the image");
   Scanner input = new Scanner(System.in);
   String imagename;
   imagename=input.next();
BufferedImage image =
ImageIO.read(this.getClass().getResource(imagename+".jpg"));
marchThroughImage(image);
} catch (IOException e) {
System.err.println(e.getMessage());
}
}}

Add a comment
Know the answer?
Add Answer to:
Write a Java program that can embed and extract a simple text message into a chosen...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT