aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Need help ImageToCode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Need help ImageToCode" Watch "Need help ImageToCode" New topic
Author

Need help ImageToCode

Surya R N
Greenhorn

Joined: Mar 24, 2006
Posts: 4
hi all,

i got this code from net, tried to run my winxp SP2 machine. It complied but i am not am to interpret using

java ImageToCode input.gif output g.

Here is the code

package com.applook.tools;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.Vector;
import java.util.*;
import java.net.URL;
import java.net.URLClassLoader;
import java.io.*;

public class ImageToCode {

static String imageSource = "";
static String outputFile = "";
static String prefix = "";
static HashMap storeByColor = new HashMap();
int xOffSet = 0 ;
int yOffSet = 0 ;


public static void main (String args[])
{

if(args.length != 3)
{
System.out.println("Usage: java ImageToCode <sourceimage> <outputfile> <graphicsprefix>");
System.out.println("<sourceimage>\tThe gif image to be processed");
System.out.println("<outputfile>\tThe generated code will be written out to this file in plain text format ");
System.out.println("<graphicsprefix>\tThe variable name of your graphics context i.e. g.");
System.exit(0);
}
else
{
imageSource = args[0];
outputFile = args[1];
prefix = args[2];

Image source = loadImage(imageSource);

processImage(loadImage(imageSource));
System.out.println("completed");
System.exit(0);
}
}

static private void processImage(Image source)
{
BufferedImage buf = null;

if(source != null)
{
buf = toBufferedImage(source);
StringBuffer output = new StringBuffer();
}

if(source != null)
{
int width = buf.getWidth(null);
int height = buf.getHeight(null);
int rgbcol = -99999;
int lastrgb = -99999;
Color color;
int lastX = 0;
int lastY = 0;
int lastSetCol = 0;

for(int y =0 ; y < height; y++)
{
for(int x =0 ; x < width ; x++)
{
if(x==0)
{
lastX = 0;
lastrgb = buf.getRGB(x,y);
color = new Color(lastrgb);
lastSetCol = lastrgb;
lastY = y;
}

rgbcol = buf.getRGB(x,y);
color = new Color(rgbcol);

if(rgbcol != lastrgb )
{
storeLine(String.valueOf(lastSetCol), new LineData(lastX,lastY,x-1,lastY));
lastX = x;
lastY = y;
lastSetCol = rgbcol;

// trap last pixel
if(x == (width -1))
storeLine(String.valueOf(lastSetCol), new LineData(lastX,lastY,x,lastY));

lastrgb = rgbcol;
}

// solid horizontal line
if((x == (width-1)) && lastX == 0 )
storeLine(String.valueOf(lastSetCol), new LineData(lastX,lastY,x,lastY));

}
}
optimiseOutput();
}

}

private void processSolidBlocks()
{

}


static void storeLine(String key, LineData data)
{
if(storeByColor.containsKey(key))
{
Vector temp = (Vector)storeByColor.get(key);
temp.add(data);
storeByColor.put(key,temp);
}
else
{
Vector temp = new Vector();
temp.add(data);
storeByColor.put(key,temp);
}
}

static void optimiseOutput()
{
Iterator iter = storeByColor.keySet().iterator();
LineData data;
Vector vec;
StringBuffer output = new StringBuffer();
Color col;
int tracker = 0;

while (iter.hasNext())
{
Object aKey = iter.next();
col = new Color(Integer.parseInt((String)aKey));
output.append(prefix + "setColor(new Color(" + col.getRGB() + ")); \n");
//System.out.println("colour used: RGB: " + col.getRGB() + " , HEX: " + getHexColor(col.getRed(),col.getGreen(),col.getBlue()) );
tracker++;

vec = (Vector)storeByColor.get(aKey);
Enumeration enum = vec.elements();
while(enum.hasMoreElements())
{
data = (LineData)enum.nextElement();
output.append(prefix + "drawLine(" + String.valueOf(data.x1) + "," + String.valueOf(data.y1) + "," + String.valueOf(data.x2) + "," + String.valueOf(data.y2)+ "); \n" );
tracker++;
}

}
try
{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)));
out.write(output.toString());
out.flush();
out.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}

System.out.println("lines of code generated = " + tracker);
}

static private Image loadImage(String imageName)
{
Image image = null;
try
{
image = Toolkit.getDefaultToolkit().getImage(imageName);

MediaTracker mediaTracker = new MediaTracker(new Frame());
mediaTracker.addImage(image, 0);
mediaTracker.waitForID(0);

}
catch(Exception e)
{
System.out.println("Error loading image + " + imageName + " : " + e.getMessage());
}

return image;

}

// This method returns true if the specified image has transparent pixels
public static boolean hasAlpha(Image image) {

// If buffered image, the color model is readily available
if (image instanceof BufferedImage) {
BufferedImage bimage = (BufferedImage)image;
return bimage.getColorModel().hasAlpha();
}

// Use a pixel grabber to retrieve the image's color model;
// grabbing a single pixel is usually sufficient
PixelGrabber pg = new PixelGrabber(image, 0, 0, 1, 1, false);
try {
pg.grabPixels();
} catch (InterruptedException e) {
}

// Get the image's color model
ColorModel cm = pg.getColorModel();
return cm.hasAlpha();
}


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 e665 Determining If an Image Has Transparent Pixels
boolean hasAlpha = hasAlpha(image);

// 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 (Exception 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;
}



static private String getHexColor(int r,int g,int b)
{
String[] RGB = new String[256];
int k =0;
String[] hex = {"0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"};

for(int i = 0 ; i < 16; i++)
{
for(int j=0;j<16;j++)
{
RGB[k] = hex + hex[j];
k++;
}
}

return "#" + RGB[r] + RGB[g] + RGB;

}


static public class LineData {
int x1;
int y1;
int x2;
int y2;

public LineData(int x1, int y1, int x2, int y2)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}

}

}

i am getting this message :

Exception in thread "main" java.lang.NoClassDefFoundError: ImageToCode (wrong na
me: com/applook/tools/ImageToCode)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
3)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)

hope somebody is there to help me

Regards
[B][I]surya
:cool:


Regards<br /> Surya
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42584
    
  65
Hello Surya-

Welcome to JavaRanch.

On your way in you may have missed that JavaRanch has a policy on display names, and yours does not comply with it - please adjust it accordingly, which you can do right here. Thanks for your prompt attention to this matter.

Enjoy your time here.


Ping & DNS - my free Android networking tools app
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42584
    
  65
As to your question, are you familiar with the concepts of packages and classpaths? Class ImageToCode is in package "com.applook.tools", so it needs to be run by "java com.applook.tools.ImageToCode ...", but the class file also needs to be in a directory hierarchy com/applook/tools, not in the directory from which you execute the command.
Surya R N
Greenhorn

Joined: Mar 24, 2006
Posts: 4
Hello Ulf Dittmer,
i have jre1.5.0_06 , but i couldn't find any folder called applook in com folder. did i have to update any other version.

Surya R N
Greenhorn

Joined: Mar 24, 2006
Posts: 4
Hope anyone will reply
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42584
    
  65
Hello "Surya R"-

Thanks for changing your display name, but you're not quite there. If you read the policy, it states that it must consist of a first name, a space character, and a last name. Please adjust it accordingly.

Also, be aware that PatienceIsAVirtue.

i couldn't find any folder called applook in com folder

That's just the point - there needs to be one. In whichever directory you execute the command I gave before, there must be a com/applook/tools directory, in which the class file resides. It will be easiest to keep the source file in there as well, and to compile it with "javac com/applook/tools/*.java". You really should read up on the concept of packages.
Surya R N
Greenhorn

Joined: Mar 24, 2006
Posts: 4
Hello Ulf Dittmer

Thank you very much for ur replies!!
Sure, i will go thru packages..

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help ImageToCode