Advertisement
tchnmncr

illuminate.java

Dec 14th, 2023
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.97 KB | None | 0 0
  1. /* This code was generated by ChatGPT with this header comment added my me.
  2.  
  3. This code requires the necessary permissions for camera access and audio recording (for voice recognition), which need to be declared in your AndroidManifest.xml file:
  4.  
  5. <uses-permission android:name="android.permission.CAMERA" />
  6. <uses-feature android:name="android.hardware.camera" />
  7. <uses-feature android:name="android.hardware.camera.flash" />
  8. <uses-permission android:name="android.permission.RECORD_AUDIO" />
  9. <uses-permission android:name="android.permission.INTERNET" />
  10.  
  11. */
  12.  
  13. import android.Manifest;
  14. import android.content.pm.PackageManager;
  15. import android.hardware.camera2.CameraAccessException;
  16. import android.hardware.camera2.CameraManager;
  17. import android.os.Bundle;
  18. import android.speech.RecognizerIntent;
  19. import android.speech.SpeechRecognizer;
  20. import android.content.Intent;
  21. import android.speech.RecognitionListener;
  22. import android.support.annotation.NonNull;
  23. import android.support.v4.app.ActivityCompat;
  24. import android.support.v7.app.AppCompatActivity;
  25. import android.widget.Toast;
  26. import java.util.ArrayList;
  27. import java.util.Locale;
  28.  
  29. public class MainActivity extends AppCompatActivity {
  30.  
  31.     private SpeechRecognizer speechRecognizer;
  32.     private CameraManager cameraManager;
  33.     private String cameraId;
  34.  
  35.     @Override
  36.     protected void onCreate(Bundle savedInstanceState) {
  37.         super.onCreate(savedInstanceState);
  38.         setContentView(R.layout.activity_main);
  39.  
  40.         cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
  41.         try {
  42.             cameraId = cameraManager.getCameraIdList()[0];
  43.         } catch (CameraAccessException e) {
  44.             e.printStackTrace();
  45.         }
  46.  
  47.         speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
  48.         Intent speechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
  49.         speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
  50.         speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
  51.  
  52.         speechRecognizer.setRecognitionListener(new RecognitionListener() {
  53.             @Override
  54.             public void onResults(Bundle bundle) {
  55.                 ArrayList<String> data = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
  56.                 if (data != null && data.get(0).equalsIgnoreCase("Illuminate")) {
  57.                     toggleFlashLight();
  58.                 }
  59.             }
  60.  
  61.             // Implement other necessary methods of RecognitionListener interface
  62.         });
  63.  
  64.         if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
  65.             ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, 1);
  66.         } else {
  67.             speechRecognizer.startListening(speechRecognizerIntent);
  68.         }
  69.     }
  70.  
  71.     private void toggleFlashLight() {
  72.         try {
  73.             if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
  74.                 cameraManager.setTorchMode(cameraId, true); // Turn on
  75.                 Toast.makeText(this, "Flashlight turned on", Toast.LENGTH_SHORT).show();
  76.             }
  77.         } catch (CameraAccessException e) {
  78.             e.printStackTrace();
  79.         }
  80.     }
  81.  
  82.     @Override
  83.     protected void onDestroy() {
  84.         super.onDestroy();
  85.         if (speechRecognizer != null) {
  86.             speechRecognizer.destroy();
  87.         }
  88.     }
  89.  
  90.     @Override
  91.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  92.         super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  93.         if (requestCode == 1 && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  94.             speechRecognizer.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));
  95.         }
  96.     }
  97. }
  98.  
  99.  
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement