Advertisement
franciscominajas

Modos

Feb 25th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.24 KB | None | 0 0
  1.  
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.awt.Image;
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import javax.imageio.ImageIO;
  9. import javax.imageio.stream.ImageInputStream;
  10. import javax.swing.ImageIcon;
  11. import javax.swing.JFileChooser;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.JPanel;
  15. import javax.swing.filechooser.FileNameExtensionFilter;
  16.  
  17. public class Modos {
  18.     public static int N=3;
  19.     public static Image image;
  20.     public static BufferedImage bi;
  21.     public static Graphics gr;
  22.    
  23.    
  24.     public int[][] cipher(File input, int a[]) throws IOException{
  25.         image= new ImageIcon(input.getAbsolutePath()).getImage();
  26.         bi = new BufferedImage( image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);  
  27.         gr = bi.getGraphics();
  28.         gr.drawImage(image, 0, 0, null);
  29.        
  30.         Color c;
  31.         int r, g, b;
  32.            
  33.         int tam = bi.getHeight() * bi.getWidth();
  34.         int p=0;
  35.         int[][] MatPixel = new int[tam][3];
  36.         while(p<tam){
  37.             for(int y=0; y < bi.getHeight(); y++){
  38.                 for(int x=0; x< bi.getWidth(); x++){
  39.                     c = new Color(bi.getRGB(x, y));
  40.                     r = c.getRed();
  41.                     g = c.getGreen();
  42.                     b = c.getBlue();
  43.                     MatPixel[p][0] = ObtenerMod(r+a[0]);
  44.                     MatPixel[p][1] = ObtenerMod(g+a[1]);
  45.                     MatPixel[p][2] = ObtenerMod(b+a[2]);
  46.                     p++;
  47.                 }
  48.             }
  49.         }
  50.         return MatPixel;
  51. }
  52.     public int[][] Decipher(File input, int a[]) throws IOException{
  53.         image= new ImageIcon(input.getAbsolutePath()).getImage();
  54.         bi = new BufferedImage( image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);  
  55.         gr = bi.getGraphics();
  56.         gr.drawImage(image, 0, 0, null);
  57.        
  58.         Color c;
  59.         int r, g, b;
  60.         a[0]=InverseAdditive(a[0]);
  61.         a[1]=InverseAdditive(a[1]);
  62.         a[2]=InverseAdditive(a[2]);
  63.         int tam = bi.getHeight() * bi.getWidth();
  64.         int p=0;
  65.         int[][] MatPixel = new int[tam][3];
  66.         while(p<tam){
  67.             for(int y=0; y < bi.getHeight(); y++){
  68.                 for(int x=0; x< bi.getWidth(); x++){
  69.                     c = new Color(bi.getRGB(x, y));
  70.                     r = c.getRed();
  71.                     g = c.getGreen();
  72.                     b = c.getBlue();
  73.                     MatPixel[p][0] = ObtenerMod(r+a[0]);
  74.                     MatPixel[p][1] = ObtenerMod(g+a[1]);
  75.                     MatPixel[p][2] = ObtenerMod(b+a[2]);
  76.                     p++;
  77.                 }
  78.             }
  79.         }
  80.         return MatPixel;
  81. }
  82.     public int InverseAdditive(int a)
  83.     {
  84.         while(a<0)
  85.         {
  86.             a=a+256;
  87.         }
  88.         return a;
  89.     }
  90.  
  91.    
  92.     public void ModificarRGB(JPanel Panel,int result[][]) throws IOException{
  93.             bi = new BufferedImage( image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);  
  94.             gr = bi.getGraphics();
  95.             gr.drawImage(image, 0, 0, null);
  96.             System.out.println("desde modificar RGB");
  97.            
  98.             Color c;
  99.             int p=0, r, g, b;
  100.             while(p<bi.getHeight()){
  101.                 for(int y=0; y < bi.getHeight(); y++){
  102.                     for(int x=0; x< bi.getWidth(); x++){
  103.                             //c = new Color(bi.getRGB(x, y));
  104.                             r = (int)result[p][0];
  105.                             g = (int)result[p][1];
  106.                             b = (int)result[p][2];
  107.                             c = new Color(r, g, b);
  108.                             bi.setRGB(x, y, c.getRGB());
  109.                             p++;
  110.                         }
  111.                     }
  112.             }
  113.            
  114.             Panel.add(new Panel(bi, Panel.getSize()));
  115.             Panel.setVisible(true);
  116.             Panel.repaint();
  117.             GuardarIma(bi);
  118.  
  119.         }
  120.  
  121.     public int ObtenerMod(int num){
  122.         if(num<0){
  123.             while(num<0){
  124.                 num = num + 256;
  125.             }
  126.         }
  127.         else{
  128.             num = num %256;
  129.         }
  130.         return num;
  131.     }
  132.    
  133.     public void GuardarIma(BufferedImage bi) throws IOException
  134.     {
  135.         JFileChooser fc = new JFileChooser();
  136.         fc.addChoosableFileFilter(new FileNameExtensionFilter("todos los archivos *.png", "png", "PNG"));
  137.         int elegido = fc.showSaveDialog(null);
  138.        
  139.         try{
  140.             if(elegido == JFileChooser.APPROVE_OPTION)
  141.             {
  142.                 File output = fc.getSelectedFile();
  143.                 String ubicacion = output.getAbsolutePath();
  144. //        int cifrado=0;
  145. //        String conta=Integer.toString(cifrado++);
  146. //                ImageIO.write(bi, "png", new File("C:\\Users\\FRANCISCO MINAJAS\\Desktop\\"+conta+".png"));
  147.         //cifrado= Integer.toString(conta);
  148.                 if(!(ubicacion.endsWith(".png"))){
  149.                     File g = new File(ubicacion+".png");
  150.                     output.renameTo(g);
  151.                     System.out.println(output.getAbsolutePath());
  152.                     ImageIO.write(bi, "png", new File(ubicacion));
  153.                 }
  154.                
  155.             }            
  156.         }
  157.         catch(Exception e)
  158.         {
  159.             System.out.println("no se guardo");
  160.         }
  161.     }
  162.    
  163.     public int[][] MultiplicacionMatrices(int A[][], int B[][])
  164.     {
  165.         int suma = 0;  
  166.         int result[][] = new int[A.length][B.length];  
  167.         for(int i = 0; i < A.length; i++){  
  168.             for(int j = 0; j < B.length; j++){  
  169.                 suma = 0;  
  170.                 for(int k = 0; k < B.length; k++){  
  171.                     suma += A[i][k] * B[k][j];  
  172.                 }  
  173.                 result[i][j] = suma;  
  174.             }  
  175.         }  
  176.         return result;
  177.     }
  178.    
  179.     public int[][] Modulacion(int a[][])
  180.     {
  181.         for(int i=0;i<a.length;i++)
  182.         {
  183.             for(int j=0;j<a[0].length;j++)
  184.             {
  185.                 System.out.print(" "+a[i][j]%256);
  186.             }
  187.             System.out.println();
  188.         }
  189.         return a;
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement