Today i'm going to show you how to develop flashlight app using android. In this i'm using drawable resource called torch. you can use any image for that. copy the image to drawable folder in res directory.
Then open the activity xml layout and add this button to show button for on and off the flash light.
<ImageButton
android:layout_width="290dp"
android:layout_height="300dp"
android:id="@+id/flash_light"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:src="@drawable/torch"
android:background="@null"
/>
This is the main class which i use to turn on the flashlight and turn of the flashlight. When the turn on and off happen it will change the background of the button. This is not mandatory. It will give better feedback to the user whether the flash is on or off.
package com.flashlight.ishanfx.flashlight; import android.content.pm.PackageManager; import android.hardware.Camera; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.ImageButton; import android.widget.RelativeLayout; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private ImageButton btnSwitch; private Camera camera; private Camera.Parameters parameter; private boolean isFlash; private boolean isFlashLightOn = false; RelativeLayout relativeLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); relativeLayout = (RelativeLayout) findViewById(R.id.main); btnSwitch = (ImageButton) findViewById(R.id.flash_light); isFlash = getApplication().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH); if(!isFlash){ Toast.makeText(MainActivity.this, "Camera Device Not Found", Toast.LENGTH_SHORT).show(); return; } else{ this.camera = Camera.open(0); parameter = this.camera.getParameters(); } btnSwitch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(!isFlashLightOn){ onFlashLight(); }else{ offFlashLight(); } } }); } // getting camera parameters private void offFlashLight() { parameter.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); this.camera.setParameters(parameter); this.camera.stopPreview(); isFlashLightOn = false; relativeLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark)); //flashLight.setImageResource(R.drawable.buttonoff); } private void onFlashLight() { if(this.camera != null){ parameter = this.camera.getParameters(); parameter.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); this.camera.setParameters(parameter); this.camera.startPreview(); isFlashLightOn = true; relativeLayout.setBackgroundColor(getResources().getColor(R.color.colorAccent)); } } /* * Turning On flash */ }
Add these 2 permissions to the AndroidManifest file
<uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" />
You can check the application from below link.
https://play.google.com/store/apps/details?id=com.flashlight.ishanfx.flashlight
No comments:
Post a Comment