Sunday, 14 September 2014

Flash Light Android App tutorial

Hi,
 This tutorial will explains you to implement Android Flash Light.

Following things are required to implement,

1. The device must have flash light facility.
2. Need to get CAMERA permission in AndroidMainifest.xml
3. Little bit knowledge in Android Development

Let us go with the following code example...................................

Permissions
<uses-permission android:name="android.permission.CAMERA" />

   <uses-feature android:name="android.hardware.camera" />


public class MainActivity extends Activity {

// flag to detect flash is on or off
private boolean isLighOn = false;
private Camera camera;
private TextView batteryTxt;
private ImageView cameraBtn;
Parameters params;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);

batteryTxt = (TextView) this.findViewById(R.id.textView1);
this.registerReceiver(this.mBatInfoReceiver, new           IntentFilter(Intent.ACTION_BATTERY_CHANGED));
cameraBtn = (ImageView) findViewById(R.id.buttonFlashlightappa);
Context context = this;
PackageManager pm = context.getPackageManager();

// if device support camera?
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
return;
}
try {
camera = Camera.open();
params = camera.getParameters();
} catch (Exception e) {
System.out.println(e);
}
                // For flashlight button animation
final Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in);
final Animation animation2 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_out);

cameraBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (isLighOn) {
try {
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isLighOn = false;
cameraBtn.startAnimation(animation2);
cameraBtn.setImageDrawable(getResources().getDrawable(R.drawable.green));
cameraBtn.startAnimation(animation);
} catch (Exception e) {
System.out.println(e);
}

} else {
try {
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isLighOn = true;
cameraBtn.startAnimation(animation2);
cameraBtn.setImageDrawable(getResources().getDrawable(R.drawable.red));
cameraBtn.startAnimation(animation);
} catch (Exception e) {
System.out.println(e);
}
}
}
});
}

// get the level of battery in percentage
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context ctxt, Intent intent) {
try {
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
batteryTxt.setText("Battery Level-" + String.valueOf(level)+ "%");
} catch (Exception e) {
System.out.println(e);
}
}
};
@Override
protected void onStop() {
super.onStop();
try {
if (camera != null) {
camera.release();
}
} catch (Exception e) {
System.out.println(e);
}
}

@Override
protected void onPause() {
super.onPause();
}
 
        // Detect light is on, if it is on, stop it when back button is pressed
public void onBackPressed() {
if (isLighOn) {
try {
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isLighOn = false;
} catch (Exception e) {
System.out.println(e);
}

}
finish();
}
}

No comments:

Post a Comment