Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
hezhilong committed May 4, 2020
1 parent ce02a6b commit ae49a2d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
5 changes: 2 additions & 3 deletions src/main/java/com/luooqi/ocr/snap/ScreenCapture.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
import org.imgscalr.Scalr;

import java.awt.*;
import java.awt.image.BufferedImage;
Expand Down Expand Up @@ -403,14 +402,14 @@ public void prepareForCapture() {
isSnapping = true;
MainFm.stage.setOpacity(0.0f);
Platform.runLater(() -> {
Rectangle rectangle = CommUtils.snapScreen(MainFm.stage);
Rectangle rectangle = CommUtils.getDisplayScreen(MainFm.stage);
data.reset();
CaptureInfo.ScreenMinX = rectangle.x;
CaptureInfo.ScreenMaxX = rectangle.x + rectangle.width;
CaptureInfo.ScreenWidth = rectangle.width;
CaptureInfo.ScreenHeight = rectangle.height;
BufferedImage bufferedImage = ScreenUtil.captureScreen(rectangle);
bufferedImage = Scalr.resize(bufferedImage, Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, CaptureInfo.ScreenWidth * 2, CaptureInfo.ScreenHeight * 2);
//bufferedImage = Scalr.resize(bufferedImage, Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, CaptureInfo.ScreenWidth * 2, CaptureInfo.ScreenHeight * 2);
WritableImage fxImage = SwingFXUtils.toFXImage(bufferedImage, null);
deActivateAllKeys();
scene.setRoot(new Pane());
Expand Down
39 changes: 23 additions & 16 deletions src/main/java/com/luooqi/ocr/utils/CommUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -273,31 +273,38 @@ public static void initStage(Stage stage) {
stage.getIcons().add(new javafx.scene.image.Image(MainFm.class.getResource("/img/logo.png").toExternalForm()));
}

private static final Pattern scalePattern = Pattern.compile("renderScale:([\\d.]+)");

public static Rectangle snapScreen(Stage stage){
double x = stage.getX();
private static final Pattern SCALE_PATTERN = Pattern.compile("renderScale:([\\d.]+)");

public static Rectangle getDisplayScreen(Stage stage){
Screen crtScreen = getCrtScreen(stage);
Rectangle2D rectangle2D = crtScreen.getBounds();
return new Rectangle((int)rectangle2D.getMinX (), (int)rectangle2D.getMinY(),
(int)rectangle2D.getWidth(),
(int)rectangle2D.getHeight());
}

public static float getScale(Stage stage){
Screen crtScreen = getCrtScreen(stage);
float scale = 1.0f;
assert crtScreen != null;
String str = crtScreen.toString();
Matcher matcher = SCALE_PATTERN.matcher(str);
if (matcher.find()){
scale = Float.parseFloat(matcher.group(1));
}
return scale;
}

private static Screen getCrtScreen(Stage stage) {
double x = stage.getX();
Screen crtScreen = null;
for (Screen screen : Screen.getScreens()) {
crtScreen = screen;
Rectangle2D bounds = screen.getBounds();
if (bounds.getMaxX() > x){
if (bounds.getMaxX() > x) {
break;
}
}
float scale = 1.0f;
assert crtScreen != null;
String str = crtScreen.toString();
Matcher matcher = scalePattern.matcher(str);
if (matcher.find()){
scale = Float.parseFloat(matcher.group(1));
}
Rectangle2D rectangle2D = crtScreen.getBounds();
return new Rectangle((int)rectangle2D.getMinX (), (int)rectangle2D.getMinY(),
(int)(rectangle2D.getWidth() * scale),
(int)(rectangle2D.getHeight() * scale));
return crtScreen;
}
}
6 changes: 5 additions & 1 deletion src/test/java/com/luooqi/ocr/utils/OcrUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.junit.Test;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
Expand All @@ -30,6 +31,9 @@ private Point frameToPoint(String text){

@Test
public void sogouWebOcr() {

GraphicsConfiguration asdf = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
AffineTransform asfd2 = asdf.getDefaultTransform();
double scaleX = asfd2.getScaleX();
double scaleY = asfd2.getScaleY();
}
}

0 comments on commit ae49a2d

Please sign in to comment.