You need to implement Speech to Text
RecognizerIntent
in android , RecognizerIntent
will give the text version of what user said and later you can use the result text result.contains("Launch camera")
to verify that result text contains your desired action , it will basically give you a list of possible results and you can go through all to match your input
@Override
public void onResults(Bundle results) {
List<String> list=results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for(String s: list)
{
if(s.contains("Launch camera"))
{
dispatchTakePictureIntent()
// call the function to take picture
break;
}
}
}
add the below code into your class
// constant variable for request code
static final int REQUEST_IMAGE_CAPTURE = 1;
// function to open the camera app using explicit intent
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
And later you will receive the response in onActivityResult
from where you can fetch your image from data intent
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// verify the successful completion of picture taken
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
// get the bundle
Bundle extras = data.getExtras();
// imageBitmap will hold the image you are looking for
Bitmap imageBitmap = (Bitmap) extras.get("data");
// optionally you can display this pic in some ImageView or can delete this line
mImageView.setImageBitmap(imageBitmap);
}
}
Code implementation
Android Speech to Text Example
Taking Photos Simply
Can I use "contains" for a text ?
Yes you can use contains
to match and verify result that you found , a required text input from user. You should use contains
because equals
will only work when you found an exact word to word match and there can be some noise which can effect the response text so safer option is to use contains
How can I make it respond it first and turning into text later?
Basically using RecognizerIntent
, the input has been already converted into text and you also need the text input to display it later into your TextView
so simply you can launch camera intent first and display the text after the picture is taken so store the match content into some String
variable and later use that variable to display it into your TextView
String inputcommand="";
@Override
public void onResults(Bundle results) {
List<String> list=results.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
for(String s: list)
{
if(s.contains("Launch camera"))
{
// store your match in global variable to use it later
inputcommand = s;
dispatchTakePictureIntent()
// respond it first
break;
}
}
}
After the picture has been successfully capture then you can display the text
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
... // same above mentioned code
// display your input in TextView
Text.setText(inputcommand );
}
}