Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* This code was generated by ChatGPT with this header comment added my me.
- 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:
- <uses-permission android:name="android.permission.CAMERA" />
- <uses-feature android:name="android.hardware.camera" />
- <uses-feature android:name="android.hardware.camera.flash" />
- <uses-permission android:name="android.permission.RECORD_AUDIO" />
- <uses-permission android:name="android.permission.INTERNET" />
- */
- import android.Manifest;
- import android.content.pm.PackageManager;
- import android.hardware.camera2.CameraAccessException;
- import android.hardware.camera2.CameraManager;
- import android.os.Bundle;
- import android.speech.RecognizerIntent;
- import android.speech.SpeechRecognizer;
- import android.content.Intent;
- import android.speech.RecognitionListener;
- import android.support.annotation.NonNull;
- import android.support.v4.app.ActivityCompat;
- import android.support.v7.app.AppCompatActivity;
- import android.widget.Toast;
- import java.util.ArrayList;
- import java.util.Locale;
- public class MainActivity extends AppCompatActivity {
- private SpeechRecognizer speechRecognizer;
- private CameraManager cameraManager;
- private String cameraId;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
- try {
- cameraId = cameraManager.getCameraIdList()[0];
- } catch (CameraAccessException e) {
- e.printStackTrace();
- }
- speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
- Intent speechRecognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
- speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
- speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
- speechRecognizer.setRecognitionListener(new RecognitionListener() {
- @Override
- public void onResults(Bundle bundle) {
- ArrayList<String> data = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
- if (data != null && data.get(0).equalsIgnoreCase("Illuminate")) {
- toggleFlashLight();
- }
- }
- // Implement other necessary methods of RecognitionListener interface
- });
- if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
- ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO}, 1);
- } else {
- speechRecognizer.startListening(speechRecognizerIntent);
- }
- }
- private void toggleFlashLight() {
- try {
- if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
- cameraManager.setTorchMode(cameraId, true); // Turn on
- Toast.makeText(this, "Flashlight turned on", Toast.LENGTH_SHORT).show();
- }
- } catch (CameraAccessException e) {
- e.printStackTrace();
- }
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- if (speechRecognizer != null) {
- speechRecognizer.destroy();
- }
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- if (requestCode == 1 && grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
- speechRecognizer.startListening(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement