Created
December 15, 2020 00:33
-
-
Save kiwiandroiddev/cc89f2b3294e7de29dd17eead1c3b096 to your computer and use it in GitHub Desktop.
Returns an Observable stream of screen grabs using java.awt.Robot. Uses multiple threads to get a somewhat reasonable FPS as per https://github.com/bahusvel/JavaScreenCapture
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun getScreenCaptureSource( | |
screenRectangle: Rectangle, | |
numThreads: Int = 6, | |
fpsPerThread: Int = 8, | |
robot: Robot = Robot() | |
): Observable<BufferedImage> { | |
val periodPerThreadMs: Long = 1000 / fpsPerThread.toLong() | |
val perThreadOffsetMs: Long = periodPerThreadMs / numThreads | |
val sources = (0 until numThreads).map { i -> | |
val initialDelayMs = i * perThreadOffsetMs | |
Observable.interval(initialDelayMs, periodPerThreadMs, TimeUnit.MILLISECONDS) | |
.observeOn(Schedulers.newThread()) | |
.map { robot.createScreenCapture(screenRectangle) } | |
} | |
return Observable.merge(sources) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment