添加tess4j 文字识别 2023年3月31日16:53:53

master
zhangmeng 2023-03-31 16:54:07 +08:00
parent b7fb200ec4
commit 62be4db1be
19 changed files with 215 additions and 0 deletions

View File

@ -92,6 +92,10 @@
![](./src/main/resources/static/redame/img_35.png)
![](./src/main/resources/static/redame/img_38.png)
#### 3.12 tess4j ocr 文字识别
![](./src/main/resources/static/redame/img_40.png)
### 4. 编解码工具
> 编解码工具也是对开源组件 Hutool 简单封装展示

View File

@ -488,4 +488,8 @@ public class HomeController implements Serializable {
public void maven_jar_install_menu_item(ActionEvent event) {
load_small_tools(10);
}
public void word_ocr_menu_item(ActionEvent event) {
load_small_tools(11);
}
}

View File

@ -78,6 +78,7 @@ public class SmallToolsController {
private AnchorPane telephone;
private AnchorPane json_view;
private AnchorPane maven_install_jar;
private AnchorPane word_ocr;
@FXML
private ListView<ResourcesUtils.SmallTools> listView;
@ -369,10 +370,18 @@ public class SmallToolsController {
}
maven_install_jar(flag);
}
if (newValue.getIndex() == 11) {
if (word_ocr != null) {
flag = true;
}
word_ocr(flag);
}
}
});
}
public static Image getImage(ResourcesUtils.SmallTools SmallTools) {
return switch (SmallTools) {
case Hex_16 -> new Image(ImagePath.path(ImagePath.ImagePathType.Hex_16));
@ -386,6 +395,7 @@ public class SmallToolsController {
case TelePhone -> new Image(ImagePath.path(ImagePath.ImagePathType.Qr_CODE));
case JSONView -> new Image(ImagePath.path(ImagePath.ImagePathType.Qr_CODE));
case Maven_Install_Jar -> new Image(ImagePath.path(ImagePath.ImagePathType.Qr_CODE));
case Word_ocr -> new Image(ImagePath.path(ImagePath.ImagePathType.Qr_CODE));
};
}
@ -622,6 +632,24 @@ public class SmallToolsController {
common_method();
}
public void word_ocr(boolean flag) {
//默认选择第一个
listView.getSelectionModel().select(11);
if (!flag) {
try {
root = FXMLLoader.load(ResourcesUtils.getResource("word-ocr"));
} catch (IOException e) {
e.printStackTrace();
}
word_ocr = root;
} else {
root = word_ocr;
}
common_method();
}
private void common_method() {
splitPane.getItems().remove(1);
splitPane.getItems().add(1, root);
@ -771,4 +799,6 @@ public class SmallToolsController {
}
maven_install_jar(flag);
}
}

View File

@ -0,0 +1,114 @@
package com.zhangmeng.tools.controller;
import com.zhangmeng.tools.utils.AlertUtils;
import com.zhangmeng.tools.utils.ImagePath;
import javafx.application.Platform;
import javafx.beans.property.SimpleObjectProperty;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.ITesseract;
import net.sourceforge.tess4j.Tesseract;
import net.sourceforge.tess4j.Tesseract1;
import net.sourceforge.tess4j.TesseractException;
import java.io.File;
/**
* @author :
* @version : 1.0
* @date : 2023-03-31 14:11
*/
@Slf4j
public class WordOcrController {
public static String tessdata = System.getProperty("user.dir");
@FXML
public Button file_choose_button;
@FXML
public TextArea res_view;
public static final String language = "eng+chi_sim+chr+num";
@FXML
public Button cover;
@FXML
public TextField file_path;
@FXML
private ImageView image_view;
public static final SimpleObjectProperty<File> choose_file = new SimpleObjectProperty<>();
@FXML
public void initialize() {
file_choose_button.setText(null);
ImageView iv = new ImageView(new Image(ImagePath.path(ImagePath.ImagePathType.IMAGE_FILE)));
iv.setPreserveRatio(true);
iv.setFitWidth(18);
file_choose_button.setGraphic(iv);
file_choose_button.setOnAction(event -> {
choose_file();
});
cover.setOnAction(event -> {
if (choose_file.getValue() == null) {
AlertUtils.alert_warning("请选择将要识别的文字再试!");
return;
}
File file = choose_file.getValue();
Stage alert = AlertUtils.alert_loading(cover.getScene().getWindow());
new Thread(() -> {
Platform.runLater(() -> {
init_tess4j(file);
alert.close();
});
}).start();
});
}
public void choose_file() {
Stage stage = new Stage();
FileChooser dc = new FileChooser();
dc.setTitle("文件选择");
dc.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("图片类型", "*.jpg", "*.png"));
File file = dc.showOpenDialog(stage);
if (file != null) {
String path = file.getAbsolutePath();
file_path.setText(path);
log.info("base64---file:{}", path);
choose_file.set(file);
image_view.setImage(new Image(choose_file.get().getPath()));
}
}
public void init_tess4j(File file) {
String path = tessdata + "/src/main/resources/tessdata";
log.info(path);
ITesseract instance = new Tesseract();
instance.setDatapath(path);
instance.setLanguage(language);
try {
String res = instance.doOCR(file);
log.info("file:{}", file.getPath());
res_view.setText(res);
} catch (TesseractException e) {
AlertUtils.alert_warning(e.getMessage());
}
}
}

View File

@ -25,12 +25,22 @@
package com.zhangmeng.tools.utils;
import com.zhangmeng.tools.controller.MusicController;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.Window;
import java.io.IOException;
/**
* @author :
@ -39,6 +49,29 @@ import javafx.stage.StageStyle;
*/
public class AlertUtils {
/**
*
*/
public static Stage alert_loading(Window window) {
AnchorPane root = null;
try {
root = FXMLLoader.load(ResourcesUtils.getResource("loading"));
} catch (IOException e) {
e.printStackTrace();
}
Scene scene = new Scene(root);
Stage stage = new Stage();
stage.setScene(scene);
stage.setTitle("加载中");
stage.setWidth(400);
stage.setHeight(150);
stage.initStyle(StageStyle.UTILITY);
stage.initOwner(window);
stage.initModality(Modality.APPLICATION_MODAL);
stage.show();
return stage;
}
/**
*
*/

View File

@ -272,6 +272,10 @@ public class ImagePath {
public static String Tools_ICON = "svg/tools-hardware.png";
public static String Qr_CODE = "svg/qr-code.png";
public static String path(String file_path){
return "static/" + file_path;
}
public static String path(ImagePathType type) {
String path = null;

View File

@ -121,6 +121,7 @@ public class ResourcesUtils {
TelePhone("手机号工具",8),
JSONView("json工具",9),
Maven_Install_Jar("maven安装jar",10),
Word_ocr("文字识别ocr",11),
;
SmallTools(String title, int index) {

View File

@ -41,6 +41,7 @@
<MenuItem mnemonicParsing="false" text="手机号工具" onAction="#telephone_menu_item"/>
<MenuItem mnemonicParsing="false" text="JsonView" onAction="#JsonView_menu_item"/>
<MenuItem mnemonicParsing="false" text="maven-jar-install" onAction="#maven_jar_install_menu_item"/>
<MenuItem mnemonicParsing="false" text="word-ocr" onAction="#word_ocr_menu_item"/>
</items>
</Menu>

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="649.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.zhangmeng.tools.controller.WordOcrController">
<children>
<AnchorPane layoutX="201.0" layoutY="117.0" style="-fx-border-color: #cbcacb; -fx-border-width: 1;" AnchorPane.bottomAnchor="202.0" AnchorPane.leftAnchor="201.0" AnchorPane.topAnchor="117.0">
<children>
<ImageView fx:id="image_view" fitHeight="330.0" fitWidth="508.0" pickOnBounds="true" preserveRatio="true" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
<TextArea fx:id="res_view" layoutX="727.0" layoutY="69.0" prefHeight="534.0" prefWidth="430.0" AnchorPane.bottomAnchor="46.0" AnchorPane.rightAnchor="43.0" AnchorPane.topAnchor="69.0" />
<Button fx:id="file_choose_button" layoutX="202.0" layoutY="67.0" mnemonicParsing="false" text="Button" />
<Label layoutX="58.0" layoutY="71.0" text="请选择将要识别的图片:" />
<TextField fx:id="file_path" layoutX="280.0" layoutY="67.0" prefHeight="25.0" prefWidth="430.0" />
<Label layoutX="129.0" layoutY="116.0" text="图片预览:" />
<Button fx:id="cover" layoutX="390.0" layoutY="492.0" mnemonicParsing="false" text="Button" AnchorPane.bottomAnchor="134.0" />
</children>
</AnchorPane>

Binary file not shown.

After

Width:  |  Height:  |  Size: 116 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.