Android Realizes Simple Flashlight Function

  • 2021-11-13 02:45:09
  • OfStack

In this paper, we share the specific code of Android to realize simple flashlight function for your reference. The specific contents are as follows

XML:


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

 <ImageButton
  android:id="@+id/imageButton"
  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"  Horizontal center -->

 <SurfaceView
  android:id="@+id/surfaceView"
  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 com.example.flashlightactivite;

import android.Manifest;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;

public class Switch_Activity extends AppCompatActivity {

 ImageButton flashLightSwitch;
 boolean flag = false;
 SurfaceView surfaceView;
 Camera camera;
 Camera.Parameters camera_parameters; // Camera parameters 

//  Set the camera resolution and the resolution supported by the device 
// Camera.Size camera_size;

 SurfaceHolder surfaceHolder;
// SurfaceHolder Yes 1 Interface, which acts like 1 About Surface The listener. Provide access and control SurfaceView Behind Surface  Related methods 

// ***** 1 It must be in  AndroidManifest.xml  Add permissions to: <uses-permission android:name="android.permission.CAMERA"></uses-permission>
// <uses-permission android:name="android.permission.FLASHLIGHT"/>

//  Authority  PERMISSION_STORAGE: Allow storage 
 private static String [] PERMISSION_STORAGE = {
  Manifest.permission.CAMERA
 };

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

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

//   Determine version number and   Lollipop version number   The rank of 
  if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){
//   checkSelfPermission:  Self-check authority 
   if (ActivityCompat.checkSelfPermission( this,Manifest.permission.CAMERA ) != PackageManager.PERMISSION_GRANTED){
    ActivityCompat.requestPermissions( this,PERMISSION_STORAGE,REQUEST_PERMISSION_CODE );
   }
  }

  flashLightSwitch = findViewById( R.id.imageButton );
  surfaceView = findViewById( R.id.surfaceView );
  camera = Camera.open();
  camera_parameters = camera.getParameters();
  surfaceHolder = surfaceView.getHolder();

  surfaceHolder.addCallback( new SurfaceHolder.Callback() {
   @Override
   public void surfaceCreated(SurfaceHolder holder) {
    try{
     camera.setPreviewDisplay( surfaceHolder );
    }catch(Exception e){
//       Release  camera  Object 
     camera.release();
    }
    camera.startPreview();
   }

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

   }

   @Override
   public void surfaceDestroyed(SurfaceHolder holder) {

   }
  } );

//  ImageButton  Add a click event 
  flashLightSwitch.setOnClickListener( new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    if ( !flag ) {
//      Change  flag  Value of 
     flag = true;
//      Change Picture  R. Bag . Figure 
     flashLightSwitch.setImageResource( R.drawable.on );

//      Settings  flash  Type of   Flash mode: torch 
     camera_parameters.setFlashMode( Camera.Parameters.FLASH_MODE_TORCH );
     camera.setParameters( camera_parameters );
    }else {
     flashLightSwitch.setImageResource( R.drawable.off );
     flag = false;
//      Shut down  flash
     camera_parameters.setFlashMode( Camera.Parameters.FLASH_MODE_OFF );
     camera.setParameters( camera_parameters );
    }
   }
  } );

 }
}


/*
* FLASH_MODE_RED_EYE Anti-red eye mode, which reduces or prevents the red eye of people on pictures. 
  FLASH_MODE_TORCH Fill mode will reduce the flash intensity under normal illumination. 
  FLASH_MODE_AUTO Automatic mode, it will flash automatically when necessary. 
  FLASH_MODE_OFF  Flash mode will not be turned off 
  FLASH_MODE_ON  Flash mode will always be turned off during snapshot 
* */

AndroidManifest. xml:


<!--  Add Permissions  -->
 <uses-permission android:name="android.permission.CAMERA"/>
 <uses-permission android:name="android.permission.FLASHLIGHT"/>
 <uses-feature android:name="android.hardware.Camera"/>

Related articles: