Click here to Skip to main content
15,391,989 members
Articles / Programming Languages / Java
Posted 30 Aug 2016

Tagged as


2 bookmarked

Programmable Poetry Using OCR

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
30 Aug 2016CPOL5 min read
This whitepaper shows how I used OCR Xpress for Java to OCR a scanned newspaper, redact the key words, and accomplish my goal.

This article is a sponsored article. Articles such as these are intended to provide you with information on products and services that we consider useful and of value to developers

Did you know that optical character recognition (OCR) can bring forth your artistry? Have you seen the newspaper blackout poems? Using OCR I was able to create a simple program that allowed me to create just that. This whitepaper shows how I used OCR Xpress for Java to OCR a scanned newspaper, redact the key words, and accomplished my goal. Redaction is a very popular means of removing sensitive material from any document. However, it can also be entertaining.

Image 1


There were a few functionalities that I required before I channeled my inner Walt Whitman. The first was OCR, the second was word/phrase search, the third was redaction or, in my case, negated redaction, and the final thing was saving it to a file. In this article, I will briefly go over how I was able to create a small application using OCR Xpress.


Getting OCR up and running was my first step to becoming a modern-day poet. OCR is the conversion of an image (or images) that has typed / printed text to an electronic output. OCR has many use cases for many different companies and also has a practical need for the blind and visually impaired. My intent, however, was much more whimsical.

//required accusoft dependencies 
package com.accusoft.ocrxpress.samples; 
import com.accusoft.ocrxpress.*; 
//java dependencies 
import java.awt.Color; 
import java.awt.image.*: 
import javax.imageio.*; 
import java.util.*; 
//Display Image 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.ImageIcon; 
import java.awt.Flowlayout:

I took the Memory sample that OCR Xpress was packaged with and added a NewspaperBlackout class. Above are the imports and the package that I needed within my class.

There are were a few different ways to code this application, but I decided to create a couple helper functions. Below I used InputString, DisplayImage, ConvertToBlack, and ConvertRect.

public class NewspaperBlackout 
    public static String InputString() { 
        Scanner scanner = new scanner (; 
        System.out.prints "Type your poem. <Press Enter for default>"); 
        return scanner.nextLine();

This helper function allows you to either hit "Enter" for the default poem, or type your own poem within the Console window.

public static void DisplayImage(BufferedImage bi) { 
    ImageIcon icon = new ImageIcon(bi); 
    JFrame frame = new JFrame(); 
    frame.setLayouts new FlowLayouts()); 
    frame.setSize (bi.getWidth(), bi.getHeight()); 
    JLabel lbl = new JLabel(); 

The helper function displays the final output in a JFrame.

public static void convertToBlack(BufferedImage bi) {
    Color c = new Color (0,0,0); //black pixel
    for (int x = 0; x < bi.getWidth(); x++) { 
        for (int y = 0; y < bi.getHeight(); y ++) {
            bi.setRGB(x, y, c.getRGB());

This helper function converts the image to completely black. There are many different ways to accomplish this, and I recommend using the method you feel most comfortable with.

    public static java.awt.Rectangle ConvertRect 
    (com.accusoft.ocrxpress.Rectangle accusoftRect) {
    java.awt.Rectangle javaRect = new java.awt.Rectangle(); 
    javaRect.x = accusoftRect.getLeft(); 
    javaRect.y = accusoftRect.getTop (); 
    javaRect.width = accusoftRect.getRight() - accusoftRect.getLeft(); 
    javaRect.height = accusoftRect.getBottom() - accusoftRect.getTop(); 
    return javaRect;

This helper function takes in an Accusoft rectangle, which has Top, Bottom, Right, and Left, and converts it to the standard Java rectangle, x, y, width, and height.

public static void main (String[] args) throws OcrxException {
    String inputImagePath = "images/NP01.bmp"; 
    BufferedImage originalImg = null, outputImg = null;
    //Load and display original image
    try {
        originalImg = File (inputImagePath));
    catch (IOException e) { 

Once I had the newspaper scanned and saved as a BMP image, I was ready to load my image into a bufferedImage by calling

0crXpress ocrx = new 0crXpress();
RecognitionParameters parameters = new RecognitionParameters(); 
Document document = ocrx.recognizeToMemory(parameters, original Img);

Image scanned to BMP? Check. BMP loaded as a bufferedimage? Check. Now onto the next step of my project. OCR Xpress provided a few options: output to file, output to PDF, and output to memory. For my needs, I wanted to use recognizeToMemory. I needed to initialize the OCR Xpress engine, and RecognitionParameters. Then I simply called recognizeToMemory to OCR the image to memory.

Word Search

Now that I had my newspaper in an electronic form, I could search for words/phrases. This also allowed me to find the rectangle area of each word/phrase that I searched for. I was able to add these to a list of RECTs, which I then merged with my "blackout" page.

String searchString = InputString(); 
if (searchString.isEmpty()) { 
    searchString = "You give something possible by psychic sign passed forth on networks?";
String[] searchWords = searchString.split("\\s+");

Using the helper function InputString, all I needed to do was pass in a default value and populate the string array with Java’s split function using either the users input or the default value.

This part was the most difficult, I actually had to stop and think of a poem to write, using the words given in the article.

Image 2

"You give something possible by psychic sign passed forth on networks?" OK, so not the best, but writing poetry is a lot harder than writing code.

Redaction Action

The next step (which seemed to be most difficult) was also made easy. First, let me explain redaction and what it’s used for. Redaction is the process of censoring or obscuring part of a text for legal or security purposes. When someone says redaction, you might think top secret "your eyes only" documents. However, every company has sensitive material that they may wish to redact, such as customer data, company data, or employee data to name a few. However, my redaction needs are more of the "I love you" variety. Once the text was redacted, I was then able to merge the redacted areas with the original image that was converted to black.

try { 
        outputImg = File (inputImagePath));
    } catch (IOException e) { 

There are a few different ways to copy the original image, but for simplicity I created a new bufferedImage outputImg. For this image, I needed to convert all pixels to black. This is where the ConvertToBlack helper function comes into play.

        int curWord = 0; 
        for (Word word : document.getWords()) { 
            if (curWord >= searchWords.length) 
            if (word.getText().equals(searchWords[curWord]) == false) 

            Raster img = originalImg.getData(ConvertRect(word.getArea())); 

I needed to search each word and extract the area corresponding to the image’s coordinates. OCR Xpress provided a getArea() function that did just that. I had the area of the word I was searching. I needed to convert the RECTs, get the pixel data, and then set the data to the outputImg file before moving to the next word.

Then I could display my newspaper blackout poem.

Image 3


Before you run off and create your own newspaper blackout poems, let’s quickly summarize what was mentioned above. I described what OCR was, and how many companies find it very useful in everyday practical means. I also went over redaction and how companies and individuals benefit from its use. Lastly, I showed you how I used OCR and redaction for an interesting and fun project. I would encourage you to try this project for yourself, and share your "artsy" side with the world.


You can download OCR Xpress and this example from the following links:

OCR Xpress:

To obtain an evaluation license, you will need to contact Accusoft’s support. They will also be able to answer any questions that you might have.

OCR Xpress & NewspaperBlackout Application:


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Authors

United States United States
Accusoft provides a full spectrum of document, content, and imaging solutions as fully supported, enterprise-grade, client-server applications, mobile apps, cloud services, and software development kits. The company is focused on solving document lifecycle complexities through:

- A customer-focused approach
- Continuous product development
- Proactive support
- Forward-thinking leadership

Founded in 1991, Accusoft has grown through persistent product innovation and strategic mergers and acquisitions into the best-in-class solutions provider it is today. The company has been awarded 30 patents and is recognized as a thought leader in the industry.
Group type: Organisation

1 members

Corey Morehead
United States United States
Corey Morehead is a Software Engineer for OCR Xpress at Accusoft. He joined the company in 2014 as a Software Engineer in Support. Corey contributes to the Image Recognition team as a Software Engineer and has a BS in Game Development.

Comments and Discussions

-- There are no messages in this forum --