Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Swing / AWT / SWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Paul Clapham
Ron McLeod
Sheriffs:
Jeanne Boyarsky
Liutauras Vilda
Saloon Keepers:
Tim Holloway
Carey Brown
Roland Mueller
Piet Souris
Bartenders:
Forum:
Swing / AWT / SWT
Interesting Image problem
u sankarlal
Greenhorn
Posts: 11
posted 23 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hi all,
How to find the number of colors in a particular image, (image is in file format.) ?
Help me to proceed.
thanks
Nathan Pruett
Bartender
Posts: 4121
I like...
posted 23 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I only had to modify one of my programs a little bit to get it to do this...
If you need any further explanations, please post...
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.filechooser.*; import java.io.File; import java.util.HashMap; import java.awt.image.PixelGrabber; public class ColorCounter extends JFrame { private JButton imagePanel; private JTextField numText; public ColorCounter() { super( "Color Counter" ); imagePanel = new JButton(); JScrollPane js = new JScrollPane( imagePanel ); getContentPane().add( js, BorderLayout.CENTER ); numText = new JTextField( 18 ); numText.setEditable( false ); JPanel p = new JPanel(); p.add( numText ); getContentPane().add( p, BorderLayout.SOUTH ); } protected void frameInit() { super.frameInit(); JMenuBar menuBar = new JMenuBar(); JMenu file = new JMenu( "File" ); JMenuItem open = new JMenuItem( "Open" ); open.addActionListener( new OpenActivated() ); file.add( open ); menuBar.add( file ); setJMenuBar( menuBar ); } public static void main( String args[] ) { ColorCounter cc = new ColorCounter(); cc.addWindowListener( cc.new ExitActivated() ); cc.setSize( 300, 300 ); cc.setVisible( true ); } private class ExitActivated extends WindowAdapter { public void windowClosing( WindowEvent event ) { System.exit( 0 ); } } private class OpenActivated implements ActionListener { public void actionPerformed( ActionEvent event ) { JFileChooser fileChooser = new JFileChooser(); ExtensionFilter filter = new ExtensionFilter(); filter.addExtension( "gif" ); filter.addExtension( "jpg" ); filter.addExtension( "jpeg" ); filter.setDescription( "Image Files" ); fileChooser.addChoosableFileFilter( filter ); fileChooser.setFileFilter( filter ); int selected = fileChooser.showOpenDialog( imagePanel ); switch( selected ) { case JFileChooser.CANCEL_OPTION: break; case JFileChooser.APPROVE_OPTION: File f = new File( fileChooser.getCurrentDirectory(), fileChooser.getName( fileChooser.getSelectedFile() ) ); String fileName = f.getAbsolutePath(); Image i = Toolkit.getDefaultToolkit().getImage( fileName ); MediaTracker mt = new MediaTracker( imagePanel ); mt.addImage( i, 0 ); try { mt.waitForAll(); } catch( InterruptedException exception ) { } ImageIcon ii = new ImageIcon( i ); imagePanel.setIcon( ii ); ColorCounter.this.validate(); HashMap colorHolder = new HashMap(); int pixels[] = new int[ ii.getIconWidth() * ii.getIconHeight() ]; PixelGrabber pg = new PixelGrabber( i, 0, 0, ii.getIconWidth(), ii.getIconHeight(), pixels, 0, ii.getIconWidth() ); try { pg.grabPixels(); } catch( InterruptedException exception ) { } for( int j = 0; j < pixels.length; j++ ) { colorHolder.put( Integer.toString( pixels[ j ] ), new Integer( pixels[ j ] ) ); } numText.setText( Integer.toString( colorHolder.size() ) ); break; default: break; } } } private class ExtensionFilter extends FileFilter { private String description; private HashMap filters = null; public ExtensionFilter() { filters = new HashMap(); setDescription( "undefined" ); } public void addExtension( String extension ) { filters.put( extension.toLowerCase(), this ); } public String getDescription() { return( description ); } public void setDescription( String description ) { this.description = description; } public boolean accept( File f ) { if( f != null ) { if( f.isDirectory() ) { return( true ); } String extension = getExtension( f ); if( extension != null && filters.get( extension ) != null ) { return( true ); } } return( false ); } protected String getExtension( File f ) { if( f != null ) { String fileName = f.getName(); int i = fileName.lastIndexOf( '.' ); if( i > 0 && i < fileName.length() - 1 ) { return( fileName.substring( i + 1).toLowerCase() ); } } return( null ); } } }
HTH,
-Nate
-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
converting png to tiff and character recognition with tesseract
Loading image file
Unsupported Color Conversion Request
Easy one
Getting rid of Java Icon on the upper left hand corner
More...