Advertisement
kajacx

Cernobila

Sep 28th, 2012
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.40 KB | None | 0 0
  1. package gTest;
  2.  
  3. import java.awt.*;
  4. import java.awt.color.*;
  5. import java.awt.event.*;
  6. import java.awt.image.*;
  7. import java.io.*;
  8. import javax.imageio.*;
  9. import javax.swing.*;
  10.  
  11. public class PictureLoader extends JFrame implements ActionListener {
  12.  
  13.     private Image image;
  14.     private JFileChooser chooser = new JFileChooser();
  15.     private PicPanel pic = new PicPanel();
  16.     private JButton load = new JButton("Load"), save = new JButton("Save"),
  17.             convert1 = new JButton("Con1"), convert2 = new JButton("Con2"),
  18.             convert3 = new JButton("Con3");
  19.  
  20.     public static void main(String[] args) {
  21.         new PictureLoader().setVisible(true);
  22.     }
  23.  
  24.     @SuppressWarnings("LeakingThisInConstructor")
  25.     private PictureLoader() {
  26.         super("PictureLoader");
  27.         setLayout(new BorderLayout());
  28.         setLocation(20, 20);
  29.  
  30.         chooser.setSelectedFile(new File("X:/java/pic/brb.jpg")); //TODO: change this
  31.  
  32.         Container flow = new Container();
  33.         flow.setLayout(new FlowLayout(FlowLayout.CENTER));
  34.  
  35.         load.addActionListener(this);
  36.         flow.add(load);
  37.  
  38.         save.addActionListener(this);
  39.         flow.add(save);
  40.  
  41.         convert1.addActionListener(this);
  42.         flow.add(convert1);
  43.  
  44.         convert2.addActionListener(this);
  45.         flow.add(convert2);
  46.  
  47.         convert3.addActionListener(this);
  48.         flow.add(convert3);
  49.  
  50.         add(flow, BorderLayout.NORTH);
  51.  
  52.         add(new JScrollPane(pic), BorderLayout.CENTER);
  53.  
  54.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  55.  
  56.         setPicture("X:/java/pic/fish.jpg"); //TODO: change this too
  57.  
  58.         pack();
  59.  
  60.         repaint();
  61.     }
  62.  
  63.     private void setPicture(Image i) {
  64.         MediaTracker mt = new MediaTracker(this);
  65.         mt.addImage(i, 0);
  66.         try {
  67.             mt.waitForID(0);
  68.         } catch (Exception ex) {
  69.             ex.printStackTrace(System.out);
  70.         }
  71.         pic.setPicture(i);
  72.         pack();
  73.         repaint();
  74.         setVisible(true);
  75.     }
  76.  
  77.     private void setPicture(String fileName) {
  78.         Image i = Toolkit.getDefaultToolkit().getImage(fileName);
  79.         setPicture(i);
  80.     }
  81.  
  82.     private void savePictureto(String absolutePath) {
  83.         try {
  84.             // retrieve image
  85.             BufferedImage bi = toBufferedImage(image);
  86.             File outputfile = new File(absolutePath);
  87.             int index = absolutePath.lastIndexOf(".");
  88.             String end = absolutePath.substring(index + 1);
  89.             //System.out.println("saving: " + end);
  90.             ImageIO.write(bi, end, outputfile);
  91.         } catch (IOException e) {
  92.             e.printStackTrace(System.out);
  93.         }
  94.     }
  95.  
  96.  
  97.  
  98.  
  99.     //here are the methods for conversion
  100.  
  101.     private void convert1() {
  102.         BufferedImage bi = toBufferedImage(image);
  103.         int width = bi.getWidth();
  104.         int height = bi.getHeight();
  105.         int argb, r, g, b, avr, out;
  106.         for (int i = 0; i < width; i++) {
  107.             for (int j = 0; j < height; j++) {
  108.                 argb = bi.getRGB(i, j);
  109.                 r = (argb >> 16) & 0xff;
  110.                 g = (argb >> 8) & 0xff;
  111.                 b = argb & 0xff;
  112.                 avr = (r + g + b) / 3;
  113.                 out = avr + 0x100 * avr + 0x10000 * avr + 0xff000000;
  114.                 bi.setRGB(i, j, out);
  115.             }
  116.         }
  117.         image = Toolkit.getDefaultToolkit().createImage(bi.getSource());
  118.         repaint();
  119.     }
  120.  
  121.     private void convert2() {
  122.         BufferedImage bi = toBufferedImage(image);
  123.         int width = bi.getWidth();
  124.         int height = bi.getHeight();
  125.         int argb, r, g, b, avr, out;
  126.         for (int i = 0; i < width; i++) {
  127.             for (int j = 0; j < height; j++) {
  128.                 argb = bi.getRGB(i, j);
  129.                 r = (argb >> 16) & 0xff;
  130.                 g = (argb >> 8) & 0xff;
  131.                 b = argb & 0xff;
  132.                 avr = Math.round(r * 0.21f + g * 0.71f + b * 0.07f);
  133.                 out = avr + 0x100 * avr + 0x10000 * avr + 0xff000000;
  134.                 bi.setRGB(i, j, out);
  135.             }
  136.         }
  137.         image = Toolkit.getDefaultToolkit().createImage(bi.getSource());
  138.         repaint();
  139.     }
  140.  
  141.     private void convert3() {
  142.         BufferedImage bi = toBufferedImage(image);
  143.         ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
  144.         ColorConvertOp op = new ColorConvertOp(cs, null);
  145.         BufferedImage bi2 = op.filter(bi, null);
  146.         image = Toolkit.getDefaultToolkit().createImage(bi2.getSource());
  147.         repaint();
  148.     }
  149.  
  150.     //here end the methods for conversion
  151.  
  152.  
  153.  
  154.  
  155.     @Override
  156.     public void actionPerformed(ActionEvent ae) {
  157.  
  158.         Object src = ae.getSource();
  159.         if (src == load) {
  160.             if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
  161.                 setPicture(chooser.getSelectedFile().getAbsolutePath());
  162.             }
  163.         } else if (src == save) {
  164.             if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
  165.                 savePictureto(chooser.getSelectedFile().getAbsolutePath());
  166.             }
  167.         } else if (src == convert1) {
  168.             convert1();
  169.         } else if (src == convert2) {
  170.             convert2();
  171.         } else if (src == convert3) {
  172.             convert3();
  173.         }
  174.  
  175.     }
  176.  
  177.     private class PicPanel extends JPanel {
  178.  
  179.         private void setPicture(Image i) {
  180.             image = i;
  181.             Dimension d1 = new Dimension(image.getWidth(null), image.getHeight(null));
  182.             //Dimension d2 = new Dimension(500, 500);
  183.             setPreferredSize(d1);
  184.             setSize(d1);
  185.         }
  186.  
  187.         @Override
  188.         public void paint(Graphics g) {
  189.             super.paint(g);
  190.             g.drawImage(image, 0, 0, null);
  191.         }
  192.     }
  193.  
  194.     public static BufferedImage toBufferedImage(Image image) {
  195.         if (image instanceof BufferedImage) {
  196.             return (BufferedImage) image;
  197.         }
  198.  
  199.         // This code ensures that all the pixels in the image are loaded
  200.         image = new ImageIcon(image).getImage();
  201.  
  202.         // Determine if the image has transparent pixels; for this method's
  203.         // implementation, see Determining If an Image Has Transparent Pixels
  204.         boolean hasAlpha = false;
  205.  
  206.         // Create a buffered image with a format that's compatible with the screen
  207.         BufferedImage bimage = null;
  208.         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
  209.         try {
  210.             // Determine the type of transparency of the new buffered image
  211.             int transparency = Transparency.OPAQUE;
  212.             if (hasAlpha) {
  213.                 transparency = Transparency.BITMASK;
  214.             }
  215.  
  216.             // Create the buffered image
  217.             GraphicsDevice gs = ge.getDefaultScreenDevice();
  218.             GraphicsConfiguration gc = gs.getDefaultConfiguration();
  219.             bimage = gc.createCompatibleImage(
  220.                     image.getWidth(null), image.getHeight(null), transparency);
  221.         } catch (HeadlessException e) {
  222.             // The system does not have a screen
  223.         }
  224.  
  225.         if (bimage == null) {
  226.             // Create a buffered image using the default color model
  227.             int type = BufferedImage.TYPE_INT_RGB;
  228.             if (hasAlpha) {
  229.                 type = BufferedImage.TYPE_INT_ARGB;
  230.             }
  231.             bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
  232.         }
  233.  
  234.         // Copy image to buffered image
  235.         Graphics g = bimage.createGraphics();
  236.  
  237.         // Paint the image onto the buffered image
  238.         g.drawImage(image, 0, 0, null);
  239.         g.dispose();
  240.  
  241.         return bimage;
  242.     }
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement