Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.30 KB | None | 0 0
  1. package de.tuberlin.snet.prog2.ue08.imagefilter;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.Collection;
  7. import java.util.List;
  8. import java.util.stream.Collectors;
  9.  
  10. import javafx.application.Application;
  11. import javafx.geometry.Orientation;
  12. import javafx.scene.Scene;
  13. import javafx.scene.image.Image;
  14. import javafx.scene.image.ImageView;
  15. import javafx.scene.image.PixelReader;
  16. import javafx.scene.image.PixelWriter;
  17. import javafx.scene.image.WritableImage;
  18. import javafx.scene.layout.FlowPane;
  19. import javafx.scene.paint.Color;
  20. import javafx.stage.Stage;
  21.  
  22. /**
  23. * Class for showing and manipulating images.
  24. * @author prog2-team
  25. *
  26. */
  27. public class ImageFilter extends Application {
  28.  
  29. final static String TU_LOGO = "file:TU_Logo_kurz_RGB_schwarz.jpeg";
  30. final static String PIC1 = "file:pic1.jpg";
  31. final static String PIC2 = "file:pic2.bmp";
  32.  
  33. /** max x-coordinate + 1 */
  34. int width;
  35. /** max y-coordinate + 1 */
  36. int height;
  37.  
  38. public static void main(String[] args) throws IOException {
  39. ImageFilter.launch(args);
  40. }
  41.  
  42. @Override
  43. public void start(Stage primaryStage) {
  44.  
  45. // load and set the image to the view
  46. Image image = new Image(PIC1);
  47. ImageView imageView = new ImageView();
  48. imageView.setImage(image);
  49.  
  50. width = (int)image.getWidth();
  51. height = (int)image.getHeight();
  52.  
  53. // transform image to collection of pixels
  54. PixelReader pixelReader = image.getPixelReader();
  55. Collection<Pixel> imagePixels;
  56. imagePixels = readImage(pixelReader);
  57. // manipulate image
  58. imagePixels = manipulateImage(imagePixels);
  59.  
  60. // TODO: erzeuge und manipuliere 2d-Array
  61.  
  62. // write writableImage via pixelWriter
  63. WritableImage writableImage = new WritableImage(width, height);
  64. PixelWriter pixelWriter = writableImage.getPixelWriter();
  65. imagePixels.forEach( p -> pixelWriter.setColor(p.x, p.y, p.color) );
  66.  
  67. // set the manipulated image to the view
  68. ImageView destImageView = new ImageView();
  69. destImageView.setImage(writableImage);
  70.  
  71. FlowPane root = new FlowPane(Orientation.VERTICAL);
  72. root.getChildren().addAll(imageView, destImageView);
  73. Scene scene = new Scene(root);
  74. primaryStage.setTitle("TU-Filter");
  75. primaryStage.setScene(scene);
  76. primaryStage.show();
  77. }
  78.  
  79. /**
  80. * manipulates the the given pixels with diverse mathods
  81. * @param imagePixels
  82. * @return manipulated image pixels
  83. */
  84. private Collection<Pixel> manipulateImage(Collection<Pixel> imagePixels) {
  85. Collection<Pixel> filteredImagePixels;
  86. // TODO: apply filter methods
  87. // filteredImagePixels = saturatePixels(imagePixels);
  88. filteredImagePixels = changeBrightness(imagePixels);
  89. return filteredImagePixels;
  90. }
  91.  
  92.  
  93. /**
  94. * Creates a collection of pixels.
  95. * @param pixelReader
  96. * @return collection of pixels
  97. */
  98. private Collection<Pixel> readImage(PixelReader pixelReader) {
  99. ArrayList<Pixel> imagePixels = new ArrayList<Pixel>();
  100. for (int y = 0; y < height; y++){
  101. for (int x = 0; x < width; x++){
  102. imagePixels.add(new Pixel(x, y, pixelReader.getColor(x, y)));
  103. }
  104. }
  105. return imagePixels;
  106. }
  107.  
  108.  
  109. /**
  110. * Creates a copy of the given collection of pixels.
  111. * @param imagePixels
  112. * @return copy of imagePixels
  113. */
  114. private Collection<Pixel> copyPixels(Collection<Pixel> imagePixels) {
  115. Collection<Pixel> filteredImagePixels;
  116. filteredImagePixels = imagePixels.stream()
  117. .map(p -> p)
  118. .collect(Collectors.toCollection(ArrayList::new));
  119. return filteredImagePixels;
  120. }
  121.  
  122. private Collection<Pixel> saturatePixels(Collection<Pixel> imagePixels){
  123. Collection<Pixel> newPixels = imagePixels.stream()
  124. .map(x->x.copy(x,x.color.saturate()))
  125. .collect(Collectors.toCollection(ArrayList::new));
  126. return newPixels;
  127. }
  128.  
  129. private Collection<Pixel> changeBrightness(Collection<Pixel> imagePixels){
  130. imagePixels = imagePixels.stream()
  131. .map(x->x.copy(x, x.color.darker()))
  132. .collect(Collectors.toCollection(ArrayList::new));
  133. return imagePixels;
  134. }
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement