Android Tutorials

Android Mobile Vision API Face Filter implementation

Since MSQRD and SnapChat has created lot of hype on the internet and they got humongous users(I am one of them 😉 ) who loves to use the face filter feature. I am also getting lot of request to have a example on my blog. So here I am presenting a working demo for face filter feature.

I spent few minutes on google and came to know that MSQRD is using Google’s Mobile Vision API.

Mobile Vision API is powerful library to find objects in photos and video, using real-time on-device vision technology.

I performed a POC using their official code from their examples here. And a final outcome will look like below image.


Achieving above output is really simple. Follow the below mentioned steps

Grab a good .png image which you would like to see as filter. And out it inside relevant drawable folder.

Set front facing camera. Go to, inside createCameraSource() method update below code.

mCameraSource = new CameraSource.Builder(context, detector)
        .setRequestedPreviewSize(640, 480)


There is a variable mOverlay inside Graphic class, which is inner class of GraphicOverlay class. Write getter method for the same.

public GraphicOverlay getOverlay() {
    return mOverlay;


Now take two member data variables for class

private Bitmap bitmap;
private Bitmap op;


We will decode the drawable as bitmap which we will use to render once the face is detected. Perform the decoding process inside the constructor for class.

bitmap = BitmapFactory.decodeResource(getOverlay().getContext().getResources(), R.drawable.op);
op = bitmap;


Within same class there is updateFace method. We will resize the bitmap so that rendered filter image will fit to the size of detected face.

void updateFace(Face face) {
    mFace = face;
    op = Bitmap.createScaledBitmap(bitmap, (int) scaleX(face.getWidth()),
            (int) scaleY(((bitmap.getHeight() * face.getWidth()) / bitmap.getWidth())), false);


We are almost set now simply copy the below onDraw() method and replace your ondraw method at

public void draw(Canvas canvas) {
    Face face = mFace;
    if (face == null) {

    // Draws a circle at the position of the detected face, with the face's track id below.
    float x = translateX(face.getPosition().x + face.getWidth() / 2);
    float y = translateY(face.getPosition().y + face.getHeight() / 2);
    float xOffset = scaleX(face.getWidth() / 2.0f);
    float yOffset = scaleY(face.getHeight() / 2.0f);
    float left = x - xOffset;
    float top = y - yOffset;
    float right = x + xOffset;
    float bottom = y + yOffset;
    canvas.drawRect(left, top, right, bottom, mBoxPaint);
    canvas.drawBitmap(op, left, top, new Paint());

private float getNoseAndMouthDistance(PointF nose, PointF mouth) {
    return (float) Math.hypot(mouth.x - nose.x, mouth.y - nose.y);


After this step we are done. Go create your own MSQRD app :).

Find the complete source code on github here.

Android Tutorials

Twitter App Only Authentication example

This example is to showcase how we can search in twitter android sdk WITHOUT LOGIN (Yes it is possible) . Twitter4j Offers this.


Twitter search example without login

Twitter search example without login


Twitter offers applications the ability to issue authenticated requests on behalf of the application itself (as opposed to on behalf of a specific user). Twitter’s implementation is based on the Client Credentials Grant flow of the OAuth 2 specification. Note that OAuth 1.0a is still required to issue requests on behalf of users.

With Application-only authentication you don’t have the context of an authenticated user and this means that any request to API for endpoints that require user context, such as posting tweets, will not work. However, the set of endpoints that will still be available can have a higher rate limit.

Your app will be able to, for example:

  • Pull user timelines;
  • Access friends and followers of any account;
  • Access lists resources;
  • Search in tweets;
  • Retrieve any user information;

And it won’t be able to:

  • Post tweets or other resources;
  • Connect in Streaming endpoints;
  • Search for users;
  • Use any geo endpoint;
  • Access DMs or account credentials;
Find the Source code of the example on github

Do In Background method of my async task


        protected Integer doInBackground(String... params) {
            try {
                ConfigurationBuilder builder = new ConfigurationBuilder();

                OAuth2Token token = new TwitterFactory(;

                builder = new ConfigurationBuilder();

                Twitter twitter = new TwitterFactory(;

                Query query = new Query(params[0]);
                // YOu can set the count of maximum records here
                QueryResult result;
                result =;
                List<twitter4j.Status> tweets = result.getTweets();
                StringBuilder str = new StringBuilder();
                if (tweets != null) {
                    this.tweets = new ArrayList<Tweet>();
                    for (twitter4j.Status tweet : tweets) {
                        str.append("@" + tweet.getUser().getScreenName() + " - " + tweet.getText() + "\n");
                        this.tweets.add(new Tweet("@" + tweet.getUser().getScreenName(), tweet.getText()));
                    return SUCCESS;
            } catch (Exception e) {

            return FAILURE;


If you observe the above code. We are creating our own configuration using ConfigureBuilder class. You only need your consumer key and consumer secret key

After that set the


Create your own OAuthToken which allows application to access rest API

than setApplicationOnlyAuthEnabled flag true

Set OAuthToken parameter and after that you are good to go.

Find the Source code of the example on github.
Android Tutorials

Securely store user credentials

We always store user’s credentials like User-name/Email Id or passwords, some times API public Keys also.

Some uses database(worst option), Shared Preferences (Better) .

We knows that this can be vulnerable, as security is always a problem with rooted devices. So we think of the other options.

Someone comes and says hey lets encrypt the data (Ok Good suggestion). I will say I can still decrypt the apk file.

Also can deobfuscate your code files and understand the the algorithm used for encryption and decryption (Simple :p) .

Let me explain.

First we will write two methods for encryption and decryption.

The functions are very straight forward. I have put them in wrapper class.

Which looks like below.


import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class AESHelper {
    public static String encrypt(String seed, String cleartext)
            throws Exception {
        byte[] rawKey = getRawKey(seed.getBytes());
        byte[] result = encrypt(rawKey, cleartext.getBytes());
        return toHex(result);

    public static String decrypt(String seed, String encrypted)
            throws Exception {
        byte[] rawKey = getRawKey(seed.getBytes());
        byte[] enc = toByte(encrypted);
        byte[] result = decrypt(rawKey, enc);
        return new String(result);

    private static byte[] getRawKey(byte[] seed) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
        kgen.init(128, sr); // 192 and 256 bits may not be available
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();
        return raw;

    private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(clear);
        return encrypted;

    private static byte[] decrypt(byte[] raw, byte[] encrypted)
            throws Exception {
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec);
        byte[] decrypted = cipher.doFinal(encrypted);
        return decrypted;

    public static String toHex(String txt) {
        return toHex(txt.getBytes());

    public static String fromHex(String hex) {
        return new String(toByte(hex));

    public static byte[] toByte(String hexString) {
        int len = hexString.length() / 2;
        byte[] result = new byte[len];
        for (int i = 0; i < len; i++)
            result[i] = Integer.valueOf(hexString.substring(2 * i, 2 * i + 2),
        return result;

    public static String toHex(byte[] buf) {
        if (buf == null)
            return "";
        StringBuffer result = new StringBuffer(2 * buf.length);
        for (int i = 0; i < buf.length; i++) {
            appendHex(result, buf[i]);
        return result.toString();

    private final static String HEX = "0123456789ABCDEF";

    private static void appendHex(StringBuffer sb, byte b) {
        sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));


And an Activity’s code may look like

public class EncryptDecrypt extends Activity {
    public static String seedValue = "I AM UNBREAKABLE";
    public static String MESSAGE = "No one can read this message without decrypting me.";

    protected void onCreate(Bundle savedInstanceState) {
        try {
            String encryptedData = AESHelper.encrypt(seedValue, MESSAGE);
            Log.v("EncryptDecrypt", "Encoded String " + encryptedData);
            String decryptedData = AESHelper.decrypt(seedValue, encryptedData);
            Log.v("EncryptDecrypt", "Decoded String " + decryptedData);

        } catch (Exception e) {

At this point of time, as I said it is very easy to get that public key

public static String seedValue = "I AM UNBREAKABLE";

In such circumstances I have better solution. I will use NDK the best available solution.

Using NDK and building your code creates .so file. To decompile a .so file is a herculean task.

Now Add following line of code in your Activity.

static {
public native String getSecretKey();

Your .C file will look like

Java_com_example_encryptdecrypt_EncryptDecrypt_getSecretKey( JNIEnv* env,
                                                  jobject thiz )
    return (*env)->NewStringUTF(env, "I AM UNBREAKABLE");

Now add below mentioned line in your Activity before starting your encryption process.

seedValue = getSecretKey();
And you are secure now.
Happy coding and Stay Secure. 🙂


Android Tutorials

Android Float Label Example

FloatingLabel Allows you to create a blow kind of EditText.

Floating Lable project is in initial mode, where a few customization is provided like changing Text color of EditText and Its Label


      label:floatHintText="Whatever hint you want to display" 

You can also provide custom fonts also, condition is to put them in assets folder.

Download the example and source code from github.

Android Tutorials

Modifying App Chooser Intent

Hi Friends,

I am seeing many questions on stack overflow asking about filtering app from android app chooser (though its very old topic). So I thought to write a well describing blog on it.

Problem :  Intent.ACTION_SEND lists all application which can
handle this intent. Sometimes lot of unnecessary application is
also gets listed out and user gets confuse. In most of the
cases I found that user only wants to see Facebook,Twitter,
Gmail or message option in it.

Please find below image.


When simple Intent.ACTION_SEND chosen

As you see all application is being listed.

How to remove the unnecessary apps.

Solution : Use PackageManager.  Rather than removing the unnecessary
apps , I focus on adding necessary app (Positive thinking 😉 ).

Observe the below given function

private void onShareFiltered() {
	Intent emailIntent = new Intent();
	// Native email client doesn't currently support HTML, but it doesn't
	// hurt to try in case they fix it
	emailIntent.putExtra(Intent.EXTRA_TEXT, shareText.getText().toString());
		PackageManager pm = getPackageManager();
	Intent sendIntent = new Intent(Intent.ACTION_SEND);
	Intent openInChooser = Intent.createChooser(emailIntent,
			+ getString(R.string.share_message));
	List resInfo = pm.queryIntentActivities(sendIntent, 0);
	List intentList = new ArrayList();
	for (int i = 0; i < resInfo.size(); i++) {
		// Extract the label, append it, and repackage it in a LabeledIntent
		ResolveInfo ri = resInfo.get(i);
		String packageName = ri.activityInfo.packageName;
		if (packageName.contains("")) {
		} else if (packageName.contains("twitter")
			|| packageName.contains("facebook")) {
			Intent intent = new Intent();
			intent.setComponent(new ComponentName(packageName,;
			if (packageName.contains("twitter")) {
				intent.putExtra(Intent.EXTRA_TEXT, shareText.getText()
			} else if (packageName.contains("facebook")) {
				// Warning: Facebook IGNORES our text. They say
				// "These fields are intended for users to express themselves. 
                                //Pre-filling these fields erodes the authenticity of the user voice."
				// One workaround is to use the Facebook SDK to post, but
				// that doesn't allow the user to choose how they want to
				// share. We can also make a custom landing page, and the
				// link
				// will show the  text from that page
				// with our link in Facebook.
				intent.putExtra(Intent.EXTRA_TEXT, shareText.getText()
			} else if (packageName.contains("")) {
				intent.putExtra(Intent.EXTRA_TEXT, shareText.getText()
			intentList.add(new LabeledIntent(intent, packageName, ri
				.loadLabel(pm), ri.icon));

	// convert intentList to array
	LabeledIntent[] extraIntents = intentList
		.toArray(new LabeledIntent[intentList.size()]);
	openInChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, extraIntents);

After executing the above method from some button click you will have a sharing screen matching like below given screen.

When we add selected apps using PackageManager

When we add selected apps using PackageManager

You can download the source code from here.

Android Tutorials

Intent Service in Android

Today I will demonstrate how to make a queue for any task in Android using Intent Service.

IntentService is a base class of Service in android. It can handle asynchronous request on demand. We can send request through

startService(Intent) method . The service gets started automatically and handles each intent when its turn comes on worker thread.

Differences between Service and IntentService

  1. Service class uses the application’s main thread, while IntentService creates a worker thread and uses that thread to run the service.
  2. IntentService creates a queue that passes one intent at a time to onHandleIntent(). Thus, implementing a multi-thread should be made by extending Service class directly. Service class needs a manual stop using stopSelf(). Meanwhile, IntentService automatically stops itself when there is no intent in queue.
  3. IntentService implements onBind() that returns null. This means that the IntentService can not be bound by default.
  4. IntentService implements onStartCommand() that sends Intent to queue and to onHandleIntent(). In brief, there are only two things to do to use IntentService. Firstly, to implement the constructor. And secondly, to implement onHandleIntent(). For other callback methods, the super is needed to be called so that it can be tracked properly.

To create a queue of few requests we can use IntentService.

Problem : Handle X number of Intents and download Images in background.

Step 1 : Create a Service and write business logic in onHandleIntent()

public class MyQueue extends IntentService {
    String name;

    public MyQueue() {

    protected void onHandleIntent(Intent intent) {
        String img_url = intent.getStringExtra(MainActivity.KEY_URL);
        String fileName = intent.getStringExtra(MainActivity.KEY_NAME);
        try {
            URL url = new URL(img_url);
            HttpURLConnection urlConnection = (HttpURLConnection) url
            File SDCardRoot = Environment.getExternalStorageDirectory();
            File file = new File(SDCardRoot, fileName);
            FileOutputStream fileOutput = new FileOutputStream(file);
            InputStream inputStream = urlConnection.getInputStream();
            byte[] buffer = new byte[1024];
            int bufferLength = 0;
            while ((bufferLength = > 0) {
                fileOutput.write(buffer, 0, bufferLength);
        } catch (MalformedURLException e) {
        } catch (IOException e) {

Step 2 : Declare Service and necessary permission in AndroidManifest.xml file

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

Step 3: Start Service

public final String IMAGE_URL_1 = "";
    public final String IMAGE_URL_2 = "";
    public final String IMAGE_URL_3 = "";

    public static final String KEY_URL = "image_url";
    public static final String KEY_NAME = "file_name";

    public static final String file_1 = "file_1.jpg";
    public static final String file_2 = "file_2.jpg";
    public static final String file_3 = "file_3.jpg";

       Intent intent = new Intent(this, MyQueue.class);
        intent.putExtra(KEY_URL, IMAGE_URL_1);
        intent.putExtra(KEY_NAME, file_1);

        intent = new Intent(this, MyQueue.class);
        intent.putExtra(KEY_URL, IMAGE_URL_2);
        intent.putExtra(KEY_NAME, file_2);

        intent = new Intent(this, MyQueue.class);
        intent.putExtra(KEY_URL, IMAGE_URL_3);
        intent.putExtra(KEY_NAME, file_3);

I have also attached source code for the same example. You can download it from here and understand it in well manner.