解析验证码OCR
2016-04-25 00:00
453 查看
原图
处理后的图片
package cn.pwntcha;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.io.IOUtils;
public class ImagePreProcess {
public static int isBlack(int colorInt) {
Color color = new Color(colorInt);
if (color.getRed() + color.getGreen() + color.getBlue() <= 100) {
return 1;
}
return 0;
}
public static int isWhite(int colorInt) {
Color color = new Color(colorInt);
if (color.getRed() + color.getGreen() + color.getBlue() > 100) {
return 1;
}
return 0;
}
public static BufferedImage removeBackgroud(String picFile)
throws Exception {
BufferedImage img = ImageIO.read(new File(picFile));
int width = img.getWidth();
int height = img.getHeight();
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
if (isWhite(img.getRGB(x, y)) == 1) {
img.setRGB(x, y, Color.WHITE.getRGB());
} else {
img.setRGB(x, y, Color.BLACK.getRGB());
}
}
}
return img;
}
public static List<BufferedImage> splitImage(BufferedImage img)
throws Exception {
List<BufferedImage> subImgs = new ArrayList<BufferedImage>();
subImgs.add(img.getSubimage(10, 6, 8, 10));
subImgs.add(img.getSubimage(19, 6, 8, 10));
subImgs.add(img.getSubimage(28, 6, 8, 10));
subImgs.add(img.getSubimage(37, 6, 8, 10));
return subImgs;
}
public static Map<BufferedImage, String> loadTrainData() throws Exception {
Map<BufferedImage, String> map = new HashMap<BufferedImage, String>();
File dir = new File("train");
File[] files = dir.listFiles();
for (File file : files) {
map.put(ImageIO.read(file), file.getName().charAt(0) + "");
}
return map;
}
public static String getSingleCharOcr(BufferedImage img,
Map<BufferedImage, String> map) {
String result = "";
int width = img.getWidth();
int height = img.getHeight();
int min = width * height;
for (BufferedImage bi : map.keySet()) {
int count = 0;
Label1: for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
if (isWhite(img.getRGB(x, y)) != isWhite(bi.getRGB(x, y))) {
count++;
if (count >= min)
break Label1;
}
}
}
if (count < min) {
min = count;
result = map.get(bi);
}
}
return result;
}
public static String getAllOcr(String file) throws Exception {
BufferedImage img = removeBackgroud(file);
List<BufferedImage>
3ff0
; listImg = splitImage(img);
Map<BufferedImage, String> map = loadTrainData();
String result = "";
for (BufferedImage bi : listImg) {
result += getSingleCharOcr(bi, map);
}
ImageIO.write(img, "JPG", new File("result\\"+result+".jpg"));
return result;
}
public static void downloadImage() {
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(
"http://www.puke888.com/authimg.php");
for (int i = 0; i < 30; i++) {
try {
// 执行getMethod
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
// 读取内容
String picName = "img\\" + i + ".jpg";
InputStream inputStream = getMethod.getResponseBodyAsStream();
OutputStream outStream = new FileOutputStream(picName);
IOUtils.copy(inputStream, outStream);
outStream.close();
System.out.println(i+"OK!");
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放连接
getMethod.releaseConnection();
}
}
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
for (int i = 0; i < 30; ++i) {
String text = getAllOcr("img\\" + i + ".jpg");
System.out.println(i + ".jpg = " + text);
}
}
}
处理后的图片
package cn.pwntcha;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.io.IOUtils;
public class ImagePreProcess {
public static int isBlack(int colorInt) {
Color color = new Color(colorInt);
if (color.getRed() + color.getGreen() + color.getBlue() <= 100) {
return 1;
}
return 0;
}
public static int isWhite(int colorInt) {
Color color = new Color(colorInt);
if (color.getRed() + color.getGreen() + color.getBlue() > 100) {
return 1;
}
return 0;
}
public static BufferedImage removeBackgroud(String picFile)
throws Exception {
BufferedImage img = ImageIO.read(new File(picFile));
int width = img.getWidth();
int height = img.getHeight();
for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
if (isWhite(img.getRGB(x, y)) == 1) {
img.setRGB(x, y, Color.WHITE.getRGB());
} else {
img.setRGB(x, y, Color.BLACK.getRGB());
}
}
}
return img;
}
public static List<BufferedImage> splitImage(BufferedImage img)
throws Exception {
List<BufferedImage> subImgs = new ArrayList<BufferedImage>();
subImgs.add(img.getSubimage(10, 6, 8, 10));
subImgs.add(img.getSubimage(19, 6, 8, 10));
subImgs.add(img.getSubimage(28, 6, 8, 10));
subImgs.add(img.getSubimage(37, 6, 8, 10));
return subImgs;
}
public static Map<BufferedImage, String> loadTrainData() throws Exception {
Map<BufferedImage, String> map = new HashMap<BufferedImage, String>();
File dir = new File("train");
File[] files = dir.listFiles();
for (File file : files) {
map.put(ImageIO.read(file), file.getName().charAt(0) + "");
}
return map;
}
public static String getSingleCharOcr(BufferedImage img,
Map<BufferedImage, String> map) {
String result = "";
int width = img.getWidth();
int height = img.getHeight();
int min = width * height;
for (BufferedImage bi : map.keySet()) {
int count = 0;
Label1: for (int x = 0; x < width; ++x) {
for (int y = 0; y < height; ++y) {
if (isWhite(img.getRGB(x, y)) != isWhite(bi.getRGB(x, y))) {
count++;
if (count >= min)
break Label1;
}
}
}
if (count < min) {
min = count;
result = map.get(bi);
}
}
return result;
}
public static String getAllOcr(String file) throws Exception {
BufferedImage img = removeBackgroud(file);
List<BufferedImage>
3ff0
; listImg = splitImage(img);
Map<BufferedImage, String> map = loadTrainData();
String result = "";
for (BufferedImage bi : listImg) {
result += getSingleCharOcr(bi, map);
}
ImageIO.write(img, "JPG", new File("result\\"+result+".jpg"));
return result;
}
public static void downloadImage() {
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(
"http://www.puke888.com/authimg.php");
for (int i = 0; i < 30; i++) {
try {
// 执行getMethod
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: "
+ getMethod.getStatusLine());
}
// 读取内容
String picName = "img\\" + i + ".jpg";
InputStream inputStream = getMethod.getResponseBodyAsStream();
OutputStream outStream = new FileOutputStream(picName);
IOUtils.copy(inputStream, outStream);
outStream.close();
System.out.println(i+"OK!");
} catch (Exception e) {
e.printStackTrace();
} finally {
// 释放连接
getMethod.releaseConnection();
}
}
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
for (int i = 0; i < 30; ++i) {
String text = getAllOcr("img\\" + i + ".jpg");
System.out.println(i + ".jpg = " + text);
}
}
}
相关文章推荐
- json字符串转换为JSONObject和JSONArray .
- 三大框架struts+spring+ibatis
- eclipse不自动弹出提示(alt+/快捷键失效
- 代码摘录
- json2.js使用参考
- css常用属性知识点
- 计算实际上班时间
- jquery
- 聊天记录解密
- Spring注解的使用和区别:@Component、@Service、@Repository、@Controller
- jquery学习
- web程序验证码刷不出来原因
- 淘宝开源Web服务器Tengine基本安装步骤
- mysql低优先级
- 网络爬虫返回json处理数据
- 百度地图引用
- jQuery验证
- 文件下载兼容各种浏览器
- java19天
- java第7天