Use scala jar

8 July 2017 ยท 2 minute read

One week ago I wrote my first program on Scala. It’s was an image processing filter.

After this week I implemented seven filters:

  1. Mirror image
  2. Gray filter
  3. Sepia filter
  4. Inverse filter
  5. Split image to RGB channels
  6. Color accent filter
  7. Histogram

I was writing it like a one scala script per file. But today I changed repository to SBT project. And I learned how compile and how use jar file.

If you want to use my library of image processing you should:

$ git clone https://github.com/janczer/filters
$ cd filters
$ sbt run
$ sbt package

And in directory target/scala-2.12/ you will have file with name like filters_X.XX-X.X.jar. If you want to use it in your scala project you just need add jar file to lib directory. But if you want to use it in scala script (this is the way I’m testing my library) you should add path to jar.

For testing my filters I use this script:

import janczer.filters.Filters
import java.io.File
import javax.imageio.ImageIO

def test() {
  val photoIn = ImageIO.read(new File("test.jpg"))


  ImageIO.write(Filters.mirror(photoIn, true, true), "jpg", new File("mirror.jpg"))
  ImageIO.write(Filters.gray(photoIn, "lightness"), "jpg", new File("gray.jpg"))
  ImageIO.write(Filters.sepia(photoIn, 35), "jpg", new File("sepia.jpg"))
  ImageIO.write(Filters.inverse(photoIn), "jpg", new File("inverse.jpg"))
  ImageIO.write(Filters.rgb_channels(photoIn, true, false, false), "jpg", new File("rgb_channels.jpg"))
  ImageIO.write(Filters.color_accent(photoIn, 0, 50), "jpg", new File("color_accent.jpg"))
  ImageIO.write(Filters.histogram(photoIn), "jpg", new File("histogram.jpg"))

}

test()

This is the way how I run it:

$ scala -classpath "filters_2.12-0.2.jar" test.scala
comments powered by Disqus
github