Wednesday, February 28, 2007

The Perils of javaSchools

joel on software

read the article given above.

you can also hear the yes and nos and more in this javaranch article :

what i derive from this article is

'dont start with java. start from a concept rich (and hard) programming languages like lisp, c so that you would have trained yourslef to think abstractly, plus have become a techie once you start your professional life as an employee or a technical manager'

I guess a nice article.

Monday, February 19, 2007

export image from flash to java

Using this code you can recieve the pixel info from a flash client as a http request and paint it into a jpeg in the server.

You can get the flash code and the php version of the following code from this link.



public void saveImage(HttpServletRequest request)
{
try
{
int w = Integer.parseInt(request.getParameter("width"));
int h = Integer.parseInt(request.getParameter("height"));

String fileName = "image.jpg";

BufferedImage imageBuffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
int diagramId = Integer.parseInt(request.getParameter("diagramId"));

for(int i=0;i<w;i++)
{
for(int j=0;j<h;j++)
{
imageBuffer.setRGB(i,j,0xffffff);
}
}

int rows = 0;
int cols = 0;

String rowText="";
int countcount=0;
for(rows = 0; rows < h; rows++)
{

rowText = request.getParameter( "px"+rows ); //recieving the row of the image thru http
StringTokenizer st=null;

st = new StringTokenizer(rowText, ",", true) ; // separating by comma

String firstToken="";
String secondToken="";

String rgbText;
int rgb=0;

cols=-1;
while(st.hasMoreTokens())
{

secondToken = st.nextToken();

if(firstToken.equals("")&&secondToken.equals(",")) // the first ever token
{
cols++;
rgbText="ffffff";
rgbText = getCorrectedColor(rgbText);

int red = getRedInInt(rgbText);
int blue = getBlueInInt(rgbText);
int green = getGreenInInt(rgbText);

rgb = ( red << 16 | green << 8 | blue);

try
{
imageBuffer.setRGB(cols,rows,rgb);
}
catch(Exception e)
{
System.out.println(e);
}
}

else if(firstToken.equals(",")&&secondToken.equals(",")) //there is no value between the commas
{

cols++;
rgbText="ffffff"; // set the color as white
rgbText = getCorrectedColor(rgbText);

int red = getRedInInt(rgbText);
int blue = getBlueInInt(rgbText);
int green = getGreenInInt(rgbText);

rgb = ( red << 16 | green << 8 | blue);

try
{
imageBuffer.setRGB(cols,rows,rgb);
}
catch(Exception e)
{
System.out.println(e);
}
}

else if(firstToken.equals(",")) // there is some color value
{
cols++;
rgbText=secondToken;
rgbText = getCorrectedColor(rgbText);

int red = getRedInInt(rgbText);
int blue = getBlueInInt(rgbText);
int green = getGreenInInt(rgbText);

rgb = ( red << 16 | green << 8 | blue);

try
{
imageBuffer.setRGB(cols,rows,rgb);
}
catch(Exception e)
{
System.out.println(e);
}
}

firstToken=secondToken;
}
}


ImageIO.write(imageBuffer, fileType, new File(fileName));
ImageIO.write(imageBuffer, "jpg", new File(fileName));
}
}
catch(Exception e)
{
System.out.println("Exception in writing image file saveImage(): "); e.printStackTrace();
}
}

public int getRedInInt(String color1)
{
StringBuilder color=new StringBuilder(color1);
StringBuilder r= new StringBuilder(new Character(color.charAt(2)).toString());
r.append(new Character(color.charAt(3)).toString());
return Integer.decode("0x"+r);
}

public int getGreenInInt(String color1)
{
StringBuilder color=new StringBuilder(color1);
StringBuilder g= new StringBuilder(new Character(color.charAt(4)).toString());
g.append(new Character(color.charAt(5)).toString());
return Integer.decode("0x"+g);
}

public int getBlueInInt(String color1)
{
StringBuilder color=new StringBuilder(color1);
StringBuilder b= new StringBuilder(new Character(color.charAt(6)).toString());
b.append(new Character(color.charAt(7)).toString());
return Integer.decode("0x"+b);
}

public String getCorrectedColor(String color)
{
String newColor="0x";
int shortcome = 6-color.length();
//pad with zeros
for(int i=0;i<shortcome;i++)
{
newColor += "0";
}
//the color value always starts at the index 2
for(int i=0;i<color.length();i++)
{
newColor += new Character(color.charAt(i)).toString();
}
return newColor;
}