Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gTest;
- import java.awt.*;
- import java.awt.color.*;
- import java.awt.event.*;
- import java.awt.image.*;
- import java.io.*;
- import javax.imageio.*;
- import javax.swing.*;
- public class PictureLoader extends JFrame implements ActionListener {
- private Image image;
- private JFileChooser chooser = new JFileChooser();
- private PicPanel pic = new PicPanel();
- private JButton load = new JButton("Load"), save = new JButton("Save"),
- convert1 = new JButton("Con1"), convert2 = new JButton("Con2"),
- convert3 = new JButton("Con3");
- public static void main(String[] args) {
- new PictureLoader().setVisible(true);
- }
- @SuppressWarnings("LeakingThisInConstructor")
- private PictureLoader() {
- super("PictureLoader");
- setLayout(new BorderLayout());
- setLocation(20, 20);
- chooser.setSelectedFile(new File("X:/java/pic/brb.jpg")); //TODO: change this
- Container flow = new Container();
- flow.setLayout(new FlowLayout(FlowLayout.CENTER));
- load.addActionListener(this);
- flow.add(load);
- save.addActionListener(this);
- flow.add(save);
- convert1.addActionListener(this);
- flow.add(convert1);
- convert2.addActionListener(this);
- flow.add(convert2);
- convert3.addActionListener(this);
- flow.add(convert3);
- add(flow, BorderLayout.NORTH);
- add(new JScrollPane(pic), BorderLayout.CENTER);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- setPicture("X:/java/pic/fish.jpg"); //TODO: change this too
- pack();
- repaint();
- }
- private void setPicture(Image i) {
- MediaTracker mt = new MediaTracker(this);
- mt.addImage(i, 0);
- try {
- mt.waitForID(0);
- } catch (Exception ex) {
- ex.printStackTrace(System.out);
- }
- pic.setPicture(i);
- pack();
- repaint();
- setVisible(true);
- }
- private void setPicture(String fileName) {
- Image i = Toolkit.getDefaultToolkit().getImage(fileName);
- setPicture(i);
- }
- private void savePictureto(String absolutePath) {
- try {
- // retrieve image
- BufferedImage bi = toBufferedImage(image);
- File outputfile = new File(absolutePath);
- int index = absolutePath.lastIndexOf(".");
- String end = absolutePath.substring(index + 1);
- //System.out.println("saving: " + end);
- ImageIO.write(bi, end, outputfile);
- } catch (IOException e) {
- e.printStackTrace(System.out);
- }
- }
- //here are the methods for conversion
- private void convert1() {
- BufferedImage bi = toBufferedImage(image);
- int width = bi.getWidth();
- int height = bi.getHeight();
- int argb, r, g, b, avr, out;
- for (int i = 0; i < width; i++) {
- for (int j = 0; j < height; j++) {
- argb = bi.getRGB(i, j);
- r = (argb >> 16) & 0xff;
- g = (argb >> 8) & 0xff;
- b = argb & 0xff;
- avr = (r + g + b) / 3;
- out = avr + 0x100 * avr + 0x10000 * avr + 0xff000000;
- bi.setRGB(i, j, out);
- }
- }
- image = Toolkit.getDefaultToolkit().createImage(bi.getSource());
- repaint();
- }
- private void convert2() {
- BufferedImage bi = toBufferedImage(image);
- int width = bi.getWidth();
- int height = bi.getHeight();
- int argb, r, g, b, avr, out;
- for (int i = 0; i < width; i++) {
- for (int j = 0; j < height; j++) {
- argb = bi.getRGB(i, j);
- r = (argb >> 16) & 0xff;
- g = (argb >> 8) & 0xff;
- b = argb & 0xff;
- avr = Math.round(r * 0.21f + g * 0.71f + b * 0.07f);
- out = avr + 0x100 * avr + 0x10000 * avr + 0xff000000;
- bi.setRGB(i, j, out);
- }
- }
- image = Toolkit.getDefaultToolkit().createImage(bi.getSource());
- repaint();
- }
- private void convert3() {
- BufferedImage bi = toBufferedImage(image);
- ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
- ColorConvertOp op = new ColorConvertOp(cs, null);
- BufferedImage bi2 = op.filter(bi, null);
- image = Toolkit.getDefaultToolkit().createImage(bi2.getSource());
- repaint();
- }
- //here end the methods for conversion
- @Override
- public void actionPerformed(ActionEvent ae) {
- Object src = ae.getSource();
- if (src == load) {
- if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
- setPicture(chooser.getSelectedFile().getAbsolutePath());
- }
- } else if (src == save) {
- if (chooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {
- savePictureto(chooser.getSelectedFile().getAbsolutePath());
- }
- } else if (src == convert1) {
- convert1();
- } else if (src == convert2) {
- convert2();
- } else if (src == convert3) {
- convert3();
- }
- }
- private class PicPanel extends JPanel {
- private void setPicture(Image i) {
- image = i;
- Dimension d1 = new Dimension(image.getWidth(null), image.getHeight(null));
- //Dimension d2 = new Dimension(500, 500);
- setPreferredSize(d1);
- setSize(d1);
- }
- @Override
- public void paint(Graphics g) {
- super.paint(g);
- g.drawImage(image, 0, 0, null);
- }
- }
- public static BufferedImage toBufferedImage(Image image) {
- if (image instanceof BufferedImage) {
- return (BufferedImage) image;
- }
- // This code ensures that all the pixels in the image are loaded
- image = new ImageIcon(image).getImage();
- // Determine if the image has transparent pixels; for this method's
- // implementation, see Determining If an Image Has Transparent Pixels
- boolean hasAlpha = false;
- // Create a buffered image with a format that's compatible with the screen
- BufferedImage bimage = null;
- GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
- try {
- // Determine the type of transparency of the new buffered image
- int transparency = Transparency.OPAQUE;
- if (hasAlpha) {
- transparency = Transparency.BITMASK;
- }
- // Create the buffered image
- GraphicsDevice gs = ge.getDefaultScreenDevice();
- GraphicsConfiguration gc = gs.getDefaultConfiguration();
- bimage = gc.createCompatibleImage(
- image.getWidth(null), image.getHeight(null), transparency);
- } catch (HeadlessException e) {
- // The system does not have a screen
- }
- if (bimage == null) {
- // Create a buffered image using the default color model
- int type = BufferedImage.TYPE_INT_RGB;
- if (hasAlpha) {
- type = BufferedImage.TYPE_INT_ARGB;
- }
- bimage = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
- }
- // Copy image to buffered image
- Graphics g = bimage.createGraphics();
- // Paint the image onto the buffered image
- g.drawImage(image, 0, 0, null);
- g.dispose();
- return bimage;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement