Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.74 KB | None | 0 0
  1. package com.company;
  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.  
  9. public class Negatyw {
  10.  
  11.     private static BufferedImage image;
  12.     // niepotrzebna tablica tablicy przy typie zmiennej
  13.     private static int height;
  14.     private static int width;
  15.  
  16.     public Negatyw(String s) {  // ścieżka w argumencie
  17.         try {
  18.             File file = new File(s);    // zmienna pomocnicza
  19.             image = ImageIO.read(file);
  20.         } catch (IOException ex) {
  21.             System.err.println(ex.getLocalizedMessage());
  22.         }
  23.         width = image.getWidth()-1;
  24.         height = image.getHeight()-1;   // usuwamy printowanie
  25.     }
  26.  
  27.     public static void main(String[] args) {
  28.         Negatyw negativeImage = new Negatyw("../resources/Podział.jpg");    // lepsza nazwa obiektu
  29.         // niepotrzebny label
  30.         for (int i = 1; i < height; i++) {
  31.             for (int j = 1; j < width; j++) {
  32.                 // niepotrzebny if
  33.                 int rgb = image.getRGB(i, j);
  34.                 Color color = new Color(rgb);
  35.                 // niepotrzebny if
  36.                 int red = color.getRed();
  37.                 int green = color.getGreen();
  38.                 int blue = color.getBlue();
  39.                 Color colorNegative = new Color(255-red, 255-green, 255-blue);   // trzymamy się angielskich nazw zmiennych
  40.                 image.setRGB(i, j, colorNegative.getRGB());
  41.             }
  42.         }
  43.  
  44.         File newFile = new File("./Brudny.jpg");
  45.  
  46.         try {
  47.             ImageIO.write(image, "jpg", newFile);
  48.         } catch (IOException ex) {
  49.             System.err.println(ex.getLocalizedMessage());
  50.         }
  51.  
  52.     }
  53.  
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement