Results 1 to 6 of 6
- 12-09-2010, 04:18 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
Problem running oustide of NetBeans
Hi Everyone,
I have just finished an application that inverts colors in images (long story, but yes it has a purpose) and it runs fine in NetBeans when I hit F6. However, when I do a clean+build, and try to run the JAR via double-click, it has some interesting issues. Occasionally the JFrame GUI will display blank, other times it will be a static image that does not change when I click on it, mouse over, etc. When the GUI does work, I am able to select images to invert via the button using a JFileChooser. But when I click 'invert', the SwingWorker class that handles inversion instantly finishes, but does not actually perform the inversion.
In case it matters, the method for inverting the images is as follows:
Pull image from file into BufferedImage, invert pixels, write to png file using imageIO.write.
I have no idea why it is not working outside of NetBeans, any help would be much appreciated. Let me know if more information is needed.
Thanks!
- 12-09-2010, 04:41 PM #2
Can you provide an SSCCE that demonstrates this behavior?
- 12-09-2010, 05:03 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
Here's an example that does the exact same thing. First file is the main application, I removed the GUI and everything so now it just works from hardcoded paths
my ImageInverter class:Java Code:public class Main { public static void main(String[] args) { BufferedImage bi = ImageInverter.loadImageFromFile("c:\\HSG.tif"); ImageInverter.invertBufferedImage(bi); java.io.File outfile = new java.io.File("c:\\temp\\HSG.png"); try { javax.imageio.ImageIO.write(bi, "png", outfile); } catch (Exception ignore) { } } }
It's a very quick and dirty solution, but it is working when run through NetBeans. Has the exact same issue once built.Java Code:import java.awt.image.*; import javax.media.jai.*; import java.util.*; public class ImageInverter { //hash table stores known values private static Hashtable ht = new Hashtable(); //invert colors of a buffered image public static void invertBufferedImage(BufferedImage bi) { int width = bi.getWidth(null); int height = bi.getHeight(null); int percent; int clr, newClr; for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { clr = bi.getRGB(i, j); newClr = invertColor(clr); bi.setRGB(i, j, newClr); } } } //fetch image from file public static BufferedImage loadImageFromFile(String fname) { RenderedImage img = JAI.create("fileload", fname); BufferedImage bi = convertRenderedImage(img); img = null; return bi; } //fetch inverted color public static int invertColor(int clr) { //inverts color //format: 0xAARRGGBB long clor = 0; if (ht.containsKey(clr)) { return (Integer)ht.get(clr); } int r, g, b, a; String val = Integer.toHexString(clr); //will not affect alpha values //fetch RGB a = Integer.parseInt(val.substring(0, 2), 16); r = Integer.parseInt(val.substring(2, 4), 16); g = Integer.parseInt(val.substring(4, 6), 16); b = Integer.parseInt(val.substring(6, 8), 16); //invert RGB r = 0xFF - r; g = 0xFF - g; b = 0xFF - b; //rebuild color int val = getColorString(a, r, g, b); clor = Long.parseLong(val, 16); //Workaround for a bug in Integer.parseInt //add color as known ImageInverter.ht.put(clr, (int)clor); return (int)clor; } //parse color int (0xAARRGGBB) into string public static String getColorString(int a, int r, int g, int b) { String val = ""; String aString, bString, gString, rString; aString = fixLen(a); bString = fixLen(b); rString = fixLen(r); gString = fixLen(g); val = aString + rString + gString + bString; return val; } //helper public static String fixLen(int a) { String ret = Integer.toString(a, 16); if (ret.length() == 1) ret = "0" + ret; return ret; } //Found at http://www.jguru.com/faq/view.jsp?EID=114602 public static BufferedImage convertRenderedImage(RenderedImage img) { if (img instanceof BufferedImage) { return (BufferedImage)img; } ColorModel cm = img.getColorModel(); int width = img.getWidth(); int height = img.getHeight(); WritableRaster raster = cm.createCompatibleWritableRaster(width, height); boolean isAlphaPremultiplied = cm.isAlphaPremultiplied(); Hashtable properties = new Hashtable(); String[] keys = img.getPropertyNames(); if (keys!=null) { for (int i = 0; i < keys.length; i++) { properties.put(keys[i], img.getProperty(keys[i])); } } BufferedImage result = new BufferedImage(cm, raster, isAlphaPremultiplied, properties); img.copyData(raster); return result; } }
- 12-10-2010, 02:10 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
I managed to get it to throw an error (still works in netbeans):
Some kind of problem finding JAI maybe?. . .\dist>java -jar SSCCE.jar
Exception in thread "main" java.lang.NoClassDefFoundError: javax/media/j
at sscce.ImageInverter.loadImageFromFile(ImageInverte r.java:37)
at sscce.Main.main(Main.java:18)
Caused by: java.lang.ClassNotFoundException: javax.media.jai.JAI
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
- 12-10-2010, 05:04 PM #5
- 12-13-2010, 02:07 PM #6
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Little problem I'm running with my applet
By Lightcraft in forum Java AppletsReplies: 1Last Post: 04-09-2010, 01:27 AM -
Problem in running Java swing wizard in jre 1.6 while it is running in jre 1.4
By Sanjay Dwivedi in forum AWT / SwingReplies: 0Last Post: 08-26-2009, 01:03 PM -
error while running jar file created from netbeans
By ecstatic in forum NetBeansReplies: 2Last Post: 03-09-2009, 02:36 AM -
Problem in running ant
By sireesha in forum New To JavaReplies: 4Last Post: 05-30-2008, 06:10 PM -
jsp running problem
By bharanikumariyerjava in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 03-29-2008, 10:48 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks