1

I am new to Android development. I am trying to build a voice assistant and I want it to open the camera when I say a specific words. I know how to turn speech to text but I am stuck at opening the camera.

if (Text.getText().toString().equals("Launch camera")){
  saySomething("Launching camera");
  // How do I make it launch the camera?
}

Edit :

So far, my app turns speech to text and then it looks for if it is equal to a command and respond it that way. I have 2 questions about this.

  1. Can I use "contains" for a text ?

  2. How can I make it respond it first and turning into text later?

Code

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case RESULT_SPEECH: {
            if (resultCode == RESULT_OK && null != data) {
                ArrayList<String> text = data
                        .getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
                Text.setText(text.get(0));
            }
            if (Text.getText().toString().equals("Hello")){
                saySomething("Hello");
            }
        }

    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

4

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 );
    }
}
Community
  • 1
  • 1
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • I already have that but i couldn't figure out how to make it open the camera app – Burak Demirelli Dec 28 '16 at 18:34
  • @BurakDemirelli let me update my answer for more clarity but you have to study "Taking Photos Simply" link part i.e. "Get the Thumbnail" – Pavneet_Singh Dec 28 '16 at 18:36
  • @BurakDemirelli if this work for you then you can accept the answer to thank me by clicking on the tick icon on left , happy coding and yeah you can ask more things unless they are too big for comments – Pavneet_Singh Dec 28 '16 at 18:43
  • I made my app turn speech into text and then I made it check if it was equal to the voice command i didn't directly make it search if it contains a command and now i am not sure if I can include "contains".If it is hard to explain in comments i can ask it as a question? – Burak Demirelli Dec 28 '16 at 18:53
  • @BurakDemirelli you can add more details about your doubts in your post and give it a tittle as "Edit" – Pavneet_Singh Dec 28 '16 at 18:57
  • I know I am asking a lot but i edited this question so i can make it more understandable – Burak Demirelli Dec 28 '16 at 19:09
  • @BurakDemirelli i told you to put the new content in edit section , now it is completely a new question , so let me edit so that i don't have to remove my complete answer and always do this – Pavneet_Singh Dec 28 '16 at 19:11
  • I'm sorry I messed everything.Thank you for fixing the question – Burak Demirelli Dec 28 '16 at 19:20
  • @BurakDemirelli by this `How can I make it respond it first and turning into text later` you mean , you want to take picture first and then you want to display the text in textview ? – Pavneet_Singh Dec 28 '16 at 19:21
  • actually not quite I am not sure if I can explain it but I will try.I want it to respond directly fro speech without turning it into speech but I also want to make it display in the textview (I don't want my program to respond according to the text, i want it to respond using the voice ) – Burak Demirelli Dec 28 '16 at 19:31
  • @BurakDemirelli eventually the speech input needs to be converted into text and don't forget you also want to display it as text , so what you are saying it is something like voice recolonization , where you match the voices – Pavneet_Singh Dec 28 '16 at 19:37
  • 1
    Thank you soooo much!You helped me a lot this project would take ages without this help. – Burak Demirelli Dec 28 '16 at 19:37
  • @BurakDemirelli i am glad that i could help , happy coding :) – Pavneet_Singh Dec 28 '16 at 19:39
  • Cannot resolve method "contains (java.lang.String) This is the code: if (Text.contains("Hello")){} – Burak Demirelli Dec 28 '16 at 20:17
  • Try reading my first code example and read this http://googleweblight.com/?lite_url=http://www.javatpoint.com/java-string-contains&ei=tQVU-dQ3&lc=en-IN&s=1&m=131&host=www.google.co.in&ts=1482979208&sig=AF9NedmhymhAbnMjyIPOcCviQqofx5nFVg. – Pavneet_Singh Dec 29 '16 at 02:44
  • Hi, it is me again can you have a look at this question :) http://stackoverflow.com/questions/41415437/how-to-use-another-speech-to-text-engine-than-default-googles-or-samsung-and-e – Burak Demirelli Jan 01 '17 at 13:31