DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Kas has posted 2 posts at DZone. View Full User Profile

ImageMunger

01.21.2010
| 1675 views |
  • submit to reddit
        

/*
 ImageMunger
 Kas Thomas
 http://asserttrue.blogspot.com/

 Public domain, but please provide attribution if you reuse. Thanks.

 All it does is open an image, display it in a window,
 and run a script file. Usage:

 ImageMunger  myimage.jpg  myscript.js

 The JavaScript file will have two global variables,
 Panel and Image, in scope. They correspond to the
 ImagePanel inner class instance and the BufferedImage
 that's displaying in the panel. That way, your script
 can easily manipulate the image and/or the panel.

 */
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class ImageMunger {

    // This inner class is our canvas. We draw the image on it.
    class ImagePanel extends JComponent {
        
        BufferedImage theImage = null;
        
        ImagePanel( BufferedImage image )           {
            super();
            theImage = image;        
        }
        
        public BufferedImage getImage( ) {
            return theImage;
        }
        
        public void updatePanel() {
            
            invalidate();
            getParent().doLayout();
            repaint();
        }
        
        public void paintComponent( Graphics g ) {
            
            int w = theImage.getWidth( );
            int h = theImage.getHeight( );         
            g.drawImage( theImage, 0,0, w,h, this );
        }
    }  // end ImagePanel inner class
    
    // We need to keep a reference to the ImagePanel:
    public ImagePanel theImagePanel = null;
    
    // Constructor
    public ImageMunger( String [] args ) {
        
        parseArgs( args );
        
        // open image
        BufferedImage image = openImageFile( args[0] );
        
        // create window   
        theImagePanel  = new ImagePanel( image );
        JFrame gMainFrame = new JFrame();
        gMainFrame.setTitle( args[0] );
        gMainFrame.setBounds(50,80,
          image.getWidth( )+10, image.getHeight( )+10);
        gMainFrame.setDefaultCloseOperation(3); // dispose
        gMainFrame.getContentPane().add( theImagePanel );
        gMainFrame.setVisible(true);
        
    }

    ImagePanel getImagePanel( ) {
        
        return theImagePanel;
    }
    
    BufferedImage openImageFile( String fname ) {
        
        BufferedImage img = null;
        
        try {
            File f = new File( fname );
            if (f.exists())          
                img = ImageIO.read(f);           
        } 
        catch (Exception e) {
            showMessage("Trouble reading file.");
            e.printStackTrace();
        }
        
       return img;
    }
    
    public static void runScriptFromFile( String fileName, 
            ScriptEngine engine ) {
        
        try {
            engine.eval(new java.io.FileReader( fileName ));
        }
        catch( Exception exception ) {
            exception.printStackTrace();
            showMessage( exception.getMessage() );
        }
    }
    
    
    public static void showMessage(String s) {
        javax.swing.JOptionPane.showMessageDialog(null, s);
    }
    
    void parseArgs( String[] args ) {
        
        if ( args.length < 2 )
             tellUserHowToUseThisApp( );
    }
    
    void tellUserHowToUseThisApp( ) {
        showMessage( "Supply an image file name and a script file name." ); 
    }
    
    // main()
    public static void main( String[] args ) {

        ImageMunger munger = new ImageMunger( args );
        
        // create a script engine manager & engine
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("JavaScript");
        
        if ( munger.getImagePanel( ).getImage( ) != null )
            engine.put( "Image", munger.getImagePanel( ).getImage( ) );
        
        engine.put("Panel", munger.getImagePanel( ) );
        
        engine.put( "args", args );
        
     // evaluate JavaScript code from file
        runScriptFromFile( args[1], engine );
    }

}