上干货:
一、导入依赖
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.5</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.2</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>net.sourceforge.tess4j</groupId>
<artifactId>tess4j</artifactId>
<version>3.4.0</version>
<exclusions>
<exclusion>
<groupId>com.sun.jna</groupId>
<artifactId>jna</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.5.5</version>
</dependency>
<dependency>
<groupId>org.swinglabs</groupId>
<artifactId>swingx</artifactId>
<version>1.6.1</version>
</dependency>
二、截图(全屏)
import javax.imageio.ImageIO;
public class CaptureScreen {
public static void captureScreen(String fileName, String folder) throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
//保存路径
File screenFile = new File(fileName);
if (!screenFile.exists()) {
screenFile.mkdir();
}
File f = new File(screenFile, folder);
ImageIO.write(image, "png", f);
//自动打开
if (Desktop.isDesktopSupported()
&& Desktop.getDesktop().isSupported(Desktop.Action.OPEN))
Desktop.getDesktop().open(f);
}
public static void main(String[] args) {
try {
captureScreen("需要保存的路径","1.png");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
三、切图
import cn.hutool.core.util.IdUtil;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import org.bytedeco.javacpp.Loader;
import java.io.File;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
/**
* @Program: csdn @ClassName: CutOutTool @Author: 剑客阿良_ALiang @Date: 2022-01-23 18:27 @Description:
* 裁剪工具 @Version: V1.0
*/
public class CutOutTool {
/**
* 图片裁剪
*
* @param imagePath 图片地址
* @param outputDir 临时目录
* @param startX 裁剪起始x坐标
* @param startY 裁剪起始y坐标
* @param weight 裁剪宽度
* @param height 裁剪高度
* @throws Exception 异常
*/
public static String cutOutImage(
String imagePath,
String outputDir,
Integer startX,
Integer startY,
Integer weight,
Integer height)
throws Exception {
List<String> paths = Splitter.on(".").splitToList(imagePath);
String ext = paths.get(paths.size() - 1);
if (!Arrays.asList("png", "jpg").contains(ext)) {
throw new Exception("format error");
}
String resultPath =
Joiner.on(File.separator).join(Arrays.asList(outputDir, IdUtil.simpleUUID() + "." + ext));
String ffmpeg = Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);
ProcessBuilder builder =
new ProcessBuilder(
ffmpeg,
"-i",
imagePath,
"-vf",
MessageFormat.format(
"crop={0}:{1}:{2}:{3}",
String.valueOf(weight),
String.valueOf(height),
String.valueOf(startX),
String.valueOf(startY)),
"-y",
resultPath);
builder.inheritIO().start().waitFor();
return resultPath;
}
public static void main(String[] args) throws Exception {
System.out.println(
cutOutImage(
"C:\\Users\\攀小黑\\Desktop\\1.png", "C:\\Users\\攀小黑\\Desktop", 10, 10, 190, 210));
}
}
四、图片识别出文字
1、这里我们使用的时OCR模型算法实现的。
2、需要下载训练包,这里有大佬写好的。
Gitee:mirrors / tesseract-ocr / tessdata · GitCode
百度网盘:链接:https://pan.baidu.com/s/1ONkrzaXt48q34E2ynAnDYA
提取码:snip
根据自己要识别的国家的语言去选择训练的包。
3、因为业务需要,我这里选择的时英文包 :eng。
4、下载好训练包后放在目录名为 tessdata 的文件夹下。
5、代码如下:
public static void main(String[] args) {
try {
String fileImg = "D:/6.png";
File imageFile = new File(fileImg);
//创建tess对象
ITesseract instance = new Tesseract();
//设置训练文件目录
instance.setDatapath("D:/tessdata");
//设置训练语言
instance.setLanguage("eng");
//执行转换
String result = instance.doOCR(imageFile);
System.out.println("-->"+result);
} catch (Exception e) {
throw new RuntimeException(e);
}
}