LoginSignup
0
1

More than 5 years have passed since last update.

scalaでJavaCV使ってみたかっただけ

Last updated at Posted at 2016-06-25

手順

  1. IntelliJ ideaでsbtプロジェクトを作成する
  2. build.sbtを編集する
  3. plugin.sbtを編集する

build.sbt

// @formatter:off

name         := "sample"
organization := "com.github.cive"


scalaVersion  := "2.11.8"
scalacOptions ++= Seq("-unchecked", "-deprecation", "-Xlint")

classpathTypes += "maven-plugin"

libraryDependencies ++= Seq(
  "org.scala-lang.modules" % "scala-swing_2.11" % "1.0.1",
  "junit"                   % "junit"           % "4.12" % "test",
  "com.novocode"            % "junit-interface" % "0.11" % "test"
)

resolvers ++= Seq(
  Resolver.sonatypeRepo("snapshots"),
  // Use local maven repo for local javacv builds
  "Local Maven Repository" at "file:///" + Path.userHome.absolutePath + "/.m2/repository"
)

autoCompilerPlugins := true

fork := true
javaOptions += "-Xmx1G"

shellPrompt in ThisBuild := { state => "sbt:" + Project.extract(state).currentRef.project + "> " }

plugin.sbt

  • sbt-javacvのバージョン1.7ではエラーが出たので1.6を使用
logLevel := Level.Warn

addSbtPlugin("org.bytedeco" % "sbt-javacpp" % "1.4")
addSbtPlugin("org.bytedeco" % "sbt-javacv" % "1.6")

sample.scala

import java.awt.image.BufferedImage
import java.awt.{AWTException, GraphicsEnvironment, Robot}
import java.io.IOException
import javax.swing.JFrame

import org.bytedeco.javacpp.opencv_core._
import org.bytedeco.javacv._

object mainApp extends App {
  val image: Mat = try {
    val bounds = GraphicsEnvironment.getLocalGraphicsEnvironment.getMaximumWindowBounds
    val robot = new Robot
    val screen: BufferedImage = robot.createScreenCapture(bounds)
    new OpenCVFrameConverter.ToMat().convert(new Java2DFrameConverter().convert(screen))
  } catch {
    case e: AWTException => {
      println("awt" + e)
      new Mat(1, 1, CV_8UC3)
    }
    case e: IOException  => {
      println("io" + e)
      new Mat(1, 1, CV_8UC3)
    }
  }
  val canvas = new CanvasFrame("My Image", 1)

  canvas.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

  val converter = new OpenCVFrameConverter.ToMat()
  canvas.showImage(converter.convert(image))
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1