Spren Vision

Spren Vision Android SDK

the android sdk is in alpha we're working quickly to expand our support in the heterogeneity of android devices device support and recommendations compatible devices (tested) there may be other compatible, but untested android devices that are not contained in this list google pixel 3xl, 4, 4a, 5, 6 , 6 pro , 7 , 7 pro xiaomi redmi 9, note 10 pro huawei mate 20 samsung galaxy note 9, a51, s9, s10, s10+, s20 fe, s21 ultra, s22, s22+, s22 ultra motorola moto g 5g plus (2020 model) lg velvet oneplus 7 pro, 9 5g devices with an asterisk are ones that use the wide angle camera for readings all other devices use the default camera incompatible devices (tested) huawei p20 lite redmi note 11 google pixel 6a recommendations flash currently, we allow users to only perform readings with flash on hardware we recommend using an android device that is not a low ram device, has at least 8 cores, and has 192mb or more ram available to your app val activitymanager = getsystemservice(context activity service) as activitymanager val ishighperformingdevice = !activitymanager islowramdevice && runtime getruntime() availableprocessors() >= 8 && activitymanager memoryclass >= 192 30fps is acceptable, but for best accuracy and ux, we recommend devices that support 60fps in camerax note that 60fps or better may be listed in manufacturer's device specifications, thus, be supported in the native camera app, but be unavailable to camerax end user rub fingertips and hands to warm up ensure adequate room lighting wait a few seconds for reading to initialize installation visit the spren vision android sdk maven repository https //search maven org/search?q=com spren to install the sdk via the gradle build tool we encourage using the latest remote binary available for more information, see the android docs for add build dependencies https //developer android com/studio/build/dependencies add a camera usage permission to your app's manifest for more information, request app permissions https //developer android com/training/permissions/requesting the open source code is available in the spren vision android sdk github repository https //github com/elite hrv/spren vision android spren ui finger camera example // mainactivity kt import com spren sprenui sprenui class mainactivity appcompatactivity() { private lateinit var binding activitymainbinding private lateinit var hardwarealert hardwarealert override fun oncreate(savedinstancestate bundle?) { super oncreate(savedinstancestate) // optionally set custom theme // theme inherits from "theme materialcomponents daynight noactionbar" // see themes xml example below settheme(r style theme sprenui) binding = activitymainbinding inflate(layoutinflater) setcontentview(binding root) // optionally check if hardware is compatible hardwarealert = hardwarealert(this) if (!hardwarecheck ishighperformingdevice(this)) { hardwarealert show() } // set user id sprenui config userid = // optionally set user biological sex sprenui config usergender = // optionally set user birthdate sprenui config userbirthdate = // after dismissing results screen sprenui config onfinish = { results, > // handle completion of reading ui flow } // user presses x sprenui config oncancel = { // handle user exit of ui flow without completing a reading } // optionally override default intro screen graphics // provide drawable ids for image sets in project // all are required for each project if overriding sprenui config graphics = mapof( sprenui graphic greeting 1 to \<image set drawable id>, // greeting screen 1 sprenui graphic greeting 2 to \<image set drawable id>, // greeting screen 2 sprenui graphic finger on camera to \<image set drawable id>, // finger on camera instruction screen sprenui graphic no camera to \<image set drawable id>, // camera access authorization denied screen sprenui graphic server error to \<image set drawable id> // server or calculation error ) } } body composition example // mainactivity kt import com spren sprenui sprenui class mainactivity appcompatactivity() { private lateinit var binding activitymainbinding private lateinit var hardwarealert hardwarealert override fun oncreate(savedinstancestate bundle?) { super oncreate(savedinstancestate) // optionally set custom theme // theme inherits from "theme materialcomponents daynight noactionbar" // see themes xml example below settheme(r style theme sprenui) binding = activitymainbinding inflate(layoutinflater) setcontentview(binding root) // optionally check if hardware is compatible hardwarealert = hardwarealert(this) if (!hardwarecheck ishighperformingdevice(this)) { hardwarealert show() } // set user id sprenui config userid = // optionally set user biological sex sprenui config usergender = // optionally set user birthdate sprenui config userbirthdate = // after dismissing results screen sprenui config onfinish = { , results > // user completed a scan! print(results) // dismiss sprenui } sprenui config oncancel = { // handle user exit of ui flow without completing a scan } // optionally override default intro screen graphics // provide drawable ids for image sets in project // all are required for each project if overriding sprenui config graphics = mapof( sprenui graphic greetings to \<image set drawable id>, sprenui graphic privacy to \<image set drawable id>, sprenui graphic camera access denied \<image set drawable id>, sprenui graphic photos access denied to \<image set drawable id>, sprenui graphic setup guide to \<image set drawable id>, sprenui graphic server error to \<image set drawable id>, sprenui graphic incorrect body position to \<image set drawable id> ) } } layout and theme \<! activity main xml corresponding to mainactivity kt > \<?xml version="1 0" encoding="utf 8"?> \<com spren sprenui sprenui xmlns\ android="http //schemas android com/apk/res/android" xmlns\ app="http //schemas android com/apk/res auto" xmlns\ tools="http //schemas android com/tools" android\ layout width="match parent" android\ layout height="match parent" app\ api key="@string/api key" app\ base url="@string/base url" app\ project="finger camera" or "body composition" tools\ context=" mainactivity" /> \<! themes xml > \<resources xmlns\ tools="http //schemas android com/tools"> \<style name="apptheme base" parent="theme materialcomponents daynight noactionbar"> \<! button color > \<item name="colorprimary">#6200ee\</item> \<! button text color > \<item name="coloronprimary">@android\ color/white\</item> \<! graphics color > \<item name="colorsecondary">#03dac5\</item> \</style> \</resources> sprencapture and sprencore implementation example here is an example of how to implement the spren vision android sdk in your own app refer to the comments throughout the file to see what functions get called at what point import com spren sprencapture sprencapture import com spren sprencore import com spren sprencore finger compliance compliancecheck import com spren sprencore event sprenevent import com spren sprencore event spreneventmanager / sprencapture / // configure camera and set up camera preview sprencapture = sprencapture(activity, previewview\ surfaceprovider) sprencapture? start() / sprencore / // handle state transitions private fun statelistener(values hashmap\<string, any>) { when (values\["state"]) { sprenstate started > // handle reading started ui update startstate() sprenstate finished > // successful reading! // make post request to the spren api // with readingdata to get insights! finishstate() sprenstate cancelled > // handle user cancelled ui update cancelstate() sprenstate error > // handle error ui update errorstate() // call sprencapture reset when the app is ready to start a new reading } } // subscribe to state events spreneventmanager subscribe(sprenevent state, statelistener) // unsubscribe when leaving the flow spreneventmanager unsubscribe(sprenevent state, statelistener) // handle prereading compliance checks private fun compliancelistener(values hashmap\<string, any>) { val name = values\["name"] as compliancecheck name val compliant = values\["iscompliant"] as boolean when (name) { compliancecheck name lens coverage > // optionally update ui to reflect lens coverage compliance handlelenscoveragecompliance( name, compliant, ) compliancecheck name exposure > // optionally update ui to reflect exposure compliance handleexposurecompliance( name, compliant, ) } } // subscribe to compliance events spreneventmanager subscribe(sprenevent compliance, compliancelistener) // unsubscribe when leaving the flow spreneventmanager unsubscribe(sprenevent compliance, compliancelistener) // handle progress changes private fun progresslistener(values hashmap\<string, any>){ // handle progress ui update val progress = values\["progress"] as int updateprogress(progress) } // subscribe to progress events spreneventmanager subscribe(sprenevent progress, progresslistener) // unsubscribe when leaving the flow spreneventmanager unsubscribe(sprenevent progress, progresslistener) breaking changes in 2 x sprencapture var autostart = true (replace with sprencapture reset method) enable or disable reading autostart autostart occurs after 3 seconds of conditions checks compliance spren fun settorchmode(torch boolean) boolean (replace with fun turnflashon() ) attempts to toggle the torch (flashlight) on as appropriate returns the resulting torch mode setting the flash off has been disabled fun dropcomplexity() boolean (no need to handle anymore) this function has been deprecated and will be removed in the next releases fun handleoverexposure() (no need to handle anymore) attempts to reduce the exposure of the image by lowering the sensor exposure time this may be called if exposure is non compliant, i e, at least 5 frames are over exposed sprencapture library sprencapture is where you can initialize a camera preview and first configure the camera this will start a camera preview and image analysis, allow you to add the camera preview to your ui, and handle various other camera controls check out the function definitions below! sprencapture fun start() boolean attempts to create a preview and imageanalysis use cases returns false when the camera does not support at least 30 fps for more information, see the android docs for camerax overview https //developer android com/training/camerax fun stop() stops the preview and imageanalysis use cases fun reset() starts or restarts a reading reading will start after 3 seconds of conditions checks compliance this function needs to be called after fun start() boolean method and after subscribing to spren events fun turnflashon() attempts to toggle the torch (flashlight) on rgbanalyzer rgbanalyzer is an imageanalysis analyzer and handles providing frames to sprencore sprencore library the sprencore library provides internal control over readings and allows you to gain information on what's going on internally in the sdk so your ui can be updated accordingly here you'll be able to set reading durations, handle progress updates, start and stop readings, and more use this library in conjunction with sprencapture to utilize the full capabilities of spren sdk spreneventmanager subscribe spreneventmanager subscribe(sprenevent state, statelistener) subscribes to events when state changes occur, i e , started , finished , cancelled , and error fun statelistener(values hashmap\<string, any>) hashmap key value state sprenstate spreneventmanager subscribe(sprenevent compliance, compliancelistener) subscribes to events when a compliance check is performed compliance checks for lens coverage, and exposure are performed once per second fun compliancelistener(values hashmap\<string, any>) hashmap key value name compliancecheck name iscompliant boolean spreneventmanager subscribe(sprenevent progress, progresslistener) subscribes to events when progress updates progress ranges from 0 to 99 in integer increments state change to finished occurs in lieu of progress update at 100 fun progresslistener(values hashmap\<string, any>) hashmap key value progress int spreneventmanager unsubscribe unsubscribing events before leaving the flow spreneventmanager unsubscribe(sprenevent state, statelistener) spreneventmanager unsubscribe(sprenevent compliance, compliancelistener) spreneventmanager unsubscribe(sprenevent progress, progresslistener) spren fun spren companion startreading() manually start the reading fun spren companion cancelreading() manually stop the reading fun getreadingdata(context context) string? after spren transitions to finished, reading data may be retrieved to hit the spren api for results see the https //docs spren com/user and sdk data post endpoint for more information var readingdurationseconds = 90 reading duration ≥ 90 seconds or ≤ 240 seconds fun spren companion setreadingduration(duration int) set the reading duration a duration in the range ≥ 90 seconds or ≤ 240 seconds must be provided or the call returns if not using sprencapture if you'd like to use your own library or code to handle camera configurations and initialization, make sure to reference this section and the rgbanalyzer section to get more context and see what functions need to be implemented reference rgbanalyzer fun process(frame bitmap, timestamp long) provide a frame for processing bitmap created from imageproxy e g val bitmap = imageproxy image!! tobitmap() the imageproxy format will be pixelformat rgba 8888, which has only one image plane (r, g, b, a pixel by pixel) for more information, image analysis https //developer android com/training/camerax/analyze compliance checks compliancecheck compliance checks are run at 1 second intervals as frames are provided, i e , if internally to sprencore the time when the frame is received is >1 second later than the last check for compliancecheck name lens coverage finger must cover lens with light pressure exposure less than 30 frames must have overexposure a frame is over exposed when it appears brighter than it should