AWS with Android

Project Overview

Use Cases

Services Used

Basic Setup

Additional Dependencies

Basic AWS Setup

Working

package com.datadit.vision;import com.amazonaws.regions.Regions;
// Class for storing all the AWS Credentials
public class AWSCredentials {
// IAM Authorisation
private static final String COGNITO_POOL_ID = "Enter your POOL ID here";
private static final Regions COGNITO_REGION = "Mention your Region here";
public static String getCognitoPoolId() {
return COGNITO_POOL_ID;
}
public static Regions getCognitoRegion() {
return COGNITO_REGION;
}
}

// Encoded byte array is passed to AWS Rekognition
public HashMap<String, String> recogniseImage() {

AmazonRekognition client = new AmazonRekognitionClient(credentialsProvider);
DetectTextRequest request = new DetectTextRequest()
.withImage(new Image()
.withBytes(ByteBuffer.wrap(bytes)));

try {
DetectTextResult result = client.detectText(request);
List<TextDetection> textDetections = result.getTextDetections();

Log.d(LOG_TAG, "List : " + textDetections);
if (textDetections.isEmpty()) {
listIsEmpty = true;
Log.d(LOG_TAG, "List is Empty");
}

for (TextDetection text : textDetections) {
rekognitionMap.put(text.getDetectedText(), text.getConfidence().toString());
}

} catch (Exception e) {
Log.d(LOG_TAG, "Exception : " + e);
e.printStackTrace();
}
return rekognitionMap;
}

// Extracted text is passed onto AWS Translate
public Void translateText(String text) {
Log.d(LOG_TAG, "Text Received for translation : " + text);
AWSCredentials awsCredentials = new AWSCredentials() {
@Override
public String getAWSAccessKeyId() {
return "Specify your AWS Access Key here";
}

@Override
public String getAWSSecretKey() {
return "Specify your AWS Secret Key here";
}
};

AmazonTranslateAsyncClient translateAsyncClient = new AmazonTranslateAsyncClient(awsCredentials);
final TranslateTextRequest translateTextRequest = new TranslateTextRequest()
.withText(text)
.withSourceLanguageCode("auto")
.withTargetLanguageCode("en");
translateAsyncClient.translateTextAsync(translateTextRequest, new AsyncHandler<TranslateTextRequest, TranslateTextResult>() {
@Override
public void onError(Exception e) {
Log.e(LOG_TAG, "Error occurred in translating the text: " + e.getLocalizedMessage());
translatedText = languageNotDetected;
PollyAsyncTask pollyAsyncTask = new PollyAsyncTask();
pollyAsyncTask.execute(languageNotDetected);
}

@Override
public void onSuccess(TranslateTextRequest request, TranslateTextResult translateTextResult) {
translatedText = translateTextResult.getTranslatedText() + " ";
Log.d(LOG_TAG, "Translated Text : " + translatedText);
PollyAsyncTask pollyAsyncTask = new PollyAsyncTask();
pollyAsyncTask.execute(translatedText);
}
});

return null;
}

// Translated text is passed onto AWS Polly
public Void convertTextToSpeech(String text) {

AmazonPollyPresigningClient client = new AmazonPollyPresigningClient(credentialsProvider);

SynthesizeSpeechPresignRequest synthesizeSpeechPresignRequest = new SynthesizeSpeechPresignRequest()
.withText(text)
.withVoiceId(voiceAssistant)
.withOutputFormat(OutputFormat.Mp3);
Log.d(LOG_TAG, "Voice :" + voiceAssistant);

URL presignedSynthesizeSpeechUrl =
client.getPresignedSynthesizeSpeechUrl(synthesizeSpeechPresignRequest);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource(presignedSynthesizeSpeechUrl.toString());
} catch (IOException e) {
Log.e(LOG_TAG, "Unable to set data source for media player" + e.getMessage());
}

mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
Log.d(LOG_TAG, "Text To Speech Translation complete");
}
});
return null;
}

Final App

Additional Notes –

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store