Steganography code

Hi,

Below is the code from my Steganography application or hiding a file within an image (bmp). the application has compiled, and works to the extent that a user can chose files etc. However, when the Hidebutton is clicked (which activates the LSB algorithm) and JFrame appears, there is no image displayed. Can anyone see why? Any help would be very very very much appreciated,

Thank you.

import java.io.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.event.ActionEvent.*;

import java.awt.event.ActionListener.*;

import javax.swing.*;

import javax.swing.filechooser.*;

import java.awt.Graphics;

import java.awt.Image.*;

import java.awt.Toolkit.*;

public class GridBagApplication extends JFrame implements ActionListener {

// instance variables

private GridBagLayout layout;

private GridBagConstraints constraints;

private Container container;

JButton browseButton1;

JButton browseButton2;

JTextField textField1;

JTextField textField2;

JButton hideButton;

byte value;

int bitpos = 0;

byte Extract;

byte bit;

public static void main(String args[]){

GridBagApplication application = new GridBagApplication();

}

public GridBagApplication()

{

super( "Steganography Application " );

container = getContentPane();

layout = new GridBagLayout();

container.setLayout( layout );

constraints = new GridBagConstraints();

JLabel labelbrowse1 = new JLabel( "Carrier Image:" );

JLabel labelbrowse2 = new JLabel( " File to be hidden: " );

textField1 = new JTextField( " " , 500 );

textField2 = new JTextField( " " , 500 );

browseButton1 = new JButton( "Browse" );

browseButton1.addActionListener(this);

browseButton2 = new JButton( "Browse" );

browseButton2.addActionListener(this);

hideButton = new JButton( "Hide" );

hideButton.addActionListener(this);

constraints.fill = GridBagConstraints.NONE;

addComponent( labelbrowse1, 0,0,1,1);

constraints.fill = GridBagConstraints.HORIZONTAL;

addComponent( textField1, 0,1,1,1);

constraints.fill = GridBagConstraints.NONE;

addComponent( browseButton1, 0,2,1,1);

constraints.fill = GridBagConstraints.NONE;

addComponent( labelbrowse2, 1,0,1,1);

constraints.fill = GridBagConstraints.HORIZONTAL;

addComponent( textField2, 1,1,1,1);

constraints.fill = GridBagConstraints.NONE;

addComponent( browseButton2, 1,2,1,1);

constraints.fill = GridBagConstraints.HORIZONTAL;

addComponent( hideButton, 2,1,1,1);

setSize( 500, 220 );

setVisible( true );

} // end constructor

private void addComponent( Component component,

int row, int column, int width, int height )

{

constraints.weightx = 1;

constraints.weighty = 1;

constraints.gridx = column;

constraints.gridy = row;

constraints.gridwidth = width;

constraints.gridheight = height;

layout.setConstraints( component, constraints );

container.add( component );

} //end addComponent

// Based on Devx algorithm: (see references)

public byte Replace(byte bit, int bitpos,

byte value) {

return (byte) (value == 1 ? bit | (1 << bitpos)

: bit & ~(1 << bitpos));

} //end of replace

public byte Extract (byte bit, int bitpos){

return (byte) ((bit & (1 << bitpos)) >> bitpos);

} // end of extract

public void actionPerformed(ActionEvent e) {

File file1;

file1 = null;

File file2;

file2 = null;

if (e.getSource() == browseButton1) {

JFileChooser fc1 = new JFileChooser();

fc1.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

int returnVal = fc1.showOpenDialog(null);

if (returnVal == JFileChooser.APPROVE_OPTION) {

file1 = fc1.getSelectedFile();

textField1.setText(file1.getAbsolutePath());

}

} //end if browseButton1

if (e.getSource() == browseButton2) {

JFileChooser fc2 = new JFileChooser();

fc2.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

int returnVal = fc2.showOpenDialog(null);

if (returnVal == JFileChooser.APPROVE_OPTION) {

file2 = fc2.getSelectedFile();

textField2.setText(file2.getAbsolutePath());

}

} //end if browseButton2

if (e.getSource() == hideButton) {

//System.out.println("hide");

byte[] hidden;

// read file2 into hidden

file1=new File(textField1.getText());

file2=new File(textField2.getText());

if (file1 != null && file2 != null){

try {

DataInputStream image = new DataInputStream (new FileInputStream(( file1 )));

DataInputStream hiddenfile = new DataInputStream (new FileInputStream(( file2 )));

//DataOutputStream outimage = new DataOutputStream (new FileOutputStream (( image)));

hidden = new byte[(int)file2.length()];

hiddenfile.readFully(hidden);

byte [] newimage;

newimage = new byte[(int)file1.length()];

image.readFully( newimage );

int count = 0;

byte inbyte;

byte outbyte;

byte value;

byte replace;

int byteRead;

byte byteWrite;

for (int i= 0; i<newimage.length; i++){

outbyte = (byte)newimage;

if (count><hidden.length) {

bit = Extract(hidden [ count], bitpos++);

outbyte = Replace(outbyte, 0, bit);

if (bitpos==8) {bitpos=0; count++;}

newimage = outbyte;

} //end if

// DataOutputStream.writeByte(byteWrite);

} // end for loop

Image picture = Toolkit.getDefaultToolkit().createImage( newimage );

JFrame display = new JFrame("Steganography result");

display.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

display.setResizable(true);

display.setSize(620,460);

Container cp = display.getContentPane();

DrawingPanel dp = new DrawingPanel(picture);

cp.add(dp);

display.setVisible(true);

} catch (IOException complete) {

System.out.println("IOException");

}

} // end if either field is empty

else {

// System.out.println("file null");

}

} // end action performed if 'hide button'

} // end actionPerformed

} //end GridBagApplication class

ANOTHER CLASS (which should help display the image) :

import javax.swing.*;

import java.awt.*;

class DrawingPanel extends JPanel

{

Image img;

DrawingPanel (Image img)

{ this.img = img; }

public void paintComponent (Graphics g) {

super.paintComponent (g);

// Use the image width & height to find the starting point

int imgX = getSize ().width/2 - img.getWidth (this);

int imgY = getSize ().height/2 - img.getHeight (this);

//Draw image centered in the middle of the panel

g.drawImage (img, imgX, imgY, this);

} // paintComponent

} // DrawingPanel>

[7200 byte] By [lost_soula] at [2007-11-27 2:27:21]
# 1
Read this please: http://forum.java.sun.com/help.jspa?sec=formattingThen post your code again. And next time, please ask Swing questions in the Swing forum.Are you sure the UI is actually repainted after you got that image?
CeciNEstPasUnProgrammeura at 2007-7-12 2:37:56 > top of Java-index,Java Essentials,Java Programming...
# 2
The first thing to do is to prove that you can display the image file without it being modified by your stego algorithms.
sabre150a at 2007-7-12 2:37:56 > top of Java-index,Java Essentials,Java Programming...