Codetown ::: a software developer's community
Welcome to Kotlin Thursdays and Happy Halloween! This week, we will be exploring image processing in Kotlin. If you have used applications like Instagram, Snapchat, or even Photoshop, you may already be familiar with image processing and not even know it!
Image processing is not specific to Kotlin, but playing with images can help us think about how algorithms are applied in a 2-D matrix and develop computational thinking. Once we get the basics down, the world is your oyster!
We will be leveraging TornadoFX, a JavaFX framework written in Kotlin for native desktop development. Let's get started setting up our TornadoFX project!
Think of these resources as supplemental if you happen to be more curious. We always encourage looking into documentation for things you use!
In the spirit of open-source languages and resources, it's only a natural choice to use IntelliJ Community edition.
First, make sure you're set up with TornadoFX on your IntelliJ. If you don't have IntelliJ, you can download it for free here!
Choose any photo, or take this one below and save it in a resource folder in your project!
1. On IntelliJ, go to Preferences > Plugins and make sure Kotlin and TornadoFX is installed.
2. General consensus amongst TornadoFXers is that Maven is easier to user - and Maven is great as long as you do not require lot of dependencies for your project. Otherwise, it's recommended to use Gradle. For the scope of this project, it is not necessary to understand how to use either of build tools, but don't hesitate to leave comments/questions/concerns here if you happen to run into any trouble throughout the project. Let's go ahead and select tornadofx-maven-project, which will nicely set up much of our project immediately.
3. If you haven't already, go to your pom.xml file and check to see if your kotlin version is updated. As of November 1st, 2018, the latest Kotlin version is 1.3.0, but feel free to refer to your plugins to check versioning. If you don't have your changes imported automatically, hit "Import Changes". If it builds successfully, you should be good to go!
In your source code, you'll notice a brief hierarchy - within com.example.demo, you have an app folder which stores the files MyApp and Styles, and you also have a view folder which contains the MainView.
App
To create a TornadoFX application, you must have at least one class that extendsApp
. An App
is the entry point to the application and specifies the initial View
.
package com.example.demo.app
import com.example.demo.view.MainView
import tornadofx.App
class MyApp: App(MainView::class, Styles::class)
Stylesheet
Traditionally in JavaFX, a stylesheet is defined in a plain CSS text file included in the project. However, TornadoFX allows creating stylesheets with pure Kotlin code. This has the benefits of compilation checks, auto-completion, and other perks that come with statically-typed code. A companion object is declared to hold class-level properties that can easily be retrieved throughout the app. Then, the init() block is used to apply styling to classes. For more on styling, see Type-Safe CSS.
package com.example.demo.app
import ...
class Styles: Stylesheet( ) {
companion object {
val heading by cssclass()
}
init {
label and heading {
padding = box(10.px)
fontSize = box(20.px)
fontWeight = FontWeight.BOLD
}
}
View
A View contains the display logic as well as a layout of Nodes. TornadoFX provides a builder syntax that will streamline your UI code. These builders allow us to express the UI as a hierarchical structure, which enables you to visualize the resulting UI easily. Note that all builders are written in lowercase, so as not to confuse them with manual instantiation of the UI element classes.
We're going to modify the code to render our rooster image. Always write a little code and check if it works. You don't want to keep writing something that is broken in the first place!
package com.example.demo.app
import ...
class MainView: View("I am a chicken") {
override val root = hbox {
imageview("rooster.png")
hboxConstraints {
prefWidth = 640.0
prefHeight = 427.0
}
}
}
According to TornadoFX documentation on images, it doesn't appear much different from JavaFX Images and ImageViews, but a TornadoFX allows far less syntax in programming over JavaFX:
Image image = new Image(new FileInputStream("path of image"));
ImageView imageView = new ImageView(image);
imageView.setX(50);
imageView.setY(25);
imageView.setFitHeight(455);
imageView.setFitWidth(500);
Group root = new Group(imageView);
Scene scene = new Scene(root, 600, 500);
}
Java implementation
imageview("tornadofx.jpg")
scaleX = . 50
scaleY = . 50
}
Kotlin implementation
Let's click run and make sure we're able to render the method!
Noice.
Now that we've figured out how to render an image, we're going to talk about how we can access pixels in an image. Images are essentially 2-dimensional matrixes in which we are able to iteratively access an image's pixel value by row and column. We then can apply operations to those pixels and alter the image as a whole.
We can use JavaFX's WritableImage to allow a canvas while storing the URI as an Image. This way, we are given access to PixelReader and PixelWriter. Super convenient!
package com.example.demo.app
import ...
class MainView: View("I am a chicken") {
private val image = Image("rooster.png")
private val width = image.width.toInt( )
private val height = image.width.toInt( )
private val width = WritableImage(image.pixelReader width, height)
override val root = hbox {
imageview(wImage).apply {
makeDuller(wImage)
makeDuller(wImage)
makeDuller(wImage)
}
hboxConstraints {
prefWidth = 640.0
prefHeight = 427.0
}
}
private fun makeDuller = (image: WritableImage) {
val pixelWriter = image.pixelWriter
for ( x in 0 until width) {
for ( y in 0 until height) {
val color = image.pixelReader.getColor(x, y)
pixelWriter.setColor(x, y, color.desaturate( ))
}
}
}
}
}
We used Kotlin's apply three times because we wanted to make sure we could REALLY see the difference. In Kotlin, you can use the call apply where the scope calls this (itself) as the receiver and the result is the object with applied functions.
In the function makeDuller, we access the color of an individual pixel that we read and subsequently use pixelWriter to set that pixel a different color. Double noice.
We've figured out how to grab individual pixels of an image and manipulate them. Next week, we'll be going over pixel math and using math pixel to create our own image processing filters. Stay tuned!
Having a blast? Check out part 2 of this series!
Tags:
Codetown is a social network. It's got blogs, forums, groups, personal pages and more! You might think of Codetown as a funky camper van with lots of compartments for your stuff and a great multimedia system, too! Best of all, Codetown has room for all of your friends.
Created by Michael Levin Dec 18, 2008 at 6:56pm. Last updated by Michael Levin May 4, 2018.
Check out the Codetown Jobs group.
During the recent Developer Week, Cloudflare announced that the object storage R2 now supports event notifications, which automatically trigger Workers in response to data changes. Additionally, the migration service Super Slurper now extends its support to Google Cloud Storage and a new infrequent access storage tier is available in private beta.
By Renato LosioGoogle has released Android 15 Beta. This version brings loudness control, screen recording detection, edge-to-edge apps by default, improvements for satellite connectivity and OpenJDK core libraries, new SQLite Apis, and more.
By Diogo CarletoMicrosoft recently announced significant updates to the Govern section of its Cloud Adoption Framework (CAF) for Azure, enhancing cloud governance guidance across various domains, including identity, cost management, and AI, to support better organizations, from startups to large enterprises, in their cloud journey.
By Steef-Jan WiggersIn this podcast Shane Hastie, Lead Editor for Culture & Methods spoke to Cassie Shum, VP of Field Engineering at Relational AI, about the importance of empathy in engineering culture and the key elements of building a strong team.
By Cassandra ShumSergey Bykov discusses the concept of Durable Execution, with a real world example of how they used it to build the Control Plane for Temporal Cloud.
By Sergey Bykov© 2024 Created by Michael Levin. Powered by