Skip to content

Instantly share code, notes, and snippets.

@kiwiandroiddev
Created April 5, 2020 01:46
Show Gist options
  • Save kiwiandroiddev/5f49317353e54988a4de8604cbdb6f9c to your computer and use it in GitHub Desktop.
Save kiwiandroiddev/5f49317353e54988a4de8604cbdb6f9c to your computer and use it in GitHub Desktop.
Decode an Android test resource image as an OpenCV Mat
/**
* Uses OpenCV to decode an image that is an android test resource i.e.
* a file from `src/androidTest/resources`.
* Note that the resulting Mat will be empty if there was a problem loading the file.
*/
private fun loadOpenCvImage(testResourceFilename: String): Mat {
val bytes: ByteArray = javaClass.classLoader!!.getResource(testResourceFilename).readBytes()
val matOfByte = MatOfByte().apply { fromList(bytes.toList()) }
return Imgcodecs.imdecode(matOfByte, CV_LOAD_IMAGE_COLOR)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment