Skip to content

Instantly share code, notes, and snippets.

@h1ddengames
Last active July 4, 2020 06:50
Show Gist options
  • Save h1ddengames/e8f95a4d08fc90d6cf07eb52b9f57233 to your computer and use it in GitHub Desktop.
Save h1ddengames/e8f95a4d08fc90d6cf07eb52b9f57233 to your computer and use it in GitHub Desktop.
Using Tesseract OCR, convert an image into the string of letters and words found in the image.
package xyz.hiddengames.core;
import net.sourceforge.tess4j.Tesseract;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
// Requires Tesseract: https://mvnrepository.com/artifact/net.sourceforge.tess4j/tess4j/4.5.1
// Requires Tesseract trained data: https://github.com/tesseract-ocr/tessdata_best
// Crop image method from: https://stackoverflow.com/questions/2386064/how-do-i-crop-an-image-in-java
// Resize image method from: https://www.codejava.net/java-se/graphics/how-to-resize-images-in-java
// Invert image method from: https://dyclassroom.com/image-processing-project/how-to-convert-a-color-image-into-negative
// Read image method from: https://www.geeksforgeeks.org/tesseract-ocr-with-java-with-examples/
// Usage:
// ImageReader.cropImage("src/test/resources/516.jpg", "src/test/resources/515-1.jpg", new Rectangle(20, 150, 245, 475));
// ImageReader.invertImage("src/test/resources/515-1.jpg", "src/test/resources/515-2.jpg");
// ImageReader.resizeImage("src/test/resources/515-2.jpg", "src/test/resources/515-3.jpg", 3.8);
// System.out.println(ImageReader.readImage("src/test/resources/515-3.jpg"));
public class ImageReader {
private static String getFormatName(String outputPath) {
// Extracts extension of output file.
return outputPath.substring(outputPath.lastIndexOf(".") + 1);
}
public static void cropImage(String inputImagePath, String outputImagePath, Rectangle rect) {
try {
File inputFile = new File(inputImagePath);
BufferedImage dest = ImageIO.read(inputFile).getSubimage(rect.x, rect.y, rect.width, rect.height);
ImageIO.write(dest, getFormatName(outputImagePath), new File(outputImagePath));
} catch (Exception e) { e.printStackTrace(); }
}
/**
* Resizes an image to a absolute width and height (the image may not be
* proportional)
*
* @param inputImagePath Path of the original image
* @param outputImagePath Path to save the resized image
* @param scaledWidth absolute width in pixels
* @param scaledHeight absolute height in pixels
*/
public static void resizeImage(String inputImagePath, String outputImagePath, int scaledWidth, int scaledHeight) {
try {
// Reads input image.
File inputFile = new File(inputImagePath);
BufferedImage inputImage = ImageIO.read(inputFile);
// Creates output image.
BufferedImage outputImage = new BufferedImage(scaledWidth, scaledHeight, inputImage.getType());
// Scales the input image to the output image.
Graphics2D g2d = outputImage.createGraphics();
g2d.drawImage(inputImage, 0, 0, scaledWidth, scaledHeight, null);
g2d.dispose();
// Writes to output file.
ImageIO.write(outputImage, getFormatName(outputImagePath), new File(outputImagePath));
} catch (Exception e) { e.printStackTrace(); }
}
/**
* Resizes an image by a percentage of original size (proportional).
*
* @param inputImagePath Path of the original image
* @param outputImagePath Path to save the resized image
* @param percent a double number specifies percentage of the output image
* over the input image.
*/
public static void resizeImage(String inputImagePath, String outputImagePath, double percent) {
try {
File inputFile = new File(inputImagePath);
BufferedImage inputImage = ImageIO.read(inputFile);
int scaledWidth = (int) (inputImage.getWidth() * percent);
int scaledHeight = (int) (inputImage.getHeight() * percent);
resizeImage(inputImagePath, outputImagePath, scaledWidth, scaledHeight);
} catch (Exception e) { e.printStackTrace(); }
}
public static void invertImage(String inputImagePath, String outputImagePath) {
BufferedImage img = null;
File f = null;
// Read image.
try {
f = new File(inputImagePath);
img = ImageIO.read(f);
} catch (IOException e) { e.printStackTrace(); }
// Get image width and height.
int width = img.getWidth();
int height = img.getHeight();
// Convert to negative.
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int p = img.getRGB(x, y);
int a = (p >> 24) & 0xff;
int r = (p >> 16) & 0xff;
int g = (p >> 8) & 0xff;
int b = p & 0xff;
// Subtract RGB from 255.
r = 255 - r;
g = 255 - g;
b = 255 - b;
// Set new RGB value.
p = (a << 24) | (r << 16) | (g << 8) | b;
img.setRGB(x, y, p);
}
}
// Write image to disk.
try {
f = new File(outputImagePath);
ImageIO.write(img, getFormatName(outputImagePath), f);
} catch (IOException e) { e.printStackTrace(); }
}
public static String readImage(String inputImagePath) {
Tesseract tesseract = new Tesseract();
try {
// The path to your trained data.
tesseract.setDatapath("src/test/resources/tessdata");
File file = new File(inputImagePath);
if (file.exists()) {
return tesseract.doOCR(file);
} else {
System.out.println("File does not exist.");
}
} catch (Exception e) { e.printStackTrace(); }
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment