Skip to content

Commit

Permalink
Fix doc
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenq committed Dec 5, 2023
1 parent 07893d2 commit 66384e4
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public class HoughLine {
/**
* Constructor.
*
* @param image
* @param image input image
*/
public ImageDeskew(BufferedImage image) {
this.cImage = image;
Expand All @@ -55,7 +55,7 @@ public ImageDeskew(BufferedImage image) {
/**
* Calculates the skew angle of the image cImage.
*
* @return
* @return the skew angle
*/
public double getSkewAngle() {
ImageDeskew.HoughLine[] hl;
Expand Down
276 changes: 138 additions & 138 deletions src/main/java/com/recognition/software/jdeskew/ImageUtil.java
Original file line number Diff line number Diff line change
@@ -1,138 +1,138 @@
/**
* <a url=http://www.jdeskew.com/>JDeskew</a>
*/
package com.recognition.software.jdeskew;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;

import net.sourceforge.tess4j.util.LoggHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ImageUtil {

private static final Logger logger = LoggerFactory.getLogger(new LoggHelper().toString());

/**
* Whether the pixel is black.
*
* @param image source image
* @param x
* @param y
* @return
*/
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);
return pixelRGBValue == 0;
}

int luminanceValue = 140;
return isBlack(image, x, y, luminanceValue);
}

/**
* Whether the pixel is black.
*
* @param image source image
* @param x
* @param y
* @param luminanceCutOff
* @return
*/
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) {
logger.warn("", e);
}

return luminance < luminanceCutOff;
}

/**
* Rotates image.
*
* @param image source image
* @param angle by degrees
* @param cx x-coordinate of pivot point
* @param cy y-coordinate of pivot point
* @return rotated image
*/
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;
}
}
/**
* <a url=http://www.jdeskew.com/>JDeskew</a>
*/
package com.recognition.software.jdeskew;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;

import net.sourceforge.tess4j.util.LoggHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ImageUtil {

private static final Logger logger = LoggerFactory.getLogger(new LoggHelper().toString());

/**
* Whether the pixel is black.
*
* @param image source image
* @param x x-coordinate
* @param y y-coordinate
* @return true if pixel is black; false, otherwise
*/
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);
return pixelRGBValue == 0;
}

int luminanceValue = 140;
return isBlack(image, x, y, luminanceValue);
}

/**
* Whether the pixel is black.
*
* @param image source image
* @param x x-coordinate
* @param y y-coordinate
* @param luminanceCutOff luminance threshold
* @return true if pixel is black; false, otherwise
*/
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) {
logger.warn("", e);
}

return luminance < luminanceCutOff;
}

/**
* Rotates image.
*
* @param image source image
* @param angle by degrees
* @param cx x-coordinate of pivot point
* @param cy y-coordinate of pivot point
* @return rotated image
*/
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;
}
}
2 changes: 1 addition & 1 deletion src/main/java/net/sourceforge/tess4j/Tesseract.java
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ protected void setVariables() {
}

/**
* A wrapper for {@link #setImage(int, int, ByteBuffer, Rectangle, int)}.
* A wrapper for {@link #setImage(int, int, ByteBuffer, int)}.
*
* @param image a rendered image
* @throws java.io.IOException
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/sourceforge/tess4j/Tesseract1.java
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ protected void setVariables() {
}

/**
* A wrapper for {@link #setImage(int, int, ByteBuffer, Rectangle, int)}.
* A wrapper for {@link #setImage(int, int, ByteBuffer, int)}.
*
* @param image a rendered image
* @throws java.io.IOException
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/net/sourceforge/tess4j/util/ImageHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static IIOImage getScaledInstance(IIOImage iioSource, float scale) {
* A replacement for the standard <code>BufferedImage.getSubimage</code>
* method.
*
* @param image
* @param image input image
* @param x the X coordinate of the upper-left corner of the specified
* rectangular region
* @param y the Y coordinate of the upper-left corner of the specified
Expand Down Expand Up @@ -110,8 +110,8 @@ public static BufferedImage convertImageToBinary(BufferedImage image) {
/**
* Removes alpha channel from image
*
* @param image
* @return
* @param image input image
* @return image with alpha channel removed
*/
public static BufferedImage removeAlphaChannel(BufferedImage image) {
if (!image.getColorModel().hasAlpha()) {
Expand Down Expand Up @@ -220,8 +220,8 @@ public static Image getClipboardImage() {
* Clones an image.
* http://stackoverflow.com/questions/3514158/how-do-you-clone-a-bufferedimage
*
* @param bi
* @return
* @param bi input image
* @return cloned image
*/
public static BufferedImage cloneImage(BufferedImage bi) {
ColorModel cm = bi.getColorModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ public static File deskewImage(File imageFile, double minimumDeskewThreshold) th
/**
* Reads image meta data.
*
* @param oimage
* @param oimage input image
* @return a map of meta data
*/
public static Map<String, String> readImageData(IIOImage oimage) {
Expand Down

0 comments on commit 66384e4

Please sign in to comment.