DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

How does AI transform chaos engineering from an experiment into a critical capability? Learn how to effectively operationalize the chaos.

Data quality isn't just a technical issue: It impacts an organization's compliance, operational efficiency, and customer satisfaction.

Are you a front-end or full-stack developer frustrated by front-end distractions? Learn to move forward with tooling and clear boundaries.

Developer Experience: Demand to support engineering teams has risen, and there is a shift from traditional DevOps to workflow improvements.

Related

  • Minions in Minikube - A Kubernetes Intro for Java Developers
  • Charsets and Unicode Identifiers in Java
  • Effective Exception Handling in Java and Spring Boot Applications
  • Code of Shadows: Master Shifu and Po Use Functional Java to Solve the Decorator Pattern Mystery

Trending

  • Defining Effective Microservice Boundaries - A Practical Approach To Avoiding The Most Common Mistakes
  • Build Your Private Cloud at Home
  • Secure Your Oracle Database Passwords in AWS RDS With a Password Verification Function
  • How to Achieve SOC 2 Compliance in AWS Cloud Environments
  1. DZone
  2. Coding
  3. Java
  4. ASCII Art Generator in Java

ASCII Art Generator in Java

By 
Ivan Korhner user avatar
Ivan Korhner
·
Feb. 28, 15 · Interview
Likes (1)
Comment
Save
Tweet
Share
14.3K Views

Join the DZone community and get the full member experience.

Join For Free

ascii art is a technique that uses printable characters from ascii standard to produce visual art. it had it’s purpose in history when printers lacked graphics ability and it was also used in emails when embedding images was yet not possible. i present you a very simple ascii art generator written in java with configurable font and contrast. since it was built over a few hours during the weekend, it is not optimal but it was a fun experiment. down below you can see the code in action and an explanation of how it works.

the algorithm

the idea is rather simple. first, we create an image of each character we want to use in our ascii art and cache it. then we go through the original image and for each block of size of the characters we search for the best fit. we do so by first doing some preprocessing of the original image: we convert the image to grayscale and apply a threshold filter. by doing so we get a black and white only contrasted image that we can compare with each character and calculate the difference. we then simply pick the most similar character and do so until the whole image is converted. it is possible to experiment with threshold value to impact contrast and enhance the final result as needed.

a very simple method to accomplish this is to set red, green and blue values to the average of all three:
red = green = blue = (red + green + blue) / 3
if that value is lower than a threshold value, we make it white, otherwise we make it black. finally, we compare that image with each character pixel by pixel and calculate average error. this is demonstrated in the images and snippet below:

_config.yml _config.yml

int r1 = (charpixel >> 16) & 0xff;
int g1 = (charpixel >> 8) & 0xff;
int b1 = charpixel & 0xff;

int r2 = (sourcepixel >> 16) & 0xff;
int g2 = (sourcepixel >> 8) & 0xff;
int b2 = sourcepixel & 0xff;

int thresholded = (r2 + g2 + b2) / 3 < threshold ? 0 : 255;

error = math.sqrt((r1 - thresholded) * (r1 - thresholded) + 
    (g1 - thresholded) * (g1 - thresholded) + (b1 - thresholded) * (b1 - thresholded));

since colors are stored in a single integer, we first extract individual color components and perform calculations i explained. another challenge was to measure character dimensions accurately and to draw them centered. after a lot of experimentation with different methods i finally found this good enough:

rectangle rect = new textlayout(character.tostring((char) i), fm.getfont(), 
    fm.getfontrendercontext()).getoutline(null).getbounds();

g.drawstring(character, 0, (int) (rect.getheight() - rect.getmaxy()));



you can download the complete source code from github repo .
here are a few examples with different font sizes and threshold:

_config.yml _config.yml _config.yml
_config.yml _config.yml _config.yml
_config.yml _config.yml _config.yml

ASCII art ASCII ARTS (radiative transfer code) Java (programming language)

Published at DZone with permission of Ivan Korhner, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Minions in Minikube - A Kubernetes Intro for Java Developers
  • Charsets and Unicode Identifiers in Java
  • Effective Exception Handling in Java and Spring Boot Applications
  • Code of Shadows: Master Shifu and Po Use Functional Java to Solve the Decorator Pattern Mystery

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • [email protected]

Let's be friends: