ImageMirror.java 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import java.awt.Color;
  2. import java.awt.image.BufferedImage;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import javax.imageio.ImageIO;
  6. public class ImageMirror {
  7. public static void main(String [] args) {
  8. String outFormat = "png";
  9. File inputPath = new File("img/Doctor_Strange.png");
  10. File outputPath = new File("img/Doctor_Strange_spiegel.png");
  11. BufferedImage original_image = null;
  12. try {
  13. original_image = ImageIO.read(inputPath);
  14. System.out.println("Reading " + inputPath + " complete !"); }
  15. catch (IOException e) {
  16. System.out.println("Error: " + e);
  17. System.exit(1);
  18. }
  19. int width = original_image.getWidth();
  20. int height = original_image.getHeight();
  21. System.out.println("\n" + "width = " + width + " height = " +
  22. height + "\n");
  23. BufferedImage newImage = new BufferedImage(width * 2, height,
  24. BufferedImage.TYPE_INT_ARGB);
  25. newImage.createGraphics().drawImage(original_image, 0, 0, null);
  26. int p;
  27. for (int y = 0; y < height; y++) {
  28. // lx/rx starts from the left/right side of the image
  29. for (int lx = 0, rx = width*2 - 1; lx < width; lx++, rx--) {
  30. // Pixelwert an der Koordinate (lx, y) abfragen und speichern
  31. p = newImage.getRGB(lx, y);
  32. // Spiegel-Pixel-Wert p an der Koordinate (lx, y) setzen
  33. newImage.setRGB(lx, y, p);
  34. // Spiegel-Pixel-Wert p an der Koordinate (rx, y) setzen
  35. newImage.setRGB(rx, y, p);
  36. }
  37. }
  38. try {
  39. ImageIO.write(newImage.getSubimage(width, 0, width, height), outFormat, outputPath);
  40. System.out.println("Writing " + outputPath + " complete !"); }
  41. catch(IOException e) {
  42. System.out.println("Error: " + e);
  43. System.exit(1);
  44. }
  45. System.exit(0);
  46. }
  47. }