Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
JStenografer - Stenografer.java

Stenografer.java

Caricato da:
Scarica il programma completo

  1. package jstenografer;
  2.  
  3. import java.awt.Color;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import javax.imageio.ImageIO;
  8. import jstenografer.exception.*;
  9.  
  10. public class Stenografer {
  11.  
  12.     public static void write(File pOpenFile, String pSteno, File pDestination) throws IOException, TooLongStringException, NotBMPDestinationFile {
  13.         if (pOpenFile.exists()) {
  14.             BufferedImage img = ImageIO.read(pOpenFile);
  15.             String nameFile=pDestination.getPath();
  16.             String ext=nameFile.substring(nameFile.length()-3);
  17.             if (pSteno.length() > img.getWidth() * img.getHeight()) {
  18.                 throw new TooLongStringException();
  19.             }
  20.             if(!ext.equalsIgnoreCase("bmp")){
  21.                 throw new NotBMPDestinationFile();
  22.             }
  23.             int w = 1;
  24.             int h = 0;
  25.             Color color = new Color(img.getRGB(0, 0));
  26.             int r = color.getRed();
  27.             int g = color.getGreen();
  28.             int b = pSteno.length();
  29.             Color newColor = new Color(r, g, b);
  30.             img.setRGB(0, 0, newColor.getRGB());
  31.             for (int i = 0; i < pSteno.length(); i++) {
  32.                 color = new Color(img.getRGB(w, h));
  33.                 r = color.getRed();
  34.                 g = color.getGreen();
  35.                 b = (int) pSteno.charAt(i);
  36.                 newColor = new Color(r, g, b);
  37.                 img.setRGB(w, h, newColor.getRGB());
  38.                 w++;
  39.                 if (w >= img.getWidth()) {
  40.                     w = 0;
  41.                     h++;
  42.                 }
  43.             }
  44.             ImageIO.write(img, "bmp", pDestination);
  45.         }
  46.     }
  47.  
  48.     public static String read(File pOpenFile) throws IOException {
  49.         if (pOpenFile.exists()) {
  50.             BufferedImage img = ImageIO.read(pOpenFile);
  51.             Color color=new Color(img.getRGB(0, 0));
  52.             int leng=color.getBlue();
  53.             int w=1;
  54.             int h=0;
  55.             char lett;
  56.             String retval="";
  57.             for(int i=0; i<leng; i++){
  58.                 color=new Color(img.getRGB(w, h));
  59.                 lett=(char)color.getBlue();
  60.                 retval+=lett;
  61.                 w++;
  62.                 if(w>=img.getWidth()){
  63.                     w=0;
  64.                     h++;
  65.                 }
  66.             }
  67.             return retval;
  68.         }
  69.         return "";
  70.     }
  71. }