Install.
Download nosmai-detection.aar from the Android releases and place it in app/libs/. The published native release is 2.0.0.
Use Android API 24+, Kotlin 2.2+ and an arm64-v8a device.
android {
defaultConfig {
minSdk = 24
ndk { abiFilters += "arm64-v8a" }
}
}
dependencies {
implementation(files("libs/nosmai-detection.aar"))
}
The AAR includes its models and native inference libraries. If you use the CameraX ImageProxy overload or a live preview, add CameraX dependencies:
dependencies {
val camerax = "1.4.2"
implementation("androidx.camera:camera-core:$camerax")
implementation("androidx.camera:camera-camera2:$camerax")
implementation("androidx.camera:camera-lifecycle:$camerax")
implementation("androidx.camera:camera-view:$camerax")
}
Declare INTERNET. Declare and request CAMERA at runtime for live capture.
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.CAMERA" />
Only arm64-v8a native libraries are supplied. Test on a compatible physical device. Unsupported 32-bit and x86_64 devices cannot load the SDK. Inspect the final APK/AAB's ABI contents; packaging other dependencies for an unsupported ABI does not add Nosmai support for it.
Initialize
Use an app-owned background executor. Select NosmaiModel.NSFW explicitly.
import com.nosmai.detection.*
worker.execute {
val ready = NosmaiSDK.init(context, "NOSMAI-XXXX", NosmaiModel.NSFW)
if (!ready) {
// Show moderation unavailable; do not approve content.
return@execute
}
}
Select NosmaiModel.TEXT as well for chat, or call initText after license validation. An empty model list validates the license without loading models. Check initText(context) before chat even if TEXT was selected: overall initialization can succeed without propagating optional text-setup failure. See Authentication for enforcement behavior.
Image
Analyze a valid Bitmap off the main thread.
worker.execute {
val bitmap = BitmapFactory.decodeFile(localPath) ?: return@execute
try {
val result = NosmaiSDK.analyzeImage(bitmap)
activity.runOnUiThread { renderVerdict(result) }
} finally {
bitmap.recycle()
}
}
worker, activity and renderVerdict belong to your app. result.nsfw is SAFE, WARN or BLOCK; scores are in result.nsfwScores.
Recorded video
Pass a readable local Uri and a positive interval. The SDK handles its own background worker; progress and completion arrive on the main thread.
val task = NosmaiSDK.analyzeVideo(
context = context,
uri = localUri,
frameIntervalMs = 500L,
onProgress = { progress -> /* Update progress UI. */ },
onComplete = { result ->
if (result.framesAnalyzed == 0) {
// No completed samples; do not approve the video.
} else {
renderVideoVerdict(result.nsfw, result.nsfwFlaggedMs)
}
},
)
Call task.cancel() when the scan is no longer needed. Completion still runs with the samples processed so far; track cancellation in your UI and do not approve a partially analyzed clip. nsfwFlaggedMs lists BLOCK sample offsets. The decoder can return nearby keyframes, so offsets are not frame-perfect.
Text
worker.execute {
if (!NosmaiSDK.initText(context)) return@execute
val result = NosmaiSDK.moderateText("A message to check")
activity.runOnUiThread {
renderTextVerdict(result.blocked, result.category, result.layer)
}
}
Validate the license first. moderateText throws if the text engine was not initialized. It returns score and matchedWord as well as the verdict. A missing classifier can leave keyword-only moderation; see How moderation works.
CameraX live moderation
Start after successful initialization and camera permission. Your app owns camera binding, preview and lifecycle.
val listener = object : NosmaiListener {
override fun onResult(result: NosmaiResult) { /* Main-thread UI update. */ }
override fun onUnsafe(result: NosmaiResult) { /* Apply policy. */ }
override fun onSafe() { /* Recover UI. */ }
}
val options = NosmaiStreamOptions(
sampleEveryNFrames = 10,
requiredConsecutiveHits = 2,
unsafeHoldMs = 2_000L,
)
if (NosmaiSDK.startStream(listener, options)) {
val analysis = ImageAnalysis.Builder()
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.build()
analysis.setAnalyzer(cameraExecutor) { proxy -> NosmaiSDK.pushFrame(proxy) }
cameraProvider.bindToLifecycle(
owner, CameraSelector.DEFAULT_BACK_CAMERA, preview, analysis,
)
}
The ImageProxy overload applies its reported rotation and closes the proxy, including on decode failure. Do not close it a second time or retain it. At 30 pushed frames/sec, sampleEveryNFrames = 10 targets about three checks/sec; actual cadence also depends on inference and dropped busy frames. Default sampling is 1, not 10.
For another frame source, use pushFrame(bitmap, rotationDegrees). The SDK copies pixels before this call returns; you may then recycle your bitmap.
Lifecycle and configuration
Call NosmaiSDK.stopStream() when the screen stops and unbind its CameraX use cases. Resume deliberately when the screen is visible and active. Stopping moderation does not automatically release the camera your app owns.
NosmaiSDK.setNsfwThreshold(NosmaiNsfwClass.EXPLICIT, 0.60f) NosmaiSDK.setNsfwThreshold(NosmaiNsfwClass.SEXY, 0.55f) NosmaiSDK.setDebugLogging(true) // Integration debugging; disable for production.
The current API has no setPerformanceMode or object-class setThreshold. Use stream sampling to adjust live workload. Call NosmaiSDK.shutdown() on a worker when the whole SDK is no longer needed; initialize again before reuse.