|
|
|
import java.awt.Image;import java.awt.image.BufferedImage;import java.io.BufferedInputStream;import java.io.FileOutputStream;import java.io.InputStreamReader;import java.io.Reader;import java.net.URL;import java.net.URLConnection;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class TestUrl {public static void main(String[] args) throws Exception {// String s = "http://passport.csdn.net/UserLogin.aspx";// URL url = new URL(s);// URLConnection uc = url.openConnection();// // 打开的连接读取的输入流。// Reader reader = new InputStreamReader(new// BufferedInputStream(url.openStream()));// int c;// while ((c = reader.read()) != -1) {// System.out.print((char) c);// }// reader.close();String url = "http://passport.csdn.net/ShowExPwd.aspx"; // CSDN验证码地址getImg(url);}public static void getImg(String url) throws Exception {URL u = new URL(url);for (int j = 0; j < 10; j++) { //得到10张。如果想做研究,可以循环1W次试试看。。。JpgTset(u, j);}}public static void JpgTset(URL url, int i) throws Exception {Image src = javax.imageio.ImageIO.read(url); // 构造Image对象int wideth = src.getWidth(null); // 得到源图宽int height = src.getHeight(null); // 得到源图长BufferedImage tag = new BufferedImage(wideth / 2, height / 2,BufferedImage.TYPE_INT_RGB);tag.getGraphics().drawImage(src, 0, 0, wideth / 2, height / 2, null); // 绘制缩小后的图FileOutputStream out = new FileOutputStream("c:/image/" + i + ".jpg"); // 输出到文件流JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);encoder.encode(tag); // 近JPEG编码// System.out.print(width+"*"+height);out.close();}} |
|