はじめに
Kotlin(Android)で画像をCropする。
ソース
https://www.fixes.pub/program/297572.html
上記の3#を参考。
sample.kt
private fun cropImage(bitmap: Bitmap, x: Int, y: Int, w, Int, h: Int): ByteArray {
val bitmapFinal= Bitmap.createBitmap(
bitmap,
x, y, w, h
)
val stream= ByteArrayOutputStream()
bitmapFinal.compress(
Bitmap.CompressFormat.JPEG,
100,
stream
) //100 is the best quality possibe
return stream.toByteArray()
}
備考
KotlinはAndroidと同義と思い込んでいたが、そうではなかった。
Javaパッケージと同じように使えるものが多いが、中には使えないものもある。
特にAWTもSwingなどのGUIはほとんど使えない。(ImageIO,BufferedImageなど)
https://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q10147763825
Androidをサポートしていないらしいライブラリ
https://sksamuel.github.io/scrimage/
https://github.com/coobird/thumbnailator
サポートしているという記述は見当たらなかった。
もしかしたら動かせるかも。
Androidで使えそうなCropライブラリ
https://github.com/igreenwood/SimpleCropView#download
https://github.com/Yalantis/uCrop
Androidをサポートしている旨が表記されている。