sha256
package com.onestar.ymf;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
public class SHA256WithoutKey {
public static void main1(String[] args) {
try {
// 初始化MessageDigest
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
// 要哈希的字符串
String inputString = "ahyycg@123123";
// 使用update方法处理数据
sha256.update(inputString.getBytes("UTF-8"));
// 完成哈希计算,获取哈希值
byte[] hashBytes = sha256.digest();
// 将哈希值转换为十六进制字符串
StringBuilder hashString = new StringBuilder();
for (byte b : hashBytes) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hashString.append('0');
}
hashString.append(hex);
}
// 打印哈希值
System.out.println("SHA-256: " + hashString.toString());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String s = "/9j/4AAQSkZJRgABAgAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAyAIIDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQF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/hNvFf/Qz61/4Hy/8AxVFFAC/8Jv4s/wCho1r/AMD5f/iqP+E48W/9DRrf/gwl/wDiqKKAF/4Tnxd/0NOt/wDgwl/+Ko/4Trxf/wBDVrn/AIMJf/iqKKAMSeea6uJbi4lkmnlcvJJIxZnYnJJJ5JJ5zRRRQB//2Q==";
generateImage(s,"code");
}
public static String generateImage(String file, String path) {
// 解密
try {
// 项目绝对路径
String savePath = "D:\\testFile\\";
// 图片分类路径+图片名+图片后缀
//String imgClassPath = path.concat(UUID.randomUUID().toString()).concat(".jpg");
String imgClassPath = path.concat(".jpg");
// 解密
Base64.Decoder decoder = Base64.getDecoder();
// 去掉base64前缀 data:image/jpeg;base64,
file = file.substring(file.indexOf(",", 1) + 1, file.length());
byte[] b = decoder.decode(file);
// 处理数据
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {
b[i] += 256;
}
}
File tempFile = new File(savePath);
//判断文件目录是否存才 不存在创建 避免OutputStream发现不存在报异常
if (!tempFile.exists()) {
tempFile.mkdirs();
}
// 保存图片
OutputStream out = new FileOutputStream(tempFile.getPath()+ File.separator+imgClassPath);
out.write(b);
out.flush();
out.close();
// 返回图片的相对路径 = 图片分类路径+图片名+图片后缀
return imgClassPath;
} catch (IOException e) {
return null;
}
}
}
tess4j
package com.onestar.ymf;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.TesseractException;
import java.io.File;
import java.awt.*;
public class MainServer {
public static void main(String[] args) throws TesseractException {
long start = System.currentTimeMillis();
System.out.println("开始OCR文字识图,请稍后...");
//加载要识别的图片
File image = new File("D:\\testFile\\123.png");
//设置配置文件夹微视、识别语言、识别模式
Tesseract tesseract = new Tesseract();
tesseract.setDatapath("D:\\tessdata");
//设置识别语言为中文简体,(如果要设置为英文可改为"eng")
tesseract.setLanguage("eng");
//使用 OSD 进行自动页面分割以进行图像处理
tesseract.setPageSegMode(1);
//设置引擎模式是神经网络LSTM引擎
tesseract.setOcrEngineMode(1);
//开始识别整张图片中的文字
String result = tesseract.doOCR(image);
long time = System.currentTimeMillis()-start;
System.out.println("识别结束,耗时:"+time+" 毫秒,识别结果如下:");
System.out.println();
System.out.println(result);
}
}
依赖
<!-- Tesseract OCR -->
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>5.9.0</version>
</dependency>