Skip to content

Instantly share code, notes, and snippets.

View tsh-code's full-sized avatar

TSH code sharing tsh-code

View GitHub Profile
//add ar core
implementation 'com.google.ar:core:1.3.0'
implementation 'com.google.ar.sceneform.ux:sceneform-ux:1.3.0'
implementation 'com.google.ar.sceneform:core:1.3.0'
//in top of file
apply plugin: 'com.google.ar.sceneform.plugin'
<uses-feature android:name="android.hardware.camera.ar" android:required="true" />
<uses-permission android:name="android.permission.CAMERA" />
//and in <application> section:
<meta-data android:name="com.google.ar.core" android:value="required" />
// in onResume:
try {
if (mSession == null) {
switch (ArCoreApk.getInstance().requestInstall(this, mUserRequestedInstall)) {
case INSTALLED:
mSession = new Session(this);
// Success.
break;
case INSTALL_REQUESTED:
// Ensures next invocation of requestInstall() will either return
private fun loadAugmentedEarthImage(): Bitmap? {
try {
assets.open("earth.jpg").use { `is` -> return BitmapFactory.decodeStream(`is`) }
} catch (e: IOException) {
Log.e(TAG, "IO exception loading augmented image bitmap.", e)
}
return null
}
private fun setupAugmentedImageDb(config: Config): Boolean {
val augmentedImageDatabase = AugmentedImageDatabase(session)
val augmentedEarthImageBitmap = loadAugmentedEarthImage() ?: return false
augmentedImageDatabase.addImage("earth", augmentedEarthImageBitmap)
config.augmentedImageDatabase = augmentedImageDatabase
return true
}
private fun configureSession() {
val config = Config(session)
if (setupAugmentedImageDb(config).not()) {
messageSnackbarHelper.showError(this, "Could not setup augmented image database")
}
config.updateMode = Config.UpdateMode.LATEST_CAMERA_IMAGE
session.configure(config)
}
private fun onUpdateFrame() {
frame = surfaceview.arFrame
camera = frame.camera
val updatedAugmentedImages = frame.getUpdatedTrackables(AugmentedImage::class.java)
camera.getProjectionMatrix(projmtx, 0, 0.1f, 100.0f)
camera.getViewMatrix(viewmtx, 0)
for (augmentedImage in updatedAugmentedImages) {
if (augmentedImage.trackingState == TrackingState.TRACKING) {
surfaceview.scene.setOnUpdateListener { this.onUpdateFrame() }
<android.opengl.GLSurfaceView
android:id="@+id/surfaceviewGl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:visibility="invisible"/>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
surfaceviewGl.setZOrderOnTop(true)
surfaceviewGl.preserveEGLContextOnPause = true
surfaceviewGl.setEGLContextClientVersion(2)
surfaceviewGl.holder.setFormat(PixelFormat.RGBA_8888);
surfaceviewGl.setEGLConfigChooser(8, 8, 8, 8, 16, 0)
surfaceviewGl.setRenderer(this)