-
Notifications
You must be signed in to change notification settings - Fork 2
/
ImgaeUtil.java
120 lines (108 loc) · 3.58 KB
/
ImgaeUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.recognition.software.jdeskew;
//import java.awt.Color;
//import java.awt.Graphics2D;
//import java.awt.RenderingHints;
//import java.awt.geom.AffineTransform;
import net.sourceforge.tess4j.util.LoggHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
//import java.io.File;
//import java.io.IOException;
//import javax.imageio.ImageIO;
public class ImageUtil {
// public static BufferedImage readImageFile(File imageFile) throws IOException {
// return ImageIO.read(imageFile);
// }
private static final Logger logger = LoggerFactory.getLogger(new LoggHelper().toString());
public static boolean isBlack(BufferedImage image, int x, int y) {
if (image.getType() == BufferedImage.TYPE_BYTE_BINARY) {
WritableRaster raster = image.getRaster();
int pixelRGBValue = raster.getSample(x, y, 0);
if (pixelRGBValue == 0) {
return true;
} else {
return false;
}
}
int luminanceValue = 140;
return isBlack(image, x, y, luminanceValue);
}
public static boolean isBlack(BufferedImage image, int x, int y, int luminanceCutOff) {
int pixelRGBValue;
int r;
int g;
int b;
double luminance = 0.0;
// return white on areas outside of image boundaries
if (x < 0 || y < 0 || x > image.getWidth() || y > image.getHeight()) {
return false;
}
try {
pixelRGBValue = image.getRGB(x, y);
r = (pixelRGBValue >> 16) & 0xff;
g = (pixelRGBValue >> 8) & 0xff;
b = (pixelRGBValue) & 0xff;
luminance = (r * 0.299) + (g * 0.587) + (b * 0.114);
} catch (Exception e) {
// ignore.
}
return luminance < luminanceCutOff;
}
// public static BufferedImage rotate(BufferedImage image, double angle, int cx, int cy) {
// int width = image.getWidth(null);
// int height = image.getHeight(null);
//
// int minX, minY, maxX, maxY;
// minX = minY = maxX = maxY = 0;
//
// int[] corners = {0, 0, width, 0, width, height, 0, height};
//
// double theta = Math.toRadians(angle);
// for (int i = 0; i < corners.length; i += 2) {
// int x = (int) (Math.cos(theta) * (corners[i] - cx)
// - Math.sin(theta) * (corners[i + 1] - cy) + cx);
// int y = (int) (Math.sin(theta) * (corners[i] - cx)
// + Math.cos(theta) * (corners[i + 1] - cy) + cy);
//
// if (x > maxX) {
// maxX = x;
// }
//
// if (x < minX) {
// minX = x;
// }
//
// if (y > maxY) {
// maxY = y;
// }
//
// if (y < minY) {
// minY = y;
// }
//
// }
//
// cx = (cx - minX);
// cy = (cy - minY);
//
// BufferedImage bi = new BufferedImage((maxX - minX), (maxY - minY),
// image.getType());
// Graphics2D g2 = bi.createGraphics();
// g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
// RenderingHints.VALUE_INTERPOLATION_BICUBIC);
//
// g2.setBackground(Color.white);
// g2.fillRect(0, 0, bi.getWidth(), bi.getHeight());
//
// AffineTransform at = new AffineTransform();
// at.rotate(theta, cx, cy);
//
// g2.setTransform(at);
// g2.drawImage(image, -minX, -minY, null);
// g2.dispose();
//
// return bi;
// }
}