Skip to content

Commit

Permalink
add image file recognize
Browse files Browse the repository at this point in the history
  • Loading branch information
AnyListen committed Jun 29, 2019
1 parent 09b52ad commit 01bd29a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
6 changes: 4 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.5.1</version>
<version>4.5.16</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -77,7 +77,7 @@
<version>8.8.3</version>
<configuration>
<mainClass>com.luooqi.ocr.MainFm</mainClass>
<bundler>dmg</bundler>
<bundler>exe</bundler>
<jfxAppOutputDir>${project.build.directory}/app</jfxAppOutputDir>
<nativeOutputDir>${project.build.directory}/native</nativeOutputDir>
<appName>treehole</appName>
Expand All @@ -86,6 +86,8 @@
<needShortcut>true</needShortcut>
<bundleArguments>
<runtime />
<copyright>luooqi@2019</copyright>
<installdirChooser>true</installdirChooser>
</bundleArguments>
<nativeReleaseVersion>${soft.version}</nativeReleaseVersion>
</configuration>
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/luooqi/ocr/MainFm.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.luooqi.ocr;

import cn.hutool.core.util.StrUtil;
import cn.hutool.log.StaticLog;
import com.luooqi.ocr.controller.ProcessController;
import com.luooqi.ocr.model.CaptureInfo;
import com.luooqi.ocr.model.StageInfo;
Expand All @@ -22,10 +23,14 @@
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import org.jnativehook.GlobalScreen;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
Expand Down Expand Up @@ -68,6 +73,7 @@ public void start(Stage primaryStage) {

HBox topBar = new HBox(
CommUtils.createButton("snapBtn", MainFm::doSnap, "截图"),
CommUtils.createButton("openImageBtn", MainFm::recImage, "打开"),
CommUtils.createButton("copyBtn", this::copyText, "复制"),
CommUtils.createButton("pasteBtn", this::pasteText, "粘贴"),
CommUtils.createButton("clearBtn", this::clearText, "清空"),
Expand Down Expand Up @@ -147,6 +153,25 @@ public static void doSnap() {
runLater(screenCapture::prepareForCapture);
}

private static void recImage() {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Please Select Image File");
fileChooser.getExtensionFilters().addAll(new FileChooser.ExtensionFilter("Image Files", "*.png", "*.jpg"));
File selectedFile = fileChooser.showOpenDialog(stage);
if (selectedFile == null || !selectedFile.isFile()) {
return;
}
stageInfo = new StageInfo(stage.getX(), stage.getY(),
stage.getWidth(), stage.getHeight(), stage.isFullScreen());
MainFm.stage.close();
try {
BufferedImage image = ImageIO.read(selectedFile);
doOcr(image);
} catch (IOException e) {
StaticLog.error(e);
}
}

public static void cancelSnap() {
runLater(screenCapture::cancelSnap);
}
Expand Down
11 changes: 5 additions & 6 deletions src/main/java/com/luooqi/ocr/utils/CommUtils.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.luooqi.ocr.utils;

import cn.hutool.core.swing.RobotUtil;
import cn.hutool.core.swing.ScreenUtil;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
Expand All @@ -13,9 +11,8 @@
import javafx.geometry.Insets;
import javafx.geometry.Orientation;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.control.Button;
import javafx.scene.control.*;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
Expand All @@ -36,8 +33,10 @@
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.*;
import java.util.regex.Pattern;

public class CommUtils {
Expand Down Expand Up @@ -96,7 +95,7 @@ static String combineTextBlocks(List<TextBlock> textBlocks, boolean isEng) {
int maxX = -1;
double maxAngle = -100;
for (TextBlock textBlock : textBlocks) {
System.out.println(textBlock.getAngle()+ "\t" + textBlock.getFontSize());
//System.out.println(textBlock.getAngle()+ "\t" + textBlock.getFontSize());
if (textBlock.getTopLeft().x < minX) {
minX = textBlock.getTopLeft().x;
minBlock = textBlock;
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
-fx-background-image: url(/img/screenshot.png);
}

#openImageBtn{
-fx-background-image: url(/img/add-image.png);
}

#clearBtn{
-fx-background-image: url(/img/clear.png);
}
Expand Down
Binary file added src/main/resources/img/add-image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 01bd29a

Please sign in to comment.