图片解析

/**
 * 测试验证码解析
 * @author hanmanyi
 *
 */
public class TestOCR {
	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		String path = "E:\\ZGCCZJworksp\\HMYTicket\\image\\passCode.jpg";
//		String path = "E:\\ZGCCZJworksp\\HMYTicket\\image\\untitled.bmp";
		
//		for (int i = 0; i < 100; i++) {
			try {
				
				filter(path);
//				String valCode = new OCR().recognizeText("C:\\Program Files\\Tesseract-OCR\\",new File(path), "jpg");
//				String valCode = new OCR().recognizeText("C:\\Program Files\\Tesseract-OCR\\",new File(path), "bmp");
//				valCode = valCode.replace("\r\n\r\n", "");
//				System.out.println(i+"|"+valCode);
//			} catch (IOException e) {
//				e.printStackTrace();
			} 
			catch (Exception e) {
				e.printStackTrace();
			}
//		}
	}
	/**
	 * 过滤掉图片中的直线和孤立点
	 * 用被使用最多的颜色(即背景色)代替
	 * 
	 */
	public static void filter(String path) {
		InputStream instream;
		OutputStream out;
		String newpath = "E:\\ZGCCZJworksp\\HMYTicket\\image\\new_passCode.jpg";
		try {
			BufferedImage imgOrg = ImageIO.read(new File(path));
			
			instream = convert(imgOrg);
//			instream = new FileInputStream(new File(path));
			out = new FileOutputStream(new File(newpath));
			int byteread = 0;
			byte[] tmp = new byte[1];
			while ((byteread = instream.read(tmp)) != -1) {
				out.write(tmp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 用被使用最多的颜色(即背景色)代替
	 * @param imgOrg
	 */
	private static InputStream convert(BufferedImage img) {
		InputStream is = null;
		int width = img.getWidth();
		int height = img.getHeight();
		for (int j = 0; j < height; j++) {
			for (int i = 0; i < width; i++) {
				Long RGB = Long.parseLong(""+img.getRGB(i, j));
				Map<Long, Integer> map = getMaxColor(img,i,j);
				Integer c = map.get(RGB);
				
				System.out.println(RGB);
				System.out.println(map);
				if (c != null && c >1) {
					img.setRGB(i, j, 0xFF0000);
				}
			}
		}
		img.flush();
		ImageOutputStream imOut;
		ByteArrayOutputStream bs = new ByteArrayOutputStream();
		try {
			imOut = ImageIO.createImageOutputStream(bs);
			ImageIO.write(img, "jpg",imOut);
			is= new ByteArrayInputStream(bs.toByteArray());
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			
			
		return is;
	}
	
	/**
	 * 周围使用最多的颜色
	 * @param imgOrg
	 */
	private static Map<Long, Integer> getMaxColor(BufferedImage img,int x,int y){
		int width = img.getWidth();
		int height = img.getHeight();
		int range = 2;
		
		Map<Long, Integer> map = new HashMap<Long, Integer>();
		for (int j = y-range < 0?0:y-range; j < height && j<=y+range ; j++) {
			for (int i = x-range < 0?0:x-range; i < width && i<=x+range; i++) {
				long RGB = img.getRGB(i, j);
				Integer c = map.get(RGB);
				if (c == null) {
					map.put(RGB, 1);
				} else {
					map.put(RGB, c + 1);
				}
			}
		}
		return map;
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值