Last active
March 18, 2020 07:49
-
-
Save ytabuchi/36659e76d63abcd7d7a9ae5b109cef75 to your computer and use it in GitHub Desktop.
マーカーと ImageNode を AsyncTask で取得するサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.xlsoft.kudanar | |
import android.graphics.Bitmap | |
import android.graphics.BitmapFactory | |
import android.os.AsyncTask | |
import android.os.Bundle | |
import android.util.Log | |
import eu.kudan.kudan.* | |
import java.io.* | |
import java.lang.Exception | |
import java.net.HttpURLConnection | |
import java.net.URL | |
class MarkerActivity: ARActivity() { | |
lateinit var imageTrackable: ARImageTrackable | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_marker) | |
} | |
override fun setup() { | |
super.setup() | |
// Image を Web から取得後に保存して、ARImageTrackable に指定する | |
AddImageTrackableAsync().execute() | |
// Image を Web から取得後に保存して、ARImageNode に指定する | |
AddImageNodeAsync().execute() | |
} | |
inner class AddImageTrackableAsync: AsyncTask<Void, Void, Bitmap?>() { | |
private val legoUrl = "https://kudanlicenses.z20.web.core.windows.net/lego.jpg" | |
// バックグラウンドで行う処理 | |
override fun doInBackground(vararg param: Void): Bitmap? { | |
lateinit var bitmap: Bitmap | |
val url = URL(legoUrl) | |
val connection: HttpURLConnection = url.openConnection() as HttpURLConnection | |
try { | |
val inputStream: InputStream = connection.inputStream | |
bitmap = BitmapFactory.decodeStream(inputStream) | |
inputStream.close() | |
} catch (ex: Exception) { | |
Log.d(localClassName, ex.message!!) | |
} | |
finally { | |
connection.disconnect() | |
} | |
return bitmap | |
} | |
// `doInBackground` が終わった後の処理 | |
override fun onPostExecute(result: Bitmap?) { | |
super.onPostExecute(result) | |
// アプリ内部に画像ファイルとして保存 | |
val file = File(applicationContext.filesDir, "dl_lego.jpg") // "/data/user/0/com.xlsoft.kudanar/files/dl_lego.jpg" | |
val fileOutputStream = FileOutputStream(file) | |
result?.let { | |
try { | |
it.compress(Bitmap.CompressFormat.JPEG, 90, fileOutputStream) | |
addImageTrackable(file.path) | |
} catch (ex: Exception) { | |
Log.d(localClassName, ex.message!!) | |
} finally { | |
fileOutputStream.close() | |
} | |
} | |
} | |
private fun addImageTrackable(markerFilePath: String) { | |
// ARImageTrackable をインスタンス化して画像を読み込み | |
imageTrackable = ARImageTrackable() | |
imageTrackable.loadFromPath(markerFilePath) | |
// ARImageTracker のインスタンスを取得して初期化 | |
val trackableManager = ARImageTracker.getInstance() | |
trackableManager.initialise() | |
// imageTrackable を ARImageTracker に追加 | |
trackableManager.addTrackable(imageTrackable) | |
} | |
} | |
inner class AddImageNodeAsync: AsyncTask<Void, Void, Bitmap?>() { | |
private val cowUrl = "https://kudanlicenses.z20.web.core.windows.net/cow.png" | |
// バックグラウンドで行う処理 | |
override fun doInBackground(vararg param: Void): Bitmap? { | |
lateinit var bitmap: Bitmap | |
val url = URL(cowUrl) | |
val connection: HttpURLConnection = url.openConnection() as HttpURLConnection | |
try { | |
val inputStream: InputStream = connection.inputStream | |
bitmap = BitmapFactory.decodeStream(inputStream) | |
inputStream.close() | |
} catch (ex: Exception) { | |
Log.d(localClassName, ex.message!!) | |
} | |
finally { | |
connection.disconnect() | |
} | |
return bitmap | |
} | |
// `doInBackground` が終わった後の処理 | |
override fun onPostExecute(result: Bitmap?) { | |
super.onPostExecute(result) | |
// アプリ内部に画像ファイルとして保存 | |
val file = File(applicationContext.filesDir, "dl_cow.png") // "/data/user/0/com.xlsoft.kudanar/files/dl_cow.png" | |
val fileOutputStream = FileOutputStream(file) | |
result?.let { | |
try { | |
it.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream) | |
addImageNode(file.path) | |
} catch (ex: Exception) { | |
Log.d(localClassName, ex.message!!) | |
} finally { | |
fileOutputStream.close() | |
} | |
} | |
} | |
private fun addImageNode(imageFilePath: String) { | |
// ARImageNode を画像を指定して初期化 | |
val imageNode = ARImageNode().initWithPath(imageFilePath) | |
// imageNode のサイズを Trackable のサイズに合わせる | |
val textureMaterial = imageNode.material as ARTextureMaterial | |
val scale = imageTrackable.width / textureMaterial.texture.width | |
imageNode.scaleByUniform(scale) | |
// imageNode を trackable の world に追加 | |
imageTrackable.world.addChild(imageNode) | |
imageNode.visible = true | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment