Try adding text to an image in Scala using the Java standard library

Overview

When trying to add text to an image in Scala, I think the easiest way to implement it is to use the Java standard library. So, basically, the implementation content is almost the same as Java, but I will introduce the sample code together with the referenced site.

reference

There are a few, but I will list the articles that I referred to.

-Scala Documentation --Image processing: Scala image reading / output documentation. -Read a JPEG file with Java, write figures and characters, and output: An implementation that adds characters to images is introduced. I will. -[Java] Image Editing-Combine images vertically and horizontally-: An implementation that combines images with images is introduced. .. -Draw a character string in the [Java] window: The implementation related to font setting is introduced. -Fill background: An implementation that fills the background is introduced. -[Java] Use of anti-aliasing: The implementation of anti-aliasing for characters is introduced. -Change the save quality of JPEG images in Java: The implementation to set the save quality of images is introduced.

Sample code

This is a sample code that adds characters to the side of the original image and outputs it. While saying Scala, I think it's almost the same as Java.

ImageWritingWordSample.scala


import java.awt.{Color, Font, RenderingHints}
import java.awt.image.BufferedImage
import java.io.{File, FileOutputStream}

import javax.imageio.{IIOImage, ImageIO, ImageWriteParam}

object ImageWritingWordSample extends App {
  //Loading the original image
  val originalImage = ImageIO.read(new File(s"resources/200x50.png "))

  //Incorporate the original image into the new image
  val newImage = new BufferedImage(320, 50, originalImage.getType)
  val graphics = newImage.createGraphics()
  graphics.drawImage(originalImage, 0, 0, null)

  //Background color where you write letters
  graphics.setColor(Color.WHITE)
  graphics.fillRect(200, 0, 320, 50)

  //Font settings
  graphics.setFont(new Font("Arial", Font.PLAIN, 12))
  //Letter color
  graphics.setColor(Color.BLACK)
  //Antialiasing
  graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON)
  //Add characters
  graphics.drawString("Test test", 210,20)
  graphics.drawString("Test test", 210,40)

  //Image output
  val os = new FileOutputStream("resources/new_320x50.jpeg ")
  val ios = ImageIO.createImageOutputStream(os)
  val writer = ImageIO.getImageWritersByFormatName("jpeg").next
  val param = writer.getDefaultWriteParam
  param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT)
  //Image quality settings
  param.setCompressionQuality(1.0f)
  writer.setOutput(ios)
  writer.write(null, new IIOImage(newImage, null, null), param)
  writer.dispose()
}

Execution result

【The original image】 200x50.png ** [Image after adding characters] ** new_320x50.jpeg

Recommended Posts

Try adding text to an image in Scala using the Java standard library
Try implementing the Eratosthenes sieve using the Java standard library
Try global hooking in Java using the JNativeHook library
Try using the Stream API in Java
[Java] Use cryptography in the standard library
[Swift] How to set an image in the background without using UIImageView.
Try using the COTOHA API parsing in Java
[Rails] How to display an image in the view
Try using RocksDB in Java
[Java] Try editing the elements of the Json string using the library
Try an If expression in Java
Welcome to the Java Library Swamp! !!
[Java] Try to implement using generics
Try to implement Yubaba in Java
[Java] Try to solve the Fizz Buzz problem using recursive processing
[Java] Adding an element to the Collection causes a compile error
How to solve the unknown error when using slf4j in Java
How to get the length of an audio file in java
Try to solve Project Euler in Java
Java reference to understand in the figure
Try to implement n-ary addition in Java
Map without using an array in java
Try using JSON format API in Java
Try calling the CORBA service in Java 11+
How to set an image in the drawable Left / Right of a button using an icon font (Iconics)
How to get the date in java
[Java] Color the standard output to the terminal
Try using the Wii remote with Java
Rails6: Extract the image in Action Text
How to return multiple values in Java methods (Generics, Record, Triple, Tuple, using standard library or external library)
NLP4J [004] Try text analysis using natural language processing and parsing statistical processing in Java
NLP4J [003] Try text analysis using natural language processing and part-speech statistical processing in Java
ChatWork4j for using the ChatWork API in Java
I want to send an email in Java.
3 ways to import the library in Android Studio
Try to create a bulletin board in Java
Second decoction: Try an If expression in Java
Try using Sourcetrail (win version) in Java code
Try using GCP's Cloud Vision API in Java
Try using Sourcetrail (macOS version) in Java code
Try similar search of Image Search using Java SDK [Search]
(Android) Try to display static text using DataBinding
Try accessing the dataset from Java using JZOS
Library "OSHI" to get system information in Java
Display "Hello World" in the browser using Java
Display "Hello World" in the browser using Java
How to solve an Expression Problem in Java
Push the image to docker hub using Jib
[Java] Try to solve the Fizz Buzz problem
Code used when you want to process Json with only the standard library in Java (improved version) gson unnecessary
[Android development] Get an image from the server in Java and set it in ImageView! !!
What to do if you can't get the text of an element in Selenium
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
Code to use when you want to process Json with only standard library in Java
How to make an image partially transparent in Processing
Output the maximum value from the array using Java standard output
[Java] How to omit the private constructor in Lombok
How to write Scala from the perspective of Java
I tried to create an Amazon echo skill that teaches scraped information in Java using the Alexa Skills Kit (ASK)
Try Image Search's similar search using Java SDK [Registration]
How to use Z3 library in Scala with Eclipse