Monday, 9 June 2014

how to create simple torch light in android

MainActivity.java

package com.simpleflashlight;

import java.io.IOException;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
import android.widget.ToggleButton;

public class MainActivity extends Activity implements SurfaceHolder.Callback {

    public final static String TAG = "simple torch";
    Camera cam;
    ToggleButton mTorch;
    Parameters camParams;
    private Context context;
    AlertDialog.Builder builder;
    AlertDialog alertDialog;
    private SurfaceView surfaceView;
    private SurfaceHolder surfaceHolder;
    private final int FLASH_NOT_SUPPORTED = 0;
    private final int FLASH_TORCH_NOT_SUPPORTED = 1;
   
      @Override
    public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        context = MainActivity.this;
        if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){
            mTorch = (ToggleButton) findViewById(R.id.toggleButton);
            mTorch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    Log.d(TAG, "onCheckedChanged");
                    try{
                        if (cam == null){
                            cam = Camera.open();
                        }
                        camParams = cam.getParameters();
                        List<String> flashModes = camParams.getSupportedFlashModes();
                        if (isChecked){
                            if (flashModes.contains(Parameters.FLASH_MODE_TORCH)) {
                                camParams.setFlashMode(Parameters.FLASH_MODE_TORCH);
                            }else{
                                showDialog(MainActivity.this, FLASH_TORCH_NOT_SUPPORTED);
                            }
                        } else {
                            camParams.setFlashMode(Parameters.FLASH_MODE_OFF);
                        }
                        cam.setParameters(camParams);
                        cam.startPreview();
                    }catch (Exception e) {
                        Log.d(TAG, "Caught " + e);
                        Toast.makeText(MainActivity.this, "Camera/Torch failure: " + e, Toast.LENGTH_SHORT).show();
                        e.printStackTrace();
                        if (cam != null) {
                            cam.stopPreview();
                            cam.release();
                        }
                    }
                }
            });
            surfaceView = (SurfaceView) this.findViewById(R.id.SurfaceView);
            surfaceHolder = surfaceView.getHolder();
            surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
            surfaceHolder.addCallback(this);
        } else {
            showDialog(MainActivity.this, FLASH_NOT_SUPPORTED);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(cam == null){
            cam = Camera.open();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        cam.release();
    }

    @Override
    protected void onPause() {
        super.onPause();
        if(cam != null){
            cam.release();
        }
    }

    public void showDialog (Context context, int dialogId) {
        switch(dialogId){
        case FLASH_NOT_SUPPORTED:
            builder = new AlertDialog.Builder(context);
            builder.setMessage("Sorry, Your phone does not support Camera Flash")
            .setCancelable(false)
            .setNeutralButton("Close", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
            alertDialog = builder.create();
            alertDialog.show();
            break;
        case FLASH_TORCH_NOT_SUPPORTED:
            builder = new AlertDialog.Builder(context);
            builder.setMessage("Sorry, Your camera flash does not support torch feature")
            .setCancelable(false)
            .setNeutralButton("Close", new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();
                }
            });
            alertDialog = builder.create();
            alertDialog.show();
        }

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
     
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {

            cam.setPreviewDisplay(holder);

        } catch (IOException e) {
         

        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
     
    }   
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="20dip"
        android:text="@string/app_title"
        android:textSize="25sp" />

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dip"
        android:text="@string/toggleButtonLabe" />

    <SurfaceView
        android:id="@+id/SurfaceView"
        android:layout_width="1dp"
        android:layout_height="1dp" />

</LinearLayout>

AndroidManifest.xml

 <uses-feature
        android:name="android.hardware.Camera"
        android:required="true" >
    </uses-feature>
    <uses-feature
        android:name="android.hardware.camera.FLASHLIGHT"
        android:required="true" >
    </uses-feature>

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

No comments:

Post a Comment