JPEG文件的读写(一)

来源:互联网 发布:人工神经网络算法代码 编辑:程序博客网 时间:2024/06/08 14:47

闲着没事,我做一个图片数据识别器,就是通过读入一个数据字图片,就可以识别其中的数字了.
最实这个想法是发现一些网站登录和注册的验证码非常规范,而且全是数字,感觉这样很不安全.
于是我打算做一个自动登录机或自动注册机,原理是这样的:
1).利用httpRequest类包(java写的,网上有很多开源代码),写了一个读取某特定网站的验证码的类.
2).将读取数据直接转换数字识别器,识别出验证码.
3).获取验证码,利用原来建立的连接,发送登录或注册的POST请求.
4).等待网站被自动登录和注册的"好消息"

目前我已经解决了1),3), 数字图片识别是基于BMP的,但是大部分网站的验证码图片是JPG的,所以这就涉及到JPG到BMP转换过程.
我弄了一天左右,看了部分SUN公式的JIMI开源代码,妈的,有关JPG的方面的代码很模糊,感觉很不好用,从它的源代码中我找到
这样一段类似代码:

  1. byte[] data = getDocument(); //从网站中获取验证码数据
  2. Image image = Toolkit.getDefaultToolkit().createImage(data);

后来发现Image类是一个Abstract类, 里面的方法并没有直接获取原数据功能,我打算通过它来获取Raster类,但是要获取Raster类,
得有BufferedImage类才行, 于是我找到BufferedImage类, 看看它的构造函数有以下几个:

  1. /*
  2.  * Constructs a new BufferedImage with a 
  3.  * specified ColorModel and Raster.
  4.  *
  5.  * */
  6. BufferedImage(ColorModel cm, WritableRaster raster, boolean isRasterPremultiplied, Hashtable<?,?> properties);
  7. /*
  8.  *  Constructs a BufferedImage of one of the predefined image types.
  9.  */        
  10. BufferedImage(int width, int height, int imageType);
  11. /*
  12.  * Constructs a BufferedImage of one of the predefined
  13.  * image types: TYPE_BYTE_BINARY or TYPE_BYTE_INDEXED.
  14.  */ 
  15. BufferedImage(int width, int height, int imageType, IndexColorModel cm);

里面都是一些重新构造BufferedImage实例, 如果要实现这个,就必须弄懂JPG图形格式, 可是我现在还没有研究.
看来想从网页上找一些开源的东西,感觉这条路有点走不通了.这个还得自己写写,自己研究了BMPDecoder和BMPEncoder
感觉都很简单的,照葫芦画瓢应该不成问题。
这些均是java解决方案.我原本已经有了一套C语言解决方案,就是实现JPG图片识别功能,前几天我将这两套方案进行了整合,
就是在java程序中调用:

  1. String[] cmds[2] = new String[2];
  2. cmds[0] = "E:/My Document/Visual Studio 2005/Projects/FreeImage/Debug/UseFreeImageLib.exe";
  3. cmds[1] = "D:/eclipse/workspace/ictclas4j_0.9.1/javaSocket/vercode.jpg";
  4. Process proc = Runtime.getRuntime().exec(cmds);

在程序运行过程中,会出现莫名假死的现象.
程序代码调用逻辑如下:

  1. .....
  2. public static void main(String[] args) {
  3.     client.setUserName(args[0]);
  4.     client.setPassword(args[1]);
  5.     int count = 0;
  6.     while (true) {
  7.     String vercode = client.getVercodeFromUrl();
  8.     String data = "user.userName="+client.getUserName()
  9.             + "&user.password=" + client.getPassword()
  10.             + "&vercode=" + vercode;
  11.     System.out.println("vercode = " + vercode);
  12.     client.setConnection("http://172.16.3.89/login.do");
  13.     client.sendRequestByPost(data);
  14.     if (count++ == 1000break;
  15.     System.out.println(count + " round...");
  16.     }
  17. }
  18. public String getVercodeFromUrl() throws IOException, InterruptedException {
  19.     setConnection("http://172.16.3.89/authImg");
  20.     byte[] data = getDocument();
  21.     saveData(data, "vercode.jpg");
  22.     String[] cmds = new String[2];
  23.     cmds[0] = "E:/My Document/Visual Studio 2005/Projects/FreeImage/Debug/UseFreeImageLib.exe";
  24.     cmds[1] = "D:/eclipse/workspace/ictclas4j_0.9.1/javaSockets/vercode.jpg";
  25.     Process proc = Runtime.getRuntime().exec(cmds);
  26.         // wait until the subprocess proc terminate success.
  27.     Thread.sleep(50);
  28.     DataInputStream in = new DataInputStream(proc.getInputStream());
  29.     Thread.sleep(50);
  30.     int len = in.available();
  31.     byte[] vercode_bytes = new byte[len];
  32.     while (true) {
  33.         int read_len = in.read(vercode_bytes);
  34.         if (read_len == -1break;
  35.     }
  36.     String vercode = new String(vercode_bytes);
  37.     proc.destroy();
  38.     return vercode;
  39. }

目前我还不知道这个原因是什么? 希望读者中有高手赐教,在此谢谢了.

我下一步方案就是把原来我用C语言编写的图片识别用Java来实现,同发送接收模块同时进行.这样可能会更好地工作.目前我已经完成数据识别部分(基于24bit,32bit的BMP图片格式), 目前最大的困难就是JPEG文件的读写了,希望很快能够搞定.











 

<script type="text/javascript"><!--google_ad_client = "pub-1307274723602242";/* 728x15, 创建于 09-3-6 */google_ad_slot = "3124229201";google_ad_width = 728;google_ad_height = 15;// --></script><script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>

原创粉丝点击