tess4j3.2.1识别中英文

tess4j3.2.1识别中英文1.测试代码publicclassTesseractExample{ publicstaticvoidmain(String[]args)throwsException{ //1.本地 //FileimageFile=newFile("C:\\wangl\\eurotext.tif"); //InputStreamis=…

大家好,又见面了,我是你们的朋友全栈君。

1. 测试代码

public class TesseractExample {
	 public static void main(String[] args) throws Exception {
	    //1.本地
	    //File imageFile = new File("C:\\wangl\\eurotext.tif");
		//InputStream is = new FileInputStream(imageFile);; 
		//2.url http
	    //InputStream is = downLoadFromUrl("http://s1.sinaimg.cn/mw690/003bsgbmgy6R6ekxkl2e0"); 
		//3.url https
		InputStream is = downLoadFromUrl("https://login.10333.com/dotoyo//captchacode");
		    
		BufferedImage bi = ImageIO.read(is);
	    ITesseract instance = new Tesseract();  // JNA Interface Mapping
        //设置datapath
	    instance.setDatapath("C:\\wangl\\soft\\tess4j\\tessdata");  
        //设置语言包
        instance.setLanguage("eng+chi_sim");
	    try {
	        String result = instance.doOCR(bi);
	        System.out.println(result);
	    } catch (TesseractException e) {
	        System.err.println(e.getMessage());
	    }
	    if(is != null){
	       is.close();
	    }
	 }
	 
	 public static InputStream downLoadFromUrl(String urlStr) throws Exception{
             //指定证书文件
		System.setProperty("javax.net.ssl.trustStore","C:\\wangl\\soft\\Java\\jdk1.7.0_79\\jre\\lib\\security\\jssecacerts");
	        URL url = new URL(urlStr);  
	        HttpURLConnection conn = (HttpURLConnection)url.openConnection();  
	        //信任所有的https证书
	        trustAllHttpsCertificates();
	        HttpsURLConnection.setDefaultHostnameVerifier(hv);
	        //设置超时间为3秒
	        conn.setConnectTimeout(3*1000);
	        //防止屏蔽程序抓取而返回403错误
	        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
	        //得到输入流
	        InputStream inputStream = conn.getInputStream();  
	        return inputStream;
	}
	 
	public static HostnameVerifier hv = new HostnameVerifier() {
	        public boolean verify(String urlHostName, SSLSession session) {
	            System.out.println("Warning: URL Host: " + urlHostName + " vs. "
	                               + session.getPeerHost());
	            return true;
	        }
	};
		
	private static void trustAllHttpsCertificates() throws Exception {
		javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
		javax.net.ssl.TrustManager tm = new miTM();
		trustAllCerts[0] = tm;
		javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext
				.getInstance("SSL");
		sc.init(null, trustAllCerts, null);
		javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc
				.getSocketFactory());
	}

	static class miTM implements javax.net.ssl.TrustManager,
			javax.net.ssl.X509TrustManager {
		public java.security.cert.X509Certificate[] getAcceptedIssuers() {
			return null;
		}

		public boolean isServerTrusted(
				java.security.cert.X509Certificate[] certs) {
			return true;
		}

		public boolean isClientTrusted(
				java.security.cert.X509Certificate[] certs) {
			return true;
		}

		public void checkServerTrusted(
				java.security.cert.X509Certificate[] certs, String authType)
				throws java.security.cert.CertificateException {
			return;
		}

		public void checkClientTrusted(
				java.security.cert.X509Certificate[] certs, String authType)
				throws java.security.cert.CertificateException {
			return;
		}
	}
}

 

2. 设置datapath,设置语言包中英文

3. 注意如果https证书不被信任,那么需要导出证书(填写host port 路径,运行InstallCert

导出证书)

   

public class InstallCert {

	public static void main(String[] args) throws Exception {
//		
//		int port;
//		char[] passphrase;
//		if ((args.length == 1) || (args.length == 2)) {
//			String[] c = args[0].split(":");
//			host = c[0];
//			port = (c.length == 1) ? 443 : Integer.parseInt(c[1]);
//			String p = (args.length == 1) ? "changeit" : args[1];
//			passphrase = p.toCharArray();
//		} else {
//			System.out
//					.println("Usage: java InstallCert <host>[:port] [passphrase]");
//			return;
//		}
		String host = "14.17.69.168";
		int port = 443;
		String p =  "changeit" ;
		char[] passphrase = p.toCharArray();
		

		File file = new File("jssecacerts");
		if (file.isFile() == false) {
			char SEP = File.separatorChar;
			File dir = new File(System.getProperty("java.home") + SEP + "lib"
					+ SEP + "security");
			file = new File(dir, "jssecacerts");
			if (file.isFile() == false) {
				file = new File(dir, "cacerts");
			}
		}
		System.out.println("Loading KeyStore " + file + "...");
		InputStream in = new FileInputStream(file);
		KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
		ks.load(in, passphrase);
		in.close();

		SSLContext context = SSLContext.getInstance("TLS");
		TrustManagerFactory tmf = TrustManagerFactory
				.getInstance(TrustManagerFactory.getDefaultAlgorithm());
		tmf.init(ks);
		X509TrustManager defaultTrustManager = (X509TrustManager) tmf
				.getTrustManagers()[0];
		SavingTrustManager tm = new SavingTrustManager(defaultTrustManager);
		context.init(null, new TrustManager[] { tm }, null);
		SSLSocketFactory factory = context.getSocketFactory();

		System.out
				.println("Opening connection to " + host + ":" + port + "...");
		SSLSocket socket = (SSLSocket) factory.createSocket(host, port);
		socket.setSoTimeout(10000);
		try {
			System.out.println("Starting SSL handshake...");
			socket.startHandshake();
			socket.close();
			System.out.println();
			System.out.println("No errors, certificate is already trusted");
		} catch (SSLException e) {
			System.out.println();
			e.printStackTrace(System.out);
		}

		X509Certificate[] chain = tm.chain;
		if (chain == null) {
			System.out.println("Could not obtain server certificate chain");
			return;
		}
 
		System.out.println();
		System.out.println("Server sent " + chain.length + " certificate(s):");
		System.out.println();
		MessageDigest sha1 = MessageDigest.getInstance("SHA1");
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		for (int i = 0; i < chain.length; i++) {
			X509Certificate cert = chain[i];
			System.out.println(" " + (i + 1) + " Subject "
					+ cert.getSubjectDN());
			System.out.println("   Issuer  " + cert.getIssuerDN());
			sha1.update(cert.getEncoded());
			System.out.println("   sha1    " + toHexString(sha1.digest()));
			md5.update(cert.getEncoded());
			System.out.println("   md5     " + toHexString(md5.digest()));
			System.out.println();
		}

		System.out
				.println("Enter certificate to add to trusted keystore or 'q' to quit: [1]");
		String line = "1";
		int k;
		try {
			k = (line.length() == 0) ? 0 : Integer.parseInt(line) - 1;
		} catch (NumberFormatException e) {
			System.out.println("KeyStore not changed");
			return;
		}

		X509Certificate cert = chain[k];
		String alias = host + "-" + (k + 1);
		ks.setCertificateEntry(alias, cert);

		OutputStream out = new FileOutputStream("c://wangl//https//jssecacerts");
		ks.store(out, passphrase);
		out.close();

		System.out.println();
		System.out.println(cert);
		System.out.println();
		System.out
				.println("Added certificate to keystore 'jssecacerts' using alias '"
						+ alias + "'");
	}

	private static final char[] HEXDIGITS = "0123456789abcdef".toCharArray();

	private static String toHexString(byte[] bytes) {
		StringBuilder sb = new StringBuilder(bytes.length * 3);
		for (int b : bytes) {
			b &= 0xff; 
			sb.append(HEXDIGITS[b >> 4]);
			sb.append(HEXDIGITS[b & 15]);
			sb.append(' ');
		}
		return sb.toString();
	}

	private static class SavingTrustManager implements X509TrustManager {

		private final X509TrustManager tm;
		private X509Certificate[] chain;

		SavingTrustManager(X509TrustManager tm) {
			this.tm = tm;
		}

		public X509Certificate[] getAcceptedIssuers() {
			throw new UnsupportedOperationException();
		}

		public void checkClientTrusted(X509Certificate[] chain, String authType)
				throws CertificateException {
			throw new UnsupportedOperationException();
		}

		public void checkServerTrusted(X509Certificate[] chain, String authType)
				throws CertificateException {
			this.chain = chain;
			tm.checkServerTrusted(chain, authType);
		}
	}

}

4.https需要在代码1中指定证书文件.

5.图片信息为: 汉字验证码–demo分

  最终结果显示: 汉字验证码汴demo分享

 

  OK,识别率还可以,但存在提升的空间!

   

 6.如果追求高识别率,可以考虑付费的国外泰比和国内的汉王.

    

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/134715.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 两类交换元素使序列有序 求最少交换次数的题

    两类交换元素使序列有序 求最少交换次数的题

  • python复现softmax损失函数详细版

    python复现softmax损失函数详细版fromtorchimportnnimporttorchdefloss_func(output,target):one_hot=torch.zeros_like(output)foriinrange(target.size(0)):one_hot[i,target[i]]=1softmax_out=torch.exp(output)/(torch.unsque…

  • 最短路径dijkstra算法精品代码(超详解)

    最短路径dijkstra算法精品代码(超详解)一:简介  这个算法用于解决图中单源最短路径问题。所谓单源节点是指给定源节点,求图中其它节点到此源节点的最短路径。如下图所示:给定源节点a,求节点b到a的最短距离。(图来自于参考资料2)那么如何寻找?还是以上图为例:1)初始化:设定除源节点以外的其它所有节点到源节点的距离为INFINITE(一个很大的数),且这些节点都没被处理过。…

  • js 邮箱正则表达式_匹配邮箱的正则表达式

    js 邮箱正则表达式_匹配邮箱的正则表达式一个正则表达式就是由普通字符(a~z)以及特殊字符(称为元字符)组成的文字模式。该模式描述在查找文字主体时待匹配的一个或多个字符串。正则表达式作为一个模板,将某个字符模式与所搜索的字符串进行匹配。语法:/ 匹配对象的模式 /其中,位于“/”定界符之间的部分就是将要在目标对象中进行匹配的模式。用户只要把希望查找的匹配对象的模式内容放入“/”定界符之间即可。例如,在字符串“abcd”中查…

  • ORACLE触发器(trigger)的使用

    ORACLE触发器(trigger)的使用1、触发器说明触发器是一种在事件发生时隐式地自动执行的PL/SQL块,不能接受参数,不能被显式调用2、触发器语法create[orreplace]triggertrigger_name{before|after|insteadof}trigger_eventon{table_name|view_name}[foreachrow]beginPL/SQL语句…

  • 程序已在运行,只能运行一个实例_最常用解决冲突的办法

    程序已在运行,只能运行一个实例_最常用解决冲突的办法//开发中常用的只允许一个程序运行的办法//程序以单例模式运行常用办法,创建一个互斥量//由于互斥量只允许一个进程或者线程占用会创建失败,利用这个特性可以做到单例运行改程序#include"stdafx.h"#include&lt;windows.h&gt;#include&lt;stdio.h&gt;int_tmain(intargc,_TCHAR*argv[]…

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号