How to Use theta4j with Kotlin and the Plug-in WebAPI

theta4j can make it easier to use the WebAPI inside of the THETA Plug-in.

There’s some nice examples in both Kotlin and Java. The Kotlin examples are especially useful if you want to learn Kotlin. The primary example is in the folder plugin-example. It builds with no problems.

package org.theta4j.webapi.example.plugin

import android.os.Bundle
import android.view.KeyEvent
import com.theta360.pluginlibrary.activity.PluginActivity
import com.theta360.pluginlibrary.callback.KeyCallback
import com.theta360.pluginlibrary.receiver.KeyReceiver
import org.theta4j.webapi.CaptureMode
import org.theta4j.webapi.Options.CAPTURE_MODE
import org.theta4j.webapi.Theta
import java.util.concurrent.Executors

class MainActivity : PluginActivity() {

    private val executor = Executors.newSingleThreadExecutor()

    private val theta = Theta.createForPlugin()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setAutoClose(true);
    }

    override fun onResume() {
        super.onResume()
        setKeyCallback(keyCallback)

        executor.submit {
            theta.setOption(CAPTURE_MODE, CaptureMode.IMAGE)
        }
    }

    override fun onPause() {
        super.onPause()
        setKeyCallback(null)
    }

    private val keyCallback = object : KeyCallback {
        override fun onKeyDown(keyCode: Int, keyEvent: KeyEvent) {
            if (keyCode == KeyReceiver.KEYCODE_CAMERA) {
                executor.submit {
                    theta.takePicture()
                }
            }
        }

        override fun onKeyUp(keyCode: Int, keyEvent: KeyEvent) {
        }

        override fun onKeyLongPress(keyCode: Int, keyEvent: KeyEvent) {
        }
    }
}

The README has useful examples.

There’s a few points below that I used when creating a new plug-in from scratch.

Verify theta-plugin-library and theta-web-api

add to build.gradle Project

maven { url 'https://github.com/ricohapi/theta-plugin-library/raw/master/repository' }

add to build.gradle (Module: app)

    implementation 'com.theta360:pluginlibrary:2.0.0'
    implementation 'org.theta4j:theta-web-api:1.2.2'

Add compile options:

    android {
        compileOptions {
            sourceCompatibility 1.8
            targetCompatibility 1.8
        }
    }

Sync Gradle files.

image

Add Permission

In AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>

Press Run

image

1 Like