Я пробую API Rekognition из aws в моем новом Android-приложении в kotlin, но когда я пытаюсь создать клиент, мое приложение вылетает.Я положил файл JSON в сырой папке.Это мой код:
private var credentialsProvider: AWSCredentialsProvider? = null
private var awsConfiguration: AWSConfiguration? = null
//==============================================================================================
// Activity Methods
//==============================================================================================
/**
* Initializes the UI and initiates the creation of a face detector.
*/
public override fun onCreate(icicle: Bundle?) {
super.onCreate(icicle)
setContentView(R.layout.main)
AWSMobileClient.getInstance().initialize(this) {
credentialsProvider = AWSMobileClient.getInstance().credentialsProvider
awsConfiguration = AWSMobileClient.getInstance().configuration
Log.d(TAG, "AWSMobileClient is initialized")
setCollectionImage()
}.execute()
}
private fun getBitmapFromAsset(strName: String): Bitmap? {
val assetManager = this.assets
val istr: InputStream
var bitmap: Bitmap? = null
try {
istr = assetManager.open(strName)
bitmap = BitmapFactory.decodeStream(istr)
} catch (e: IOException) {
}
return bitmap
}
private fun getBytesFromBitmap(bitmap: Bitmap?): ByteArray {
val stream = ByteArrayOutputStream()
bitmap!!.compress(Bitmap.CompressFormat.JPEG, 70, stream)
return stream.toByteArray()
}
private fun setCollectionImage() {
val rekognitionClient = AmazonRekognitionClient(credentialsProvider!!.credentials)
//val client = AmazonRekognitionClient(credentialsProvider!!.credentials)
val pictureByteArray = getBytesFromBitmap(getBitmapFromAsset("IMG_20180611_125536.jpg"))
val collectionId = "ConstanceID"
val imageByteBuffer = ByteBuffer.wrap(pictureByteArray)
val personName = "Constance" // nom de la personne qu'on souhaite sauvegarder
val request = IndexFacesRequest()
.withCollectionId(collectionId)
.withExternalImageId(personName)
.withImage(Image().withBytes(imageByteBuffer))
val result = rekognitionClient.indexFaces(request)
val recorded = result.faceRecords.isEmpty().not()
}