Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.imageio.stream.ImageInputStream;
- import javax.swing.ImageIcon;
- import javax.swing.JFileChooser;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JPanel;
- import javax.swing.filechooser.FileNameExtensionFilter;
- public class Modos {
- public static int N=3;
- public static Image image;
- public static BufferedImage bi;
- public static Graphics gr;
- public int[][] cipher(File input, int a[]) throws IOException{
- image= new ImageIcon(input.getAbsolutePath()).getImage();
- bi = new BufferedImage( image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
- gr = bi.getGraphics();
- gr.drawImage(image, 0, 0, null);
- Color c;
- int r, g, b;
- int tam = bi.getHeight() * bi.getWidth();
- int p=0;
- int[][] MatPixel = new int[tam][3];
- while(p<tam){
- for(int y=0; y < bi.getHeight(); y++){
- for(int x=0; x< bi.getWidth(); x++){
- c = new Color(bi.getRGB(x, y));
- r = c.getRed();
- g = c.getGreen();
- b = c.getBlue();
- MatPixel[p][0] = ObtenerMod(r+a[0]);
- MatPixel[p][1] = ObtenerMod(g+a[1]);
- MatPixel[p][2] = ObtenerMod(b+a[2]);
- p++;
- }
- }
- }
- return MatPixel;
- }
- public int[][] Decipher(File input, int a[]) throws IOException{
- image= new ImageIcon(input.getAbsolutePath()).getImage();
- bi = new BufferedImage( image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
- gr = bi.getGraphics();
- gr.drawImage(image, 0, 0, null);
- Color c;
- int r, g, b;
- a[0]=InverseAdditive(a[0]);
- a[1]=InverseAdditive(a[1]);
- a[2]=InverseAdditive(a[2]);
- int tam = bi.getHeight() * bi.getWidth();
- int p=0;
- int[][] MatPixel = new int[tam][3];
- while(p<tam){
- for(int y=0; y < bi.getHeight(); y++){
- for(int x=0; x< bi.getWidth(); x++){
- c = new Color(bi.getRGB(x, y));
- r = c.getRed();
- g = c.getGreen();
- b = c.getBlue();
- MatPixel[p][0] = ObtenerMod(r+a[0]);
- MatPixel[p][1] = ObtenerMod(g+a[1]);
- MatPixel[p][2] = ObtenerMod(b+a[2]);
- p++;
- }
- }
- }
- return MatPixel;
- }
- public int InverseAdditive(int a)
- {
- while(a<0)
- {
- a=a+256;
- }
- return a;
- }
- public void ModificarRGB(JPanel Panel,int result[][]) throws IOException{
- bi = new BufferedImage( image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_ARGB);
- gr = bi.getGraphics();
- gr.drawImage(image, 0, 0, null);
- System.out.println("desde modificar RGB");
- Color c;
- int p=0, r, g, b;
- while(p<bi.getHeight()){
- for(int y=0; y < bi.getHeight(); y++){
- for(int x=0; x< bi.getWidth(); x++){
- //c = new Color(bi.getRGB(x, y));
- r = (int)result[p][0];
- g = (int)result[p][1];
- b = (int)result[p][2];
- c = new Color(r, g, b);
- bi.setRGB(x, y, c.getRGB());
- p++;
- }
- }
- }
- Panel.add(new Panel(bi, Panel.getSize()));
- Panel.setVisible(true);
- Panel.repaint();
- GuardarIma(bi);
- }
- public int ObtenerMod(int num){
- if(num<0){
- while(num<0){
- num = num + 256;
- }
- }
- else{
- num = num %256;
- }
- return num;
- }
- public void GuardarIma(BufferedImage bi) throws IOException
- {
- JFileChooser fc = new JFileChooser();
- fc.addChoosableFileFilter(new FileNameExtensionFilter("todos los archivos *.png", "png", "PNG"));
- int elegido = fc.showSaveDialog(null);
- try{
- if(elegido == JFileChooser.APPROVE_OPTION)
- {
- File output = fc.getSelectedFile();
- String ubicacion = output.getAbsolutePath();
- // int cifrado=0;
- // String conta=Integer.toString(cifrado++);
- // ImageIO.write(bi, "png", new File("C:\\Users\\FRANCISCO MINAJAS\\Desktop\\"+conta+".png"));
- //cifrado= Integer.toString(conta);
- if(!(ubicacion.endsWith(".png"))){
- File g = new File(ubicacion+".png");
- output.renameTo(g);
- System.out.println(output.getAbsolutePath());
- ImageIO.write(bi, "png", new File(ubicacion));
- }
- }
- }
- catch(Exception e)
- {
- System.out.println("no se guardo");
- }
- }
- public int[][] MultiplicacionMatrices(int A[][], int B[][])
- {
- int suma = 0;
- int result[][] = new int[A.length][B.length];
- for(int i = 0; i < A.length; i++){
- for(int j = 0; j < B.length; j++){
- suma = 0;
- for(int k = 0; k < B.length; k++){
- suma += A[i][k] * B[k][j];
- }
- result[i][j] = suma;
- }
- }
- return result;
- }
- public int[][] Modulacion(int a[][])
- {
- for(int i=0;i<a.length;i++)
- {
- for(int j=0;j<a[0].length;j++)
- {
- System.out.print(" "+a[i][j]%256);
- }
- System.out.println();
- }
- return a;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement