SoFunction
Updated on 2025-04-10

Android implements simple flashlight function

This article shares the specific code for Android to implement the simple flashlight function for your reference. The specific content is as follows

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:andro
 xmlns:app="/apk/res-auto"
 xmlns:tools="/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".Switch_Activity">

 <ImageButton
  android:
  android:layout_width="225dp"
  android:layout_height="wrap_content"
  android:layout_alignParentTop="true"
  android:layout_centerHorizontal="true"
  android:layout_marginTop="267dp"
  app:srcCompat="@drawable/off" />

 <!-- android:layout_centerHorizontal="true" Center horizontally-->

 <SurfaceView
  android:
  android:layout_width="378dp"
  android:layout_height="221dp"
  android:layout_alignParentStart="true"
  android:layout_alignParentLeft="true"
  android:layout_alignParentTop="true"
  android:layout_marginStart="23dp"
  android:layout_marginLeft="23dp"
  android:layout_marginTop="29dp" />
 
</RelativeLayout>

Java:

package ;

import ;
import ;
import ;
import ;
import .;
import .;
import ;
import ;
import ;
import ;
import ;

public class Switch_Activity extends AppCompatActivity {

 ImageButton flashLightSwitch;
 boolean flag = false;
 SurfaceView surfaceView;
 Camera camera;
  camera_parameters; //Camera parameters
// Set the camera resolution and the resolution supported by the device//  camera_size;

 SurfaceHolder surfaceHolder;
// SurfaceHolder is an interface that acts like a listener about Surface.  Provides methods related to accessing and controlling the Surface behind the SurfaceView
// ***** Be sure to add permissions in: <uses-permission android:name=""></uses-permission>// &lt;uses-permission android:name=""/&gt;

// Permissions PERMISSION_STORAGE: Allow storage private static String [] PERMISSION_STORAGE = {
  
 };

// Request dynamic code REQUEST_PERMISSION_CODE: Request permission code private static int REQUEST_PERMISSION_CODE = 1;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  ( savedInstanceState );
  setContentView( .activity_switch_ );

// Determine the version number and the lollipop version number  if (.SDK_INT &gt; Build.VERSION_CODES.LOLLIPOP){
// checkSelfPermission: Self-check permission   if (( this, ) != PackageManager.PERMISSION_GRANTED){
    ( this,PERMISSION_STORAGE,REQUEST_PERMISSION_CODE );
   }
  }

  flashLightSwitch = findViewById(  );
  surfaceView = findViewById(  );
  camera = ();
  camera_parameters = ();
  surfaceHolder = ();

  ( new () {
   @Override
   public void surfaceCreated(SurfaceHolder holder) {
    try{
     ( surfaceHolder );
    }catch(Exception e){
// Release camera object     ();
    }
    ();
   }

   @Override
   public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

   }

   @Override
   public void surfaceDestroyed(SurfaceHolder holder) {

   }
  } );

// ImageButton Add click event  ( new () {
   @Override
   public void onClick(View v) {
    if ( !flag ) {
// Change the value of flag     flag = true;
// Change the picture R. package.     (  );

// Set the type of flash Flash mode: Torch     camera_parameters.setFlashMode( .FLASH_MODE_TORCH );
     ( camera_parameters );
    }else {
     (  );
     flag = false;
// Close flash     camera_parameters.setFlashMode( .FLASH_MODE_OFF );
     ( camera_parameters );
    }
   }
  } );

 }
}


/*
 * FLASH_MODE_RED_EYE anti-red eye mode reduces or prevents the red eye of the characters in the picture from appearing.
   FLASH_MODE_TORCH fill mode, which will reduce the flash intensity under normal light.
   FLASH_MODE_AUTO automatic mode, it will automatically flash when needed.
   FLASH_MODE_OFF Flash mode will not be turned off
   FLASH_MODE_ON Flash mode will always be turned off during snapshot
 * */

&lt;!-- Add permissions --&gt;
 &lt;uses-permission android:name=""/&gt;
 &lt;uses-permission android:name=""/&gt;
 &lt;uses-feature android:name=""/&gt;

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.