Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Friday, January 30, 2009

Java tip: how to split a string with "." (a dot)

String a = "a.jpg";
String str = a.split(".")[0];
This will throw ArrayOutOfBoundException
Why is this?
This is because "." is a reserved character in regular expression, representing any character.
Instead, we should use the following statement:
String str = a.split("\\.")[0];
When the code is compiled, the regular expression is known as "\.", which is what we want it to be

Thursday, January 29, 2009

Java: How to read pixel RGB values from images

BufferedImage img = null;

int w, h;

int pixels[];

try {

img = ImageIO.read(new File("images/secret.jpg"));

w = img.getWidth();

h = img.getHeight();

pixels = new int[w * h];

img.getRGB(0, 0, w, h, pixels, 0, w);

System.out.println(img.getWidth() + " X " + img.getHeight());

System.out.println("R:" + ((pixels[0] >> 16) & 0xff) + " G:" + ((pixels[0] >> 8) & 0xff) + " B:" + (pixels[0] & 0xff));

} catch (IOException e) {

e.printStackTrace();

}


//Based on the sample code, it is really easy to see how to read RGB values from any image files.

Tuesday, November 4, 2008

Convert Human Readable Time Format to Unix Time & Java Milliseconds ( (Java Code))

I just wrote a java program to convert Human Readable Time Format to Unix Time & Java Milliseconds ( (Java Code)).
The code can be accessed here.
I also put it as follows to make it searchable:
To test it, you can convert the milliseconds back to human readable time format here.

//Created on Nov 4, 2008 5:31:17 PM
//Author : Junxian Huang
package iperf;

public class HjxTimeConverter {
public static void main(String[] argv){
System.out.println("Time converter works");
//long milli = HjxTimeConverter.getMilliseconds(1986, 6, 11, 16, 20, 0, 0, -5);
long milli = HjxTimeConverter.getMilliseconds(2008, 11, 4, 18, 22, 0, 0, -5);
//long milli = HjxTimeConverter.getMilliseconds(1970, 2, 2, 2, 15, 1, 1, -5);
System.out.println(milli);
System.out.println(System.currentTimeMillis());
}
public static final int BASE_YEAR = 1970;
public static final int BASE_MONTH = 1;
public static final int BASE_DAY = 1;
public static final int BASE_HOUR = 0;
public static final int BASE_MINUTE = 0;
public static final int BASE_SECOND = 0;
public static final long MILLI_IN_A_SECOND = 1000;
public static final long MILLI_IN_A_MINUTE = 60 * MILLI_IN_A_SECOND;
public static final long MILLI_IN_A_HOUR = 60 * MILLI_IN_A_MINUTE;
public static final long MILLI_IN_A_DAY = 24 * MILLI_IN_A_HOUR;
public static final long MILLI_IN_MONTH[] = {
0, //A pad
31 * MILLI_IN_A_DAY,
28 * MILLI_IN_A_DAY,//For leap year, we add one day
31 * MILLI_IN_A_DAY,
30 * MILLI_IN_A_DAY,
31 * MILLI_IN_A_DAY,
30 * MILLI_IN_A_DAY,
31 * MILLI_IN_A_DAY,
31 * MILLI_IN_A_DAY,
30 * MILLI_IN_A_DAY,
31 * MILLI_IN_A_DAY,
30 * MILLI_IN_A_DAY,
31 * MILLI_IN_A_DAY
};
/**
* @author Junxian Huang
* @Time Created on Nov 4, 2008 5:31:17 PM
* Given time is local time based on the supplied time zone
* @param year
* @param month
* @param day
* @param hour : hour should in 24 hour format, e.g. 8pm should be 20, 12am (midnight) is 0, 12pm (noon) is 12
* @param minute
* @param second
* @param millisecond
* @param time_zone : Number follows GMT for your time zone.
* For Ann Arbor (EST which is GMT -5, time_zone = -5)
* @return Java Milliseconds
* You can check this using the online converter here (http://www.munc.com/jseffects/timeConverter.html)
*/
public static long getMilliseconds(int year, int month, int day, int hour, int minute, int second, int millisecond, int time_zone){

long milli = 0;
int i;
for(i = BASE_YEAR ; i <>
if(i % 4 == 0){
//For leap year
milli += 366 * MILLI_IN_A_DAY;
}else{
milli += 365 * MILLI_IN_A_DAY;
}
}
for(i = BASE_MONTH ; i <>
if(year % 4 == 0 && i == 2){
//For leap year, February
milli += MILLI_IN_MONTH[i] + MILLI_IN_A_DAY;
}else{
milli += MILLI_IN_MONTH[i];
}
}
for(i = BASE_DAY ; i <>
milli += MILLI_IN_A_DAY;
}
for(i = BASE_HOUR ; i <>
milli += MILLI_IN_A_HOUR;
}
for(i = BASE_MINUTE ; i <>
milli += MILLI_IN_A_MINUTE;
}
for(i = BASE_SECOND ; i <>
milli += MILLI_IN_A_SECOND;
}
milli += millisecond;
milli -= time_zone * MILLI_IN_A_HOUR; //We should subtract here!
return milli;
}
}

Saturday, October 25, 2008

How to run your first Java Servlet

Step by step running your first Java Servlet.

Step 1. Install tomcat http server from here. Select the binary based on your operating system. If you are using Windows, just click here. After download, just install it. After installation, you can access your home page at "http://localhost:8080" actually it is the content in "C:\xxx\Tomcat 6.0\webapps\" where "xxx" is the installation directory of your tomcat.

Step 2. Install java. How to do that? Search online, it is pretty simple. After installation, run in the command windows "javac", if it says "'javac.exe' is not recognized as an internal or externa command, operable program or batch file." You have to run "set PATH=%PATH%;C:\Program Files\Java\jdk1.6.0_10\bin" to add the java bin directory to Windows PATH variable. Make sure use your own directory for javac (I'm using jdk 6 update 10).

Step 3. Write your first Servlet
Store the following into "HelloServlet"
"
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloServlet extends HttpServlet {
  
private static final long serialVersionUID = 1L;

public void doGet(HttpServletRequest request,
            HttpServletResponse response)
throws ServletException, IOException {

// Use "request" to read incoming HTTP headers (e.g. cookies)
// and HTML form data (e.g. data the user entered and submitted)
// Use "response" to specify the HTTP response line and headers
// (e.g. specifying the content type, setting cookies).
PrintWriter out = response.getWriter();
out.println("Hello Servlet");

out.close();
// Use "out" to send content to browser
}
}
"

Step 4.  In "cmd" command line, run "javac HelloServlet.java", if everything goes well, you will get a new file "HelloServlet.class"

Step 5. Goto to your home directory for tomcat (mine is "F:\Study\tomcat\Tomcat 6.0\webapps"). Make a new directory called whatever, say "test".
Inside test, make a new directory called "WEB-INF". Go into "WEB-INF", make a new directory called "classes", put "HelloServlet.class" into "classes". 

Step 6. Inside directory "WEB-INF", make a new file called "web.xml".
It can be downloaded here. After you download, change the file name from "web.xml2" to "web.xml", because .xml file can be interpretted by many browsers.

Step 7. Open your browser and type "http://localhost:8080/test/HelloServlet"

If you see a blank page with a line of words "hello servlet", you are all set with your first java servlet.

Actually, I'm new to java servlet too. But I found it really powerful and interesting.

Saturday, October 4, 2008

A small web service "Beetle Word-Phrase Checker"

Have a look at here

I built this for fun, and it is just new born, so it looks very basic and basic.

What I want to do is to provide an online word phrase checker.
That aims to correct your spellings when you are not sure.

For example, when you type "Internattional" Beetle will return a list of words with the first one "international"

For phrase checking, when you type "fall on love", Beetle will return a list of phrases with the first one "fall in love"

Currently, these functions are not finished yet. But I will work on them when I have time (of course not now, because we have a paper deadline on December)

I also want to add a web spider that crawl the web pages to gradually improve its database.

Give me some suggestions if you have some good ideas.
I think this kind of staff is really interesting and coding it is really fun.

By the way, I used java to analyze data, and web pages are written in php or basic html.
I used eclipse 3.4 Ganymede for Java. This is a really cool staff and I really like coding with eclipse. I use SVN and eclipse plugin subversive to do version control, so that I can easily program both on my laptop and on my desktop :D. For php and html, I use notepad++ to "program", I don't like IDE for html and php, because I really like building small blocks into a huge structure.