現在kotlinnにてアンドロイドのスマホで写真を撮って保存するコードを書いています。
そこで撮った写真をOpencvを用いて(今後トリミングなども考えているため)グレースケールにしたいと考えています。
調べてもいまいちピンとこず悩んでいます。
以下がコードになります
kotlin初心者なので汚い部分もありますが、どなたかぼ教授いただけると幸いです。
Kotlin
1package com.android.example.carnocamerax 2 3 4import android.content.Context 5import android.content.Intent 6import android.os.Build 7import android.os.Bundle 8import android.view.KeyEvent 9import androidx.appcompat.app.AppCompatActivity 10import androidx.core.view.WindowCompat 11import androidx.core.view.WindowInsetsCompat 12import androidx.core.view.WindowInsetsControllerCompat 13import androidx.localbroadcastmanager.content.LocalBroadcastManager 14import com.android.example.carnocamerax.databinding.ActivityMainBinding 15import java.io.File 16 17//import org.opencv.android; 18//import org.opencv.core.Mat; 19//import org.opencv.android; 20 21 22const val KEY_EVENT_ACTION = "key_event_action" 23const val KEY_EVENT_EXTRA = "key_event_extra" 24private const val IMMERSIVE_FLAG_TIMEOUT = 500L 25 26/** 27 * Main entry point into our app. This app follows the single-activity pattern, and all 28 * functionality is implemented in the form of fragments. 29 */ 30class MainActivity : AppCompatActivity() { 31 32 private lateinit var activityMainBinding: ActivityMainBinding 33 34 override fun onCreate(savedInstanceState: Bundle?) { 35 super.onCreate(savedInstanceState) 36 activityMainBinding = ActivityMainBinding.inflate(layoutInflater) 37 setContentView(activityMainBinding.root) 38 } 39 40 41 override fun onResume() { 42 super.onResume() 43 // Before setting full screen flags, we must wait a bit to let UI settle; otherwise, we may 44 // be trying to set app to immersive mode before it's ready and the flags do not stick 45 activityMainBinding.fragmentContainer.postDelayed({ 46 hideSystemUI() 47 }, IMMERSIVE_FLAG_TIMEOUT) 48 } 49 50 51 /** When key down event is triggered, relay it via local broadcast so fragments can handle it */ 52 override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean { 53 return when (keyCode) { 54 KeyEvent.KEYCODE_VOLUME_DOWN -> { 55 val intent = Intent(KEY_EVENT_ACTION).apply { putExtra(KEY_EVENT_EXTRA, keyCode) } 56 LocalBroadcastManager.getInstance(this).sendBroadcast(intent) 57 true 58 } 59 else -> super.onKeyDown(keyCode, event) 60 } 61 } 62 63 64 override fun onBackPressed() { 65 if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) { 66 // Workaround for Android Q memory leak issue in IRequestFinishCallback$Stub. 67 // (https://issuetracker.google.com/issues/139738913) 68 finishAfterTransition() 69 } else { 70 super.onBackPressed() 71 } 72 } 73 74 75 companion object { 76 77 /** Use external media if it is available, our app's file directory otherwise */ 78 fun getOutputDirectory(context: Context): File { 79 val appContext = context.applicationContext 80 val mediaDir = context.externalMediaDirs.firstOrNull()?.let { 81 File(it, appContext.resources.getString(R.string.app_name)).apply { mkdirs() } } 82 return if (mediaDir != null && mediaDir.exists()) 83 mediaDir else appContext.filesDir 84 } 85 } 86 87 88 private fun hideSystemUI() { 89 WindowCompat.setDecorFitsSystemWindows(window, false) 90 WindowInsetsControllerCompat(window, activityMainBinding.fragmentContainer).let { controller -> 91 controller.hide(WindowInsetsCompat.Type.systemBars()) 92 controller.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE 93 } 94 } 95} 96
重要なコードが抜けていました、申し訳ないです
kotlin
1package com.example.carno 2 3import android.util.Log 4import java.io.File 5import java.io.IOException 6import java.util.* 7import it.sauronsoftware.ftp4j.FTPClient 8import it.sauronsoftware.ftp4j.FTPException 9import kotlinx.coroutines.* 10 11class FTPTransfer { 12 var filename = "" 13 14 fun SetFileName( TEST:String ){ 15 filename = TEST 16 17 } 18 19// private suspend fun FtpAsyncTask( filename : String ){ 20 fun FtpAsyncTask( filename : String ){ 21// fun FtpAsyncTask(){ 22 // onPreExecuteと同等の処理 23// withContext(Dispatchers.Main) { 24//// Log.d( localClassName, "始めます") 25// } 26 var myFTPClient = FTPClient() 27 try { 28// myFTPClient.connect("192.168.11.240", 21) 29// myFTPClient.login("FC-USER", "subaru") 30 myFTPClient.connect("192.168.76.98", 21 ) 31 myFTPClient.login("ftptest", "unitec2244") 32 myFTPClient.type = FTPClient.TYPE_BINARY 33 myFTPClient.isPassive = true 34 myFTPClient.changeDirectory("./") 35 36 var filename2 = filename 37 val uploadfile = File(filename2) 38 myFTPClient.upload(uploadfile) 39 myFTPClient.logout() 40 myFTPClient.disconnect(true) 41 } catch (e: Exception) { 42 println(e.message) 43 } catch (e: FTPException) { 44 println(e.message) 45 } finally { 46 if (myFTPClient.isConnected()) { 47 try { 48 myFTPClient.disconnect(true) 49 } catch (e: IOException) { 50 } 51 } 52// myFTPClient = null 53 } 54 } 55}
あなたの回答
tips
プレビュー