package com.morpho.camera_led_demo;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
/**
* Created by g580872 on 08/06/2016.
*/
public class CameraLedActivity extends Activity {
private int valueFlash1;
private int valueFlash2;
private Camera camera;
private Parameters parameters;
/** To enable/disable power of LED1 , must use "led1". */
private static final String led1 = "led1";
/** To enable/disable power of LED2 , must use "led2". */
private static final String led2 = "led2";
/** Declaring SeekBar to cast / Initialize from XML UI. */
private SeekBar levelLed1 = null;
private TextView textLed1 = null;
/** Declaring SeekBar to cast / Initialize from XML UI. */
private SeekBar levelLed2 = null;
private TextView textLed2 = null;
private Method MTAB_SetFlashValues = null;
private Method MTAB_SetFlashTorch = null;
int progressChanged = 0;
int progressChanged2 = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createMorphoTabletReflectionMethods();
createMorphoTabletReflectionMethodsSetFlashTorch();
setContentView(R.layout.activity_camera_led);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setBackgroundDrawable(
getResources().getDrawable(R.drawable.actionbar_bg_gradient));
textLed1 = (TextView) findViewById(R.id.flashText1);
levelLed1 = (SeekBar) findViewById(R.id.seekBar1);
textLed2 = (TextView) findViewById(R.id.flashText2);
levelLed2 = (SeekBar) findViewById(R.id.seekBar2);
if (savedInstanceState != null) {
valueFlash1 = savedInstanceState.getInt("levelLed2");
valueFlash2 = savedInstanceState.getInt("levelLed1");
}
initLed();
levelLed1.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
progressChanged = progress;
textLed1.setText("Value of Led1: " + progress);
setLedPower(led1, progressChanged);
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
levelLed2.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
progressChanged2 = progress;
textLed2.setText("Value of Led2: " + progress);
setLedPower(led2, progressChanged2);
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
@SuppressWarnings("rawtypes")
private void createMorphoTabletReflectionMethods() {
if (camera == null) {
camera = Camera.open();
}
Class<? extends Camera.Parameters> c = camera.getParameters()
.getClass();
Class[] cArg1 = { int.class, int.class };
try {
MTAB_SetFlashValues = c.getMethod("setFlashValue", cArg1);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
private void createMorphoTabletReflectionMethodsSetFlashTorch() {
if (camera == null) {
camera = Camera.open();
}
Class<? extends Camera.Parameters> c = camera.getParameters()
.getClass();
Class[] cArg1 = { boolean.class};
try {
MTAB_SetFlashTorch = c.getMethod("setFlashTorch", cArg1);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
public void initLed() {
if (camera == null) {
camera = Camera.open();
}
parameters = camera.getParameters();
try {
MTAB_SetFlashTorch.invoke(parameters,false);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
// parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
//camera.setParameters(parameters);
try {
MTAB_SetFlashValues.invoke(parameters,valueFlash1, valueFlash2);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
try {
MTAB_SetFlashTorch.invoke(parameters,true);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
camera.setParameters(parameters);
}
public void setLedPower(String device, int value) {
parameters = camera.getParameters();
//parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
//camera.setParameters(parameters);
if (device.equals(led1)) {
valueFlash2 = value;
} else {
valueFlash1 = value;
}
try {
MTAB_SetFlashValues.invoke(parameters,valueFlash1, valueFlash2);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
MTAB_SetFlashTorch.invoke(parameters,true);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
camera.setParameters(parameters);
}
@Override
public boolean onOptionsItemSelected(final MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: {
finish();
}
break;
default:
break;
}
return true;
}
@Override
protected void onResume() {
super.onResume();
if (camera == null) {
camera = Camera.open();
}
}
@Override
protected void onPause() {
if (camera != null) {
camera.stopPreview();
camera.release();
camera = null;
}
super.onPause();
}
protected void onSaveInstanceState(Bundle saveInstanceState) {
super.onSaveInstanceState(saveInstanceState);
saveInstanceState.putInt("levelLed1", levelLed1.getProgress());
saveInstanceState.putInt("levelLed2", levelLed2.getProgress());
}
public void onRestoreInstanceState(Bundle savedInstanceState) {
levelLed1.setProgress(savedInstanceState.getInt("levelLed1"));
levelLed2.setProgress(savedInstanceState.getInt("levelLed2"));
}
}