fork download
  1. import java.awt.image.BufferedImage;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. //import java.util.Arrays;
  6. import java.util.Scanner;
  7.  
  8. import javax.imageio.ImageIO;
  9. public class LSB_encode {
  10. static final String MESSAGEFILE = "C:\\message.txt";
  11. static final String COVERIMAGEFILE = "C:\\cover.jpeg";
  12. static final String STEGIMAGEFILE = "C:\\steg.png";
  13. public static void main(String[] args) throws Exception {
  14.  
  15. String contentOfMessageFile = (readMessageFile());
  16. int[] bits=bit_Msg(contentOfMessageFile);
  17. System.out.println("msg in file "+contentOfMessageFile);
  18. for(int i=0;i<bits.length;i++)
  19. System.out.print(bits[i]);
  20. System.out.println();
  21. BufferedImage theImage=readImageFile(COVERIMAGEFILE);
  22. hideTheMessage(bits, theImage);
  23.  
  24. }
  25.  
  26. public static String readMessageFile () throws FileNotFoundException{
  27. String contentOfMessageFile = "";
  28. File a = new File (MESSAGEFILE);
  29. Scanner scan = new Scanner (a);
  30. while (scan.hasNextLine()){
  31. String next = scan.nextLine();
  32. contentOfMessageFile += next;
  33. if (scan.hasNextLine()){
  34. contentOfMessageFile += "\n";
  35. }
  36. }
  37. scan.close();
  38. return contentOfMessageFile;
  39. }
  40. public static int[] bit_Msg(String msg){
  41. int j=0;
  42. int[] b_msg=new int[msg.length()*8];
  43. for(int i=0;i<msg.length();i++){
  44. int x=msg.charAt(i);
  45. String x_s=Integer.toBinaryString(x);
  46. while(x_s.length()!=8){
  47. x_s='0'+x_s;
  48. }
  49. System.out.println("dec value for "+x +" is "+x_s);
  50.  
  51. for(int i1=0;i1<8;i1++) {
  52. b_msg[j] = Integer.parseInt(String.valueOf(x_s.charAt(i1)));
  53. j++;
  54. };
  55. }
  56.  
  57. return b_msg;
  58. }
  59. public static BufferedImage readImageFile(String COVERIMAGEFILE){
  60. BufferedImage theImage = null;
  61. File p = new File (COVERIMAGEFILE);
  62. try{
  63. theImage = ImageIO.read(p);
  64. }catch (IOException e){
  65. e.printStackTrace();
  66. System.exit(1);
  67. }
  68. return theImage;
  69. }
  70.  
  71.  
  72. public static void hideTheMessage (int[] bits, BufferedImage theImage) throws Exception{
  73. File f = new File (STEGIMAGEFILE);
  74. BufferedImage sten_img=null;
  75. int bit_l=bits.length/8;
  76. int[] bl_msg=new int[8];
  77. System.out.println("bit lent "+bit_l);
  78. String bl_s=Integer.toBinaryString(bit_l);
  79. while(bl_s.length()!=8){
  80. bl_s='0'+bl_s;
  81. }
  82. for(int i1=0;i1<8;i1++) {
  83. bl_msg[i1] = Integer.parseInt(String.valueOf(bl_s.charAt(i1)));
  84. };
  85. int j=0;
  86. int b=0;
  87. int currentBitEntry=8;
  88.  
  89. for (int x = 0; x < theImage.getWidth(); x++){
  90. for ( int y = 0; y < theImage.getHeight(); y++){
  91. if(x==0&&y<8){
  92. int currentPixel = theImage.getRGB(x, y);
  93. int ori=currentPixel;
  94. int red = currentPixel>>16;
  95. red = red & 255;
  96. int green = currentPixel>>8;
  97. green = green & 255;
  98. int blue = currentPixel;
  99. blue = blue & 255;
  100. String x_s=Integer.toBinaryString(blue);
  101. String sten_s=x_s.substring(0, x_s.length()-1);
  102. sten_s=sten_s+Integer.toString(bl_msg[b]);
  103.  
  104. //j++;
  105. int temp=Integer.parseInt(sten_s,2);
  106. int s_pixel=Integer.parseInt(sten_s, 2);
  107. int a=255;
  108. int rgb = (a<<24) | (red<<16) | (green<<8) | s_pixel;
  109. theImage.setRGB(x, y, rgb);
  110. //System.out.println("original "+ori+" after "+theImage.getRGB(x, y));
  111. ImageIO.write(theImage, "png", f);
  112. b++;
  113.  
  114. }
  115. else if (currentBitEntry < bits.length+8 ){
  116.  
  117. int currentPixel = theImage.getRGB(x, y);
  118. int ori=currentPixel;
  119. int red = currentPixel>>16;
  120. red = red & 255;
  121. int green = currentPixel>>8;
  122. green = green & 255;
  123. int blue = currentPixel;
  124. blue = blue & 255;
  125. String x_s=Integer.toBinaryString(blue);
  126. String sten_s=x_s.substring(0, x_s.length()-1);
  127. sten_s=sten_s+Integer.toString(bits[j]);
  128. j++;
  129. int temp=Integer.parseInt(sten_s,2);
  130. int s_pixel=Integer.parseInt(sten_s, 2);
  131.  
  132. int a=255;
  133. int rgb = (a<<24) | (red<<16) | (green<<8) | s_pixel;
  134. theImage.setRGB(x, y, rgb);
  135. //System.out.println("original "+ori+" after "+theImage.getRGB(x, y));
  136. ImageIO.write(theImage, "png", f);
  137.  
  138. currentBitEntry++;
  139. //System.out.println("curre "+currentBitEntry);
  140. }
  141. }
  142. }
  143. }
  144. }
  145.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:9: error: class LSB_encode is public, should be declared in a file named LSB_encode.java
public class LSB_encode {
       ^
1 error
stdout
Standard output is empty