Skip to content

Instantly share code, notes, and snippets.

@mzule
Created March 25, 2014 03:41
Show Gist options
  • Save mzule/9754852 to your computer and use it in GitHub Desktop.
Save mzule/9754852 to your computer and use it in GitHub Desktop.
read png file widrh and height
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
public class Main {
public static void main(String[] args) throws Exception {
InputStream is;
URL url = new URL("http://dribbble.com/system/users/1/screenshots/21603/shot_1274474082.png");
URLConnection conn = url.openConnection();
long start = System.currentTimeMillis();
is = conn.getInputStream();
long cost = System.currentTimeMillis() - start;
System.out.println(cost+"ms");
int w = 0;
int h = 0;
for (int i = 0; i < 24; i++) {
int v = is.read();
switch (i) {
// image width chunk
case 16:
w += v * 0x1000000;
break;
case 17:
w += v * 0x10000;
break;
case 18:
w += v * 0x100;
break;
case 19:
w += v;
break;
// image height chunk
case 20:
h += v * 0x1000000;
break;
case 21:
h += v * 0x10000;
break;
case 22:
h += v * 0x100;
break;
case 23:
h += v;
break;
default:
break;
}
}
System.out.println(w + "*" + h);
is.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment