com.cloudinary.upload.request.params.Coordinates.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-uploader Show documentation
Show all versions of kotlin-uploader Show documentation
Cloudinary is a cloud service that offers a solution to a web application's entire image management pipeline. Upload images to the cloud. Automatically perform smart image resizing, cropping and conversion without installing any complex software. Integrate Facebook or Twitter profile image extraction in a snap, in any dimension and style to match your websiteâs graphics requirements. Images are seamlessly delivered through a fast CDN, and much much more. This Java library allows to easily integrate with Cloudinary in Kotlin applications.
The newest version!
package com.cloudinary.upload.request.params
import com.squareup.moshi.JsonClass
@JsonClass(generateAdapter = true)
data class Coordinates(val coordinates: List) {
constructor(rect: IntArray) : this(mutableListOf(Rectangle(rect[0], rect[1], rect[2], rect[3])))
constructor(vararg rects: Rectangle) : this(rects.toMutableList())
constructor(stringCoords: String) : this(parseString(stringCoords))
companion object {
@Throws(IllegalArgumentException::class)
fun parse(coordinates: Any): Coordinates {
return when (coordinates) {
is Coordinates -> coordinates
is IntArray -> Coordinates(coordinates)
is Rectangle -> Coordinates(coordinates)
else -> Coordinates(coordinates.toString())
}
}
}
}
private fun parseString(stringCoords: String): MutableList {
val coordinates = mutableListOf()
for (stringRect in stringCoords.split("\\|").toTypedArray()) {
if (stringRect.isEmpty()) continue
val elements = stringRect.split(",").toTypedArray()
require(elements.size == 4) {
String.format(
"Must supply exactly 4 values for coordinates (x,y,width,height) %d supplied: %s",
elements.size, stringRect
)
}
coordinates.add(
Rectangle(
elements[0].toInt(),
elements[1].toInt(),
elements[2].toInt(),
elements[3].toInt()
)
)
}
return coordinates
}
@JsonClass(generateAdapter = true)
data class Rectangle(val x: Int, val y: Int, val width: Int, val height: Int)