Sunday, December 1, 2013

Flash light control in Android

I am not really satisfied with this. I am just turning it on and off, but wanted to be able to adjust intensity. See some links all the way to the bottom that may be able to help on that. Got to do more research. My email to HTC was completely unfruitful... :(

Also, sorry I didn't clean it up much. It is pretty straightforward. Only other thing besides learning to turn on the LED is how we throw an alert to the user (see below for the case the phone does not have a camera flash light). But anyhow, as I got the code, here it goes (Led.java): 
 package cell0907.example.led;  
 import android.hardware.Camera;  
 import android.hardware.Camera.Parameters;  
 import android.os.Bundle;  
 import android.app.Activity;  
 import android.app.AlertDialog;  
 import android.content.DialogInterface;  
 import android.content.pm.PackageManager;  
 import android.util.Log;  
 import android.view.Menu;  
 import android.view.View;  
 import android.widget.Button;  
 import android.widget.Toast;  
 public class Led extends Activity {  
      public static Camera cam = null;// has to be static, otherwise onDestroy() destroys it  
      private Button light_switch;  
   private boolean isFlashOn;  
   private boolean hasFlash;  
   Parameters p;  
   static final String TAG="MyActivity";  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_led);  
           light_switch=(Button)this.findViewById(R.id.button1);  
           /*  
            * First check if device is supporting flashlight or not  
            */  
           hasFlash = getApplicationContext().getPackageManager()  
               .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);  
           if (!hasFlash) {  
             // device doesn't support flash  
             // Show alert message and close the application  
             AlertDialog alert = new AlertDialog.Builder(Led.this)  
                 .create();  
             alert.setTitle("Error");  
             alert.setMessage("Sorry, your device doesn't support flash light!");  
             alert.setButton(RESULT_OK, ALARM_SERVICE, new DialogInterface.OnClickListener() {  
               public void onClick(DialogInterface dialog, int which) {  
                 // closing the application  
                 finish();  
               }  
             });  
             alert.show();  
             return;  
           }  
           light_switch.setOnClickListener(new View.OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                     // TODO Auto-generated method stub  
               if (isFlashOn) {  
                 // turn off flash  
                    flashLightOff();  
                    light_switch.setText("Turn on");  
               } else {  
                 // turn on flash  
                    light_switch.setText("Turn off");  
                    flashLightOn();  
               }  
                }  
           });  
      }  
      public void flashLightOn() {  
           Log.v(TAG, "ON");  
        try {  
             getcamera();  
       p.setFlashMode(Parameters.FLASH_MODE_TORCH);  
       cam.setParameters(p);  
       cam.startPreview();  
       isFlashOn=true;  
        } catch (Exception e) {  
          e.printStackTrace();  
          Toast.makeText(getBaseContext(), "Exception flashLightOn()",  
              Toast.LENGTH_SHORT).show();  
        }  
      }  
      public void getcamera(){  
           if (cam==null){  
                try{  
                     Log.v(TAG, "CAMERA CONFIG");  
               cam = Camera.open();  
               p = cam.getParameters();  
                }  
                catch (RuntimeException e) {  
         Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());  
                }  
      }  
      }  
      public void flashLightOff() {  
           Log.v(TAG, "OFF");  
        if (cam!=null) try {  
            cam.stopPreview();  
            cam.release();  
            cam = null;  
               isFlashOn=false;  
        } catch (Exception e) {  
          e.printStackTrace();  
          Toast.makeText(getBaseContext(), "Exception flashLightOff",  
              Toast.LENGTH_SHORT).show();  
        }  
      }       
      @Override  
      public boolean onCreateOptionsMenu(Menu menu) {  
           // Inflate the menu; this adds items to the action bar if it is present.  
           getMenuInflater().inflate(R.menu.led, menu);  
           return false;  
      }  
      @Override  
      protected void onStart() {  
        super.onStart();  
      }  
      @Override  
      protected void onRestart() {  
        super.onRestart();  
      }  
      @Override  
      protected void onResume() {  
        super.onResume();  
      }  
      @Override  
      protected void onPause() {  
        super.onPause();  
        // on pause turn off the flash  
        flashLightOff();  
      }  
      @Override  
      protected void onStop() {  
        super.onStop();  
      }  
      @Override  
      protected void onDestroy() {  
        super.onDestroy();  
      }  
 }  


The other thing to remember is to add the following lines to the Manifest so that the app got the right permissions:
<uses-permission android:name="android.permission.CAMERA"/> 
<uses-permission android:name="android.permission.FLASHLIGHT"/>


Others:
  1. A hint on the xda-developers site. In my case I had a brightness file with 0 inside and a max_brightness file with 255 inside. I tried making the second even a zero, but my flashlight app just kept working... 
  2. This other link points to something similar and explains a way to test it quickly. Got to try.
  3. A much more elaborated flashlight example 
  4. A completely different method without using the SDK It may be worth to explore if that can do the intensity.
  5. Full source code of another flashlight app
  6. Answered question in Stackoverflow 
  7. Question without answer in Stackoverflow
  8. Please, click here to see an index of other posts on Android. 

1 comment:

  1. recommended flash alerts full version free

    https://play.google.com/store/apps/details?id=com.apperway.flashalerts

    ReplyDelete