|
| 1 | +package com.example.menumap; |
| 2 | + |
| 3 | +import androidx.annotation.Nullable; |
| 4 | +import androidx.appcompat.app.AppCompatActivity; |
| 5 | + |
| 6 | +import android.os.Bundle; |
| 7 | +import android.speech.RecognizerIntent; |
| 8 | +import android.text.TextUtils; |
| 9 | +import android.util.Log; |
| 10 | +import android.view.View; |
| 11 | +import android.widget.ImageView; |
| 12 | +import android.widget.TextView; |
| 13 | +import android.content.Intent; |
| 14 | +import android.widget.Button; |
| 15 | +import android.widget.Toast; |
| 16 | + |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.Locale; |
| 19 | + |
| 20 | +import androidx.annotation.Nullable; |
| 21 | +import androidx.annotation.NonNull; |
| 22 | +import androidx.annotation.RequiresApi; |
| 23 | +import androidx.appcompat.app.AppCompatActivity; |
| 24 | +import com.google.android.gms.tasks.OnFailureListener; |
| 25 | +import com.google.android.gms.tasks.OnSuccessListener; |
| 26 | +import com.google.android.gms.tasks.Task; |
| 27 | +import com.google.firebase.ml.common.modeldownload.FirebaseModelDownloadConditions; |
| 28 | +import com.google.firebase.ml.naturallanguage.FirebaseNaturalLanguage; |
| 29 | +import com.google.firebase.ml.naturallanguage.languageid.FirebaseLanguageIdentification; |
| 30 | +import com.google.firebase.ml.naturallanguage.languageid.FirebaseLanguageIdentificationOptions; |
| 31 | +import com.google.firebase.ml.naturallanguage.translate.FirebaseTranslateLanguage; |
| 32 | +import com.google.firebase.ml.naturallanguage.translate.FirebaseTranslator; |
| 33 | +import com.google.firebase.ml.naturallanguage.translate.FirebaseTranslatorOptions; |
| 34 | +import com.google.firebase.ml.vision.FirebaseVision; |
| 35 | +import com.google.firebase.ml.vision.common.FirebaseVisionImage; |
| 36 | +import com.google.firebase.ml.vision.text.FirebaseVisionText; |
| 37 | +import com.google.firebase.ml.vision.text.FirebaseVisionTextRecognizer; |
| 38 | + |
| 39 | +public class AudioActivity extends AppCompatActivity { |
| 40 | + |
| 41 | + private TextView audioResult; |
| 42 | + private ImageView buttonSpeak; |
| 43 | + private Button backButton; |
| 44 | + private String mTranslateText; |
| 45 | + private String mResultText; |
| 46 | + |
| 47 | + @Override |
| 48 | + protected void onCreate(Bundle savedInstanceState) { |
| 49 | + super.onCreate(savedInstanceState); |
| 50 | + setContentView(R.layout.activity_audio); |
| 51 | + audioResult = findViewById(R.id.audioResult); |
| 52 | + buttonSpeak = findViewById(R.id.buttonSpeak); |
| 53 | + buttonSpeak.setOnClickListener(new View.OnClickListener() { |
| 54 | + @Override |
| 55 | + public void onClick(View view) { |
| 56 | + getSpeechInput(); |
| 57 | + } |
| 58 | + }); |
| 59 | + backButton = findViewById(R.id.backButton); |
| 60 | + backButton.setOnClickListener(new View.OnClickListener() { |
| 61 | + @Override |
| 62 | + public void onClick(View view) { |
| 63 | + finish(); |
| 64 | + } |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + public void getSpeechInput() { |
| 69 | + Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); |
| 70 | + //intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); |
| 71 | + |
| 72 | + intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "zh"); |
| 73 | + intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "zh"); |
| 74 | + intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_PREFERENCE, "zh"); |
| 75 | + intent.putExtra(RecognizerIntent.EXTRA_SUPPORTED_LANGUAGES, "zh"); |
| 76 | + intent.putExtra(RecognizerIntent.EXTRA_ONLY_RETURN_LANGUAGE_PREFERENCE,"zh"); |
| 77 | + intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, "zh"); |
| 78 | + intent.putExtra(RecognizerIntent.EXTRA_RESULTS, "zh"); |
| 79 | + |
| 80 | + if(intent.resolveActivity(getPackageManager()) != null) { |
| 81 | + startActivityForResult(intent, 10); |
| 82 | + } else { |
| 83 | + Toast.makeText(this, "Your Device Does Not Support Speech Input", Toast.LENGTH_SHORT).show(); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { |
| 89 | + super.onActivityResult(requestCode, resultCode, data); |
| 90 | + |
| 91 | + switch (requestCode) { |
| 92 | + case 10: |
| 93 | + if(resultCode == RESULT_OK && data != null) { |
| 94 | + ArrayList<String> result = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); |
| 95 | + mResultText = TextUtils.join(", ", result); |
| 96 | + identifyLanguage(mResultText); |
| 97 | + audioResult.setText(mTranslateText); |
| 98 | + } |
| 99 | + break; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public void identifyLanguage(String text) { |
| 104 | + FirebaseLanguageIdentification languageIdentifier = |
| 105 | + FirebaseNaturalLanguage.getInstance().getLanguageIdentification(); |
| 106 | + languageIdentifier.identifyLanguage(text) |
| 107 | + .addOnSuccessListener( |
| 108 | + new OnSuccessListener<String>() { |
| 109 | + @Override |
| 110 | + public void onSuccess(@Nullable String languageCode) { |
| 111 | + if (languageCode != "und") { |
| 112 | + downloadTranslator(languageCode); |
| 113 | + } else { |
| 114 | + mTranslateText = "Can't identify language."; |
| 115 | + audioResult.setText(mTranslateText); |
| 116 | + } |
| 117 | + } |
| 118 | + }) |
| 119 | + .addOnFailureListener( |
| 120 | + new OnFailureListener() { |
| 121 | + @Override |
| 122 | + public void onFailure(@NonNull Exception e) { |
| 123 | + mTranslateText = "Internal error. Make sure your wifi is connected."; |
| 124 | + audioResult.setText(mTranslateText); |
| 125 | + } |
| 126 | + }); |
| 127 | + } |
| 128 | + |
| 129 | + public void downloadTranslator(String text) { |
| 130 | + int sourceLanguage = 0; |
| 131 | + try { |
| 132 | + sourceLanguage = FirebaseTranslateLanguage |
| 133 | + .languageForLanguageCode(text); |
| 134 | + } catch (NullPointerException e) { |
| 135 | + e.printStackTrace(); |
| 136 | + } |
| 137 | + FirebaseTranslatorOptions options = |
| 138 | + new FirebaseTranslatorOptions.Builder() |
| 139 | + .setSourceLanguage(sourceLanguage) |
| 140 | + .setTargetLanguage(FirebaseTranslateLanguage.EN) |
| 141 | + .build(); |
| 142 | + final FirebaseTranslator langTranslator = |
| 143 | + FirebaseNaturalLanguage.getInstance().getTranslator(options); |
| 144 | + |
| 145 | + FirebaseModelDownloadConditions conditions = new FirebaseModelDownloadConditions.Builder() |
| 146 | + .requireWifi() |
| 147 | + .build(); |
| 148 | + langTranslator.downloadModelIfNeeded(conditions) |
| 149 | + .addOnSuccessListener( |
| 150 | + new OnSuccessListener<Void>() { |
| 151 | + @Override |
| 152 | + public void onSuccess(Void v) { |
| 153 | + Log.d("translator", "downloaded lang model"); |
| 154 | + |
| 155 | + translateText(langTranslator); |
| 156 | + } |
| 157 | + }) |
| 158 | + .addOnFailureListener( |
| 159 | + new OnFailureListener() { |
| 160 | + @Override |
| 161 | + public void onFailure(@NonNull Exception e) { |
| 162 | + mTranslateText = "Download failed."; |
| 163 | + audioResult.setText(mTranslateText); |
| 164 | + } |
| 165 | + }); |
| 166 | + } |
| 167 | + |
| 168 | + public void translateText(FirebaseTranslator langTranslator) { |
| 169 | + |
| 170 | + langTranslator.translate(mResultText) |
| 171 | + .addOnSuccessListener( |
| 172 | + new OnSuccessListener<String>() { |
| 173 | + @Override |
| 174 | + public void onSuccess(@NonNull String translatedText) { |
| 175 | + mTranslateText = translatedText; |
| 176 | + audioResult.setText(mTranslateText); |
| 177 | + |
| 178 | + } |
| 179 | + }) |
| 180 | + .addOnFailureListener( |
| 181 | + new OnFailureListener() { |
| 182 | + @Override |
| 183 | + public void onFailure(@NonNull Exception e) { |
| 184 | + |
| 185 | + } |
| 186 | + }); |
| 187 | + |
| 188 | + } |
| 189 | + |
| 190 | +} |
0 commit comments