【Android】画像をトリミングする
こんにちは,ここあです.今回は,画像をトリミングしたり回転したりっていう処理の実装について語ろうと思います.
1.外部アプリを用いる
実は日本語の情報はあまりありませんが,画像アプリを使用してトリミングするためのintentが用意されています.android.camera.action.CROP
がそれです.然るべきデータを渡してあげれば動作する,,,らしいですが外部アプリを使用するということはAndroid N以降複雑になったファイル共有周りの制約をモロにうけます.fileprovider
っていうやつです.というか,fileproviderで取得できるUriのスキームっていろいろあるんですけど,ものによってはうまく共有できなかったりします.てことで説明しません!!
2.ライブラリを用いる
本題はこちらです.トリミングのためのライブラリがあるのでそれを使いましょう.同一アプリ内なのでfile:///
スキームを利用しても怒られません.もしかしたら偉い人が怒ってくるかもしれませんが知ったこっちゃありません.
環境
この記事を書いている段階で使用している環境は以下のとおりです.
- Android Studio
- Android O
- UCrop
ライブラリ設定
build.gradle
にリポジトリとか登録しましょう.話はそれからです.全体のgradleファイルとアプリ固有のgradleファイルそれぞれに追記する必要があります.
allprojects { repositories { google() jcenter() maven { url "https://jitpack.io" } } }
dependencies { implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.core:core-ktx:1.3.0' ........... implementation 'com.github.yalantis:ucrop:2.2.6-native' }
AndroidManifest.xmlの設定
AndroidManifest.xml
に、UCropのActiviryを登録します.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="testapp"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity> ........ </activity> <!-- uCrop --> <activity android:name="com.yalantis.ucrop.UCropActivity" android:screenOrientation="portrait" android:theme="@style/Theme.AppCompat.Light.NoActionBar"/> </application> </manifest>
機能を組み込む
設定が終わったら機能を組み込んでいきます.下記のコード中にはファイルを選択するためのコードが含まれていますが,今回の趣旨とは異なるため説明は省きます.いや,本質の部分も正直言って説明が必要なほどのものじゃない気がするので省いていいですか?
override fun onCreate(savedInstanceState: Bundle?){ findViewById<Button>(R.id.selectImage).setOnClickListener { val intent = Intent(Intent.ACTION_GET_CONTENT) intent.type = "image/*" startActivityForResult(intent, GET_COTENT_CODE) } } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { if (requestCode == GET_COTENT_CODE){ if(resultCode != Activity.RESULT_OK){ return } var srcFileUri = data?.data!! val tmpFileName = UUID.randomUUID().toString() + ".png" File.createTempFile(tmpFileName, null, cacheDir) val tmpFileUri = Uri.fromFile(File(cacheDir, tmpFileName)) var options = com.yalantis.ucrop.UCrop.Options() options.setToolbarTitle("切り抜き") options.setCompressionFormat(Bitmap.CompressFormat.PNG) var uCrop = UCrop.of(srcFileUri, tmpFileUri) uCrop.withOptions(options) uCrop.start(this) }else if(requestCode == UCrop.REQUEST_CROP){ if(resultCode == Activity.RESULT_OK) { var imgView = findViewById<ImageView>(R.id.ImgView) val bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(UCrop.getOutput(data!!)!!.encodedPath), imgView.width, imgView.height, true) runOnUiThread { imgView.setImageBitmap(bitmap) } }else if(resultCode == UCrop.RESULT_ERROR){ Log.e("TAG", "uCropエラー: "+UCrop.getError(data!!).toString()) } } super.onActivityResult(requestCode, resultCode, data) }
最近のコメント