How to turn on front flash light programmatically in Android?

后端 未结 11 1613
执笔经年
执笔经年 2020-11-22 00:17

I want to turn on front flash light (not with camera preview) programmatically in Android. I googled for it but the help i found referred me to this page

Does anyo

11条回答
  •  攒了一身酷
    2020-11-22 00:51

    Complete Code for android Flashlight App

    Manifest

      
      
    
          
    
          
          
    
          
              
                  
                      
    
                      
                  
              
          
    
      
    

    XML

    
    
        

    MainActivity.java

      import android.app.AlertDialog;
      import android.content.DialogInterface;
      import android.content.pm.PackageManager;
      import android.hardware.Camera;
      import android.hardware.Camera.Parameters;
      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      import android.view.View;
      import android.widget.Button;
    
      import java.security.Policy;
    
      public class MainActivity extends AppCompatActivity {
    
          Button button;
          private Camera camera;
          private boolean isFlashOn;
          private boolean hasFlash;
          Parameters params;
    
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
    
              button = (Button) findViewById(R.id.button);
    
              hasFlash = getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
    
              if(!hasFlash) {
    
                  AlertDialog alert = new AlertDialog.Builder(MainActivity.this).create();
                  alert.setTitle("Error");
                  alert.setMessage("Sorry, your device doesn't support flash light!");
                  alert.setButton("OK", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          finish();
                      }
                  });
                  alert.show();
                  return;
              }
    
              getCamera();
    
              button.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
    
                      if (isFlashOn) {
                          turnOffFlash();
                          button.setText("ON");
                      } else {
                          turnOnFlash();
                          button.setText("OFF");
                      }
    
                  }
              });
          }
    
          private void getCamera() {
    
              if (camera == null) {
                  try {
                      camera = Camera.open();
                      params = camera.getParameters();
                  }catch (Exception e) {
    
                  }
              }
    
          }
    
          private void turnOnFlash() {
    
              if(!isFlashOn) {
                  if(camera == null || params == null) {
                      return;
                  }
    
                  params = camera.getParameters();
                  params.setFlashMode(Parameters.FLASH_MODE_TORCH);
                  camera.setParameters(params);
                  camera.startPreview();
                  isFlashOn = true;
              }
    
          }
    
          private void turnOffFlash() {
    
                  if (isFlashOn) {
                      if (camera == null || params == null) {
                          return;
                      }
    
                      params = camera.getParameters();
                      params.setFlashMode(Parameters.FLASH_MODE_OFF);
                      camera.setParameters(params);
                      camera.stopPreview();
                      isFlashOn = false;
                  }
          }
    
          @Override
          protected void onDestroy() {
              super.onDestroy();
          }
    
          @Override
          protected void onPause() {
              super.onPause();
    
              // on pause turn off the flash
              turnOffFlash();
          }
    
          @Override
          protected void onRestart() {
              super.onRestart();
          }
    
          @Override
          protected void onResume() {
              super.onResume();
    
              // on resume turn on the flash
              if(hasFlash)
                  turnOnFlash();
          }
    
          @Override
          protected void onStart() {
              super.onStart();
    
              // on starting the app get the camera params
              getCamera();
          }
    
          @Override
          protected void onStop() {
              super.onStop();
    
              // on stop release the camera
              if (camera != null) {
                  camera.release();
                  camera = null;
              }
          }
    
      }
    

提交回复
热议问题