Kotlin(Androidアプリ)で画像を回転する方法ついてサンプルコード付きでまとめました。
[latexpage]
## 画像を回転
次のようなRGBカラー画像を回転するAndroidアプリをKotlinで実装してみます。
– | 関連記事 |
---|---|
1 | ■【画像処理】アフィン変換による回転の原理・計算式 |
2 | ■【画像処理入門】アルゴリズム&プログラミング |
## 【ソースコード】Kotlin
Android Studio + Kotlinでの実装例です。
入力画像
入力画像(input.jpg)は\app\src\main\res\drawableに格納します。
MainActivity.kt
package com.sample.test.projectname // パッケージ名に応じて適宜変更 import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.widget.ImageView import android.widget.Button import android.graphics.Bitmap import android.graphics.BitmapFactory import android.graphics.Color class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val imageFrame : ImageView = findViewById(R.id.input) val runButton : Button = findViewById(R.id.run) val resetButton : Button = findViewById(R.id.reset) // 入力画像をロード var img1 : Bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.input) // まずBitmapインスタンスを生成 var img2 : Bitmap = Bitmap.createBitmap(img1) // 実行ボタンが押されたときの処理 runButton.setOnClickListener { // 回転 var img3 = rotate(img2) // グレースケール画像を画面にセット imageFrame.setImageBitmap(img3) img2 = img3 } // 初期化ボタンを押されたときの処理 resetButton.setOnClickListener { // 元画像を画面にセット imageFrame.setImageBitmap(img1) img2 = img1 } } // 回転 private fun rotate(srcImg : Bitmap) : Bitmap { // 出力画像用の配列 var dstImg : Bitmap = Bitmap.createBitmap(srcImg.height, srcImg.width, Bitmap.Config.ARGB_8888) // 画像の高さ・幅 val width : Int = srcImg.width val height : Int = srcImg.height // 画素値を格納する変数 var rgb : Int // ラスタ走査 for(j in 0..height - 1 step 1) { for (i in 0..width - 1 step 1) { rgb = srcImg.getPixel(i, j) dstImg.setPixel(height - 1 - j, i, rgb) } } return dstImg }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <ImageView android:layout_width="310dp" android:layout_height="313dp" app:srcCompat="@drawable/input" android:id="@+id/input" app:layout_constraintTop_toTopOf="parent" android:layout_marginTop="32dp" app:layout_constraintBottom_toBottomOf="parent" android:layout_marginBottom="128dp" app:layout_constraintStart_toStartOf="parent"/> /> <Button android:text="実行" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/run" app:layout_constraintTop_toBottomOf="@+id/input" app:layout_constraintEnd_toStartOf="@+id/reset" android:layout_marginEnd="44dp" android:layout_marginStart="44dp" app:layout_constraintHorizontal_bias="0.468" android:layout_marginTop="8dp"/> <Button android:text="初期化" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/reset" app:layout_constraintTop_toBottomOf="@+id/input" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="72dp" android:layout_marginRight="72dp" android:layout_marginTop="8dp"/> </android.support.constraint.ConstraintLayout>
– | 関連記事 |
---|---|
1 | ■【Kotlin】画像処理・編集・加工 |
2 | ■【Kotlin入門】使い方、アプリ開発、サンプル集 |
3 | ■【画像処理入門】アルゴリズム&プログラミング |
コメント