1
2

More than 3 years have passed since last update.

Kotlin(Android)でJpg画像をCropする

Posted at

はじめに

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をサポートしている旨が表記されている。

1
2
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
1
2