Tuesday, 16 December 2014

Program - 3) My Android Program for Face Detection and 83 Essential Points Plotting on Face
with Faceplusplus 

Using Faceplusplus Which is Face Detection Technology Used in many Applications to Detect ?Human Face for Authentication Purpose.

My Android Program for Face Detect + 83 Points :

Watch Video Here https://www.youtube.com/watch?v=ZvhbQ2SwJPU





Code to above Functionality is very Big If any one is Interested. They can mail me.
Thank you




Program- 2) Swipe Listeners With Animations

Video is here     http://youtu.be/zEM2S5VHESg

Using this Code you can Implement Left,Right,Top,Bottom SwipeListeners.
I implemented just Right Swipe Listener

Before we start the Building Swipe Listeners it is necessary to create animation code.
Create anim Folder in res folder of your Android Project and create the following xml files.


fade_back.xml: -

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale android:duration="300"
           android:pivotX="50.0%"
           android:pivotY="50.0%"
           android:fromXScale="1.0"
           android:toXScale="0.85"
           android:fromYScale="1.0"
           android:toYScale="0.85"/>
    <alpha android:duration="300"
           android:fromAlpha="1.0"
           android:toAlpha="0.1"/>
</set>

fade_in.xml :-

<?xml version="1.0" encoding="utf-8"?>

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale android:duration="300"
           android:pivotX="50.0%"
           android:pivotY="50.0%"
           android:fromXScale="0.85"
           android:toXScale="1.0"
           android:fromYScale="0.85"
           android:toYScale="1.0"/>
    <alpha android:duration="300"
           android:fromAlpha="0.1"
           android:toAlpha="1.0"/>
</set>


slide_in_left.xml :-

<?xml version="1.0" encoding="utf-8"?>

<translate xmlns:android="http://schemas.android.com/apk/res/android"
           android:duration="350"
           android:fromXDelta="100.0%p"
           android:toXDelta="0.0"/>


Slide_out_right:-

<?xml version="1.0" encoding="utf-8"?>

<translate xmlns:android="http://schemas.android.com/apk/res/android"
           android:duration="300"
           android:fromXDelta="0.0"
           android:toXDelta="100.0%p"/>


///////////////////////  Animations code part is Finished ////////////////////////////

MainActivity:-

public class MainActivity extends Activity {

Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.click);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent();
i.setClass(MainActivity.this, NewPage.class);
startActivity(i);
overridePendingTransition(R.anim.slide_in_left, R.anim.fade_back);
}
});
}
}


NewPage :- (This is another Activity)

public class NewPage extends Activity implements OnTouchListener{

RelativeLayout rl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_page);
rl = (RelativeLayout)findViewById(R.id.swipeRL);
rl.setOnTouchListener(new OnSwipeTouchListener(this){
public void onSwipeRight(){
//overridePendingTransition( R.anim.slide_out_right, R.anim.fade_in );
Intent back = new Intent();
back.setClass(NewPage.this, MainActivity.class);
startActivity(back);
overridePendingTransition( R.anim.slide_out_right, R.anim.fade_in );
}
});
}

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
}


OnSwipeTouchListener :- (This Class enables Swipe Listener effect).


public  class OnSwipeTouchListener implements OnTouchListener {

    protected final GestureDetector gestureDetector;

    public OnSwipeTouchListener (Context ctx){
        gestureDetector = new GestureDetector(ctx, new GestureListener());
    }

    private final class GestureListener extends SimpleOnGestureListener {

        private static final int SWIPE_THRESHOLD = 100;
        private static final int SWIPE_VELOCITY_THRESHOLD = 100;

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            boolean result = false;
            try {
                float diffY = e2.getY() - e1.getY();
                float diffX = e2.getX() - e1.getX();
                if (Math.abs(diffX) > Math.abs(diffY)) {
                    if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffX > 0) {
                            onSwipeRight();
                        } else {
                            onSwipeLeft();
                        }
                    }
                } else {
                    if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
                        if (diffY > 0) {
                            onSwipeBottom();
                        } else {
                            onSwipeTop();
                        }
                    }
                }
            } catch (Exception exception) {
                exception.printStackTrace();
            }
            return result;
        }
    }
    
    public void onSwipeRight() {
    }

    public void onSwipeLeft() {
    }

    public void onSwipeTop() {
    }

    public void onSwipeBottom() {
    }

@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return gestureDetector.onTouchEvent(arg1);
}
}






Program- 1 )Displaying All Images present in Sdcard In GridView.

Screenshot:
Video Link is here       http://youtu.be/1JHy7kw3omk 




All Images Form My GenyMotion EMULATOR are Displayed in GridView


activity_main.xml:-


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical">

    <GridView
        android:id="@+id/gridview"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:columnWidth="90dp"
        android:numColumns="1"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
       android:gravity="center"/>

</LinearLayout>


Add This Permission to Manifest File

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


MainActivity :-


import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

    List<String> your_array_list = new ArrayList<String>();
    ArrayAdapter<String> arrayAdapter;
public class ImageAdapter extends BaseAdapter {
    
    private Context mContext;
    ArrayList<String> itemList = new ArrayList<String>();
    
    public ImageAdapter(Context c) {
     mContext = c; 
    }
    
    void add(String path){
     itemList.add(path); 
    }

 @Override
 public int getCount() {
  return itemList.size();
 }

 @Override
 public Object getItem(int arg0) {
   
  return null;
 }

 @Override
 public long getItemId(int position) {
  
  return 0;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(120, 120));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position), 120, 120);

        imageView.setImageBitmap(bm);
        return imageView;
 }
 
 public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {
  
  Bitmap bm = null;
   
  final BitmapFactory.Options options = new BitmapFactory.Options();
  options.inJustDecodeBounds = true;
  BitmapFactory.decodeFile(path, options);
      
  
  options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
      
  
  options.inJustDecodeBounds = false;
  bm = BitmapFactory.decodeFile(path, options); 
      
  return bm;   
 }
 
 public int calculateInSampleSize(
   
  BitmapFactory.Options options, int reqWidth, int reqHeight) {
 
  final int height = options.outHeight;
  final int width = options.outWidth;
  int inSampleSize = 1;
  
  if (height > reqHeight || width > reqWidth) {
   if (width > height) {
    inSampleSize = Math.round((float)height / (float)reqHeight);    
   } else {
    inSampleSize = Math.round((float)width / (float)reqWidth);    
   }   
  }
  
  return inSampleSize;    
 }
}

   ImageAdapter myImageAdapter;

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


       GridView gridview = (GridView) findViewById(R.id.gridview);
       myImageAdapter = new ImageAdapter(this);
       gridview.setAdapter(myImageAdapter);
       
        SongsManager sm=new SongsManager();
       ArrayList<String> images=sm.getPlayList();
        
       
       for (String image:images){
        myImageAdapter.add(image);
       }
        
}
}


SongsManager:-

/* SongsManager Class will Retrieve Images not Songs*/
/*//adding JPEG and PNG format Images */

import java.io.File;
import java.util.ArrayList;

import android.os.Environment;

public class SongsManager {
// SDCard Path
final String MEDIA_PATH = Environment.getExternalStorageDirectory()
       .getPath() + "/";
private ArrayList<String> songsList= new ArrayList<String>();
private String mp3Pattern = ".jpg";
private String pngPattern = ".png";

// Constructor
public SongsManager() {

}

/**
* Function to read all mp3 files and store the details in
* ArrayList
* */
public ArrayList<String> getPlayList() {
   if (MEDIA_PATH != null) {
       File home = new File(MEDIA_PATH);
       File[] listFiles = home.listFiles();
       if (listFiles != null && listFiles.length > 0) {
           for (File file : listFiles) {
               System.out.println(file.getAbsolutePath());
               if (file.isDirectory()) {
                   scanDirectory(file);
               } else {
                   addSongToList(file);
               }
           }
       }
   }
   // return songs list array
   return songsList;
}

private void scanDirectory(File directory) {
   if (directory != null) {
       File[] listFiles = directory.listFiles();
       if (listFiles != null && listFiles.length > 0) {
           for (File file : listFiles) {
               if (file.isDirectory()) {
                   scanDirectory(file);
               } else {
                   addSongToList(file);
               }

           }
       }
   }
}

private void addSongToList(File song) {

//adding JPEG and PNG format Images
   if (song.getName().endsWith(mp3Pattern) || song.getName().endsWith(pngPattern)) {
       songsList.add(song.getPath());
   }
}
}