Pages

Saturday, September 28, 2013

Android Flashlight

You must add the permission  in AndroidManifest.xml:

1
2
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button 
        android:id="@+id/on_btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Turn Flash ON" />
</LinearLayout>



MAinactivity

import javax.security.auth.PrivateCredentialPermission;

import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
private Button mOnBtn;
    private Button mOffBtn;
    private Camera mCamera;
    private boolean flash_ok=false;
   private boolean checkoff=true;
   
    
    
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flash_ok=getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
        mOnBtn = (Button) findViewById(R.id.on_btn);
        
        mOnBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(mCamera==null){
mCamera=Camera.open();
}
 
if (checkoff)
{
if(flash_ok)
{
processOnClick();
 checkoff=false;
 
 
}else{
Toast.makeText(getApplicationContext(), "No Flash Light", Toast.LENGTH_SHORT).show();
}
}
else
{
processOffClick();
checkoff=true;
}
}
});
       

    }
private void processOffClick(){
        if( mCamera != null ){
            Parameters params = mCamera.getParameters();
            params.setFlashMode( Parameters.FLASH_MODE_OFF );
            mCamera.setParameters( params );
            mCamera.release();
            mCamera = null;
            mOnBtn.setText("Turn Flash ON");
        }
    }

private void processOnClick(){
        if( mCamera != null ){
            Parameters params = mCamera.getParameters();
            params.setFlashMode( Parameters.FLASH_MODE_TORCH );
            mCamera.setParameters( params );
            mOnBtn.setText("Turn Flash OFF");
        }
    }
@Override
protected void onResume() {
if(mCamera==null)
mCamera=Camera.open();
super.onResume();
}
@Override
protected void onDestroy() {
if(mCamera!=null){
mCamera.release();
}
super.onDestroy();
}
// @Override
// public void onBackPressed() {
// this.finish();
// super.onBackPressed();
// }
@Override
protected void onPause() {
super.onPause();
         
       // on pause turn off the flash
//        processOffClick();
}

}




No comments:

Post a Comment