Our Feeds

Monday 9 March 2015

Ajith KP

Create a bootable USB drives of Window OS

         Rufus is a utility that helps format and create bootable USB flash drives, such as USB keys/pendrives, memory sticks, etc. In my opinion Rofus is better than other utilities which does same, because it is easy to use. You can understand how to create bootable USB using Rofus from bellow screenshots.


After Finish
Home page: http://rufus.akeo.ie
Download Link: rufus.akeo.ie/downloads/rufus-2.0.exe

Sunday 8 March 2015

AJITH KP

JSON Parser Tool For Android

          JSON stands for JavaScript Object Notation.It is an independent data exchange format and is the best alternative for XML. This snippet will help you to parse JSON in Android.

          You need to download and import Apache Http Client libraries from here: https://hc.apache.org/downloads.cgi?Preferred=http%3A%2F%2Fapache.arvixe.com%2F

          Or add the line bellow in your build.gradle file.

android{
    compileSdkVersion xx
    buildToolsVersion 'xx.x.x'
    useLibrary  'org.apache.http.legacy'
}

JSON Page
JSON Data in Android Application

          Video Tutorial


          JSONTool.java

import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.*;

import java.net.HttpCookie;
import java.util.List;

/**
 * Coded By AJITH KP [@ajithkp560]
 * Visit: www.terminalcoders.blogspot.com
 */
public class JSONTool {
    static InputStream is = null;
    static JSONObject jobj = null;
    static JSONArray  jarr = null;
    List<NameValuePair> params = null;
    String url;
    List<Cookie> cookies;
    String strCookie;
    String response;
    String method="get";
    boolean flg = false;
    JSONTool(String u)
    {
        url = u;
    }
    JSONTool()
    {

    }
    JSONTool(String u, List<NameValuePair> p)
    {
        params = p;
    }
    public void setUrl(String u)
    {
        url = u;
    }
    public void setParams(List<NameValuePair> p)
    {
        params = p;
    }
    public void setMethod(String m)
    {
        method = m.toLowerCase();
    }
    public JSONObject getJSONObject()
    {
        try
        {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;
            if(params==null)
            {
                HttpGet httpGet = new HttpGet(url);
                if(flg)
                {
                    httpGet.setHeader("Cookie",strCookie);
                }
                else {
                    cookies = httpClient.getCookieStore().getCookies();
                }

                httpResponse = httpClient.execute(httpGet);
            }
            else if(params!=null && method.equals("get"))
            {
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url+="?"+paramString;
                HttpGet httpGet = new HttpGet(url);
                if(flg)
                {
                    httpGet.setHeader("Cookie", strCookie);
                }
                else {
                    cookies = httpClient.getCookieStore().getCookies();
                }

                httpResponse = httpClient.execute(httpGet);
            }
            else
            {
                HttpPost httpPost = new HttpPost(url);
                if(flg)
                {
                    httpPost.setHeader("Cookie", strCookie);
                }
                else {
                    cookies = httpClient.getCookieStore().getCookies();
                }

                httpPost.setEntity(new UrlEncodedFormEntity(params));
            }
            httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

            if(!flg) {
                cookies = httpClient.getCookieStore().getCookies();
            }

            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            response = sb.toString();

            Log.d("Message", response);
            jobj = new JSONObject(response);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jobj;
    }
    public JSONObject getJSONObject(String u, List<NameValuePair> l, String m)
    {
        params = l;
        method = m;
        url = u;
        try
        {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;
            if(params==null)
            {
                HttpGet httpGet = new HttpGet(url);
                if(flg)
                {
                    httpGet.setHeader("Cookie", strCookie);
                }
                else {
                    cookies = httpClient.getCookieStore().getCookies();
                }
                httpResponse = httpClient.execute(httpGet);
            }
            else if(params!=null && method.equals("get"))
            {
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url+="?"+paramString;
                HttpGet httpGet = new HttpGet(url);
                if(flg)
                {
                    httpGet.setHeader("Cookie", strCookie);
                }
                else {
                    cookies = httpClient.getCookieStore().getCookies();
                }
                httpResponse = httpClient.execute(httpGet);
            }
            else
            {
                HttpPost httpPost = new HttpPost(url);
                if(flg)
                {
                    httpPost.setHeader("Cookie", strCookie+";");
                }
                else {
                    cookies = httpClient.getCookieStore().getCookies();
                }
                httpPost.setEntity(new UrlEncodedFormEntity(params));
                httpResponse = httpClient.execute(httpPost);
            }
            httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

            if(!flg) {
                cookies = httpClient.getCookieStore().getCookies();
            }

            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            response = sb.toString();

            Log.d("Message", response);
            jobj = new JSONObject(response);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jobj;
    }
    public String getString()
    {
        try
        {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;
            if(params==null)
            {
                HttpGet httpGet = new HttpGet(url);
                if(flg)
                {
                    httpGet.setHeader("Cookie", strCookie);
                }
                else {
                    cookies = httpClient.getCookieStore().getCookies();
                }
                httpResponse = httpClient.execute(httpGet);
            }
            else
            {
                HttpPost httpPost = new HttpPost(url);
                if(flg)
                {
                    httpPost.setHeader("Cookie", strCookie);
                }
                else {
                    cookies = httpClient.getCookieStore().getCookies();
                }
                httpPost.setEntity(new UrlEncodedFormEntity(params));
                httpResponse = httpClient.execute(httpPost);
            }
            httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            response = sb.toString();
            Log.d("Message", response);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }

    public String getString(String u, List<NameValuePair> l, String m)
    {
        url = u;
        params = l;
        method = m.toLowerCase();

        params = l;
        method = m;
        url = u;
        try
        {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;
            if(params==null)
            {
                HttpGet httpGet = new HttpGet(url);
                if(flg)
                {
                    httpGet.setHeader("Cookie", strCookie);
                }
                else {
                    cookies = httpClient.getCookieStore().getCookies();
                }
                httpResponse = httpClient.execute(httpGet);
                httpEntity = httpResponse.getEntity();
            }
            else if(params!=null && method.equals("get"))
            {
                String paramString = URLEncodedUtils.format(params, "utf-8");
                url+="?"+paramString;
                HttpGet httpGet = new HttpGet(url);
                if(flg)
                {
                    httpGet.setHeader("Cookie", strCookie);
                }
                else {
                    cookies = httpClient.getCookieStore().getCookies();
                }
                httpResponse = httpClient.execute(httpGet);
                httpEntity = httpResponse.getEntity();
            }
            else
            {
                HttpPost httpPost = new HttpPost(url);
                if(flg)
                {
                    httpPost.setHeader("Cookie", strCookie);
                }
                else {
                    cookies = httpClient.getCookieStore().getCookies();
                }
                httpPost.setEntity(new UrlEncodedFormEntity(params));
                httpResponse = httpClient.execute(httpPost);
                httpEntity = httpResponse.getEntity();
            }

            is = httpEntity.getContent();

            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            response = sb.toString();

            Log.d("Message", response);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return response;
    }

    public List<Cookie> getCookies()
    {
        return cookies;
    }

    public void setCookies(String c)
    {
        strCookie = c;
        flg = true;
    }
}

How To Use

           You can use the snippet in Android project. First save the code as JSONTool.java and import it to your package.

           Constructors

JSONTool tool = new JSONTool(); //Without argument
tool.setUrl("http://10.0.2.2/json.php"); //Setting URL
JSONTool tool = new JSONTool("http://10.0.2.2/json.php");//With url argument

            Set Parameter

tool.setParams(params);

           Set Method

tool.setMethod("get"); //method="get"
tool.setMethod("post"); //method="post"

           Getting JSONObject

JSONObject obj = tool.getJSONObject(); //You can use if the URL and Method is set. Else
JSONObject obj = tool.getJSONObject("http://10.0.2.2/json.php", params, "get");

            Getting JSON String

String res = tool.getString(); //You can use if the URL and Method is set. Else
String res = tool.getString("http://10.0.2.2/json.php", params, "get");

            Sample Code

package com.blogspot.terminal;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.TextView;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class MyActivity extends Activity {
    /**
     * Called when the activity is first created.
     */
    TextView v;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        v = (TextView) findViewById(R.id.lbl);
        new sendPostData().execute();
    }

    private class sendPostData extends AsyncTask<String, Void, String>
    {
        JSONTool jtool = new JSONTool();
        String data;
        @Override
        protected String doInBackground(String... params) {

            try{
                List<NameValuePair> param = new ArrayList<NameValuePair>();
                param.add(new BasicNameValuePair("name","Ajith"));
                param.add(new BasicNameValuePair("mail","ajithkp560@gmail.com"));
                JSONObject obj = jtool.getJSONObject("http://10.0.2.2/ajith/json.php", param, "post");
                data = obj.getString("name")+" : "+obj.getString("email");
            }catch(Exception e){
                return new String("Exception: " + e.getMessage());
            }
            return data;
        }
        @Override
        protected void onPostExecute(String result) {
            //View your result here.
            v.setText(result);
        }
    }
}

Saturday 7 March 2015

AJITH KP

Add Graphical Component to Image using Java

          Hi guys... It is very simple program to parse an image and add new graphical components to image. Here I used Graphics2D package to add a rectangle to highlight face... Hope you enjoy this tutorial and share it with your friends.



VIDEO TUTORIAL


SOURCE CODE


import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class MarkImage extends JFrame {
    JButton btn = new JButton("Open File");
    JPanel imagePanel = new JPanel();
    JPanel menuPanel = new JPanel();
    double h, w;
    MarkImage()//Constructor
    {
        super("Mark Image -- Coded By Ajith Kp [ ajithkp560 ]");
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fc = new JFileChooser();
                int ret = fc.showOpenDialog(MarkImage.this);
                if(ret==JFileChooser.APPROVE_OPTION)
                {
                    File imgf = fc.getSelectedFile();
                    imagePanel.removeAll();
                    try {
                        setPic(imgf);
                    } catch (IOException ex) {
                    }
                }
            }
        });
        menuPanel.add(btn);
        add(menuPanel, BorderLayout.NORTH);
        add(imagePanel, BorderLayout.CENTER);
    }
    
    public void setPic(File imgf) throws IOException
    {
        BufferedImage myImg = ImageIO.read(imgf);
        double x = myImg.getWidth();
        double y = myImg.getHeight();
        h = imagePanel.getHeight();
        w= imagePanel.getWidth();
        if(x>w || y>h)
        {
            double ox = w/x, oy = h/y;
            System.out.println(ox+" "+oy);
            if(ox<oy)
            {
                x = x*ox;
                y = y*ox;
            }
            else
            {
                x = x*oy;
                y = y*oy;
            }
        }
        
        Graphics2D g2 = myImg.createGraphics();
        g2.setColor(Color.red);
        g2.setStroke(new BasicStroke(20));
        g2.drawRect(650, 220, 600, 600);
        
        Image newimg = myImg.getScaledInstance((int)x, (int)y,  Image.SCALE_SMOOTH);
        JLabel picLabel = new JLabel(new ImageIcon(newimg));
        imagePanel.add(picLabel);
        imagePanel.revalidate();
        imagePanel.repaint();
    }
    
    public static void main(String[] args) {
        MarkImage dn = new MarkImage();
        dn.setVisible(true);
        dn.setSize(1200, 800);
        dn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
}

Thursday 5 March 2015

AJITH KP

Detect Day or Night from Image Java

          Java provides few inbuilt classes for view, edit and manage images. This is very simple program to detect an image is taken day or night.

Video Tutorial



Screenshots






import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
/*

    Coded By Ajith Kp (c) http://TerminalCoders.BlogSpot.in (c)
*/
public class DayNight extends JFrame {
    JButton openB = new JButton("Open");
    JPanel imagePanel = new JPanel();
    JPanel menuPanel = new JPanel();
    DayNight()
    {
        super("DayNight Detector Java By Ajith Kp [ ajithklp560 ");
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        setMaximizedBounds(env.getMaximumWindowBounds());
        setExtendedState(getExtendedState() | MAXIMIZED_BOTH);
        openB.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fc = new JFileChooser();
                int returnVal = fc.showOpenDialog(DayNight.this);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    try {
                        File imgf = fc.getSelectedFile();
                        imagePanel.removeAll();
                        setPic(imgf);
                    } catch (IOException ex) {
                    }
                }
            }
        });
        menuPanel.add(openB);
        add(menuPanel, BorderLayout.NORTH);
        add(imagePanel, BorderLayout.CENTER);
    }
    public void setPic(File imgf) throws IOException
    {
        BufferedImage myImg = ImageIO.read(imgf);
        int w = myImg.getWidth();
        int h = myImg.getHeight();
        
        JLabel picLabel = new JLabel(new ImageIcon(myImg));
        imagePanel.add(picLabel);
        imagePanel.revalidate();
        imagePanel.repaint();
        
        double sr = 0, sg = 0, sb = 0;
        double R, G, B;
        int len = w*h;
        for(int i=0;i<w;i++)
        {
            for(int j=0;j<h;j++)
            {
                Color c = new Color(myImg.getRGB(i, j));
                sr+=c.getRed();
                sg+=c.getGreen();
                sb+=c.getBlue();
            }
        }
        sr = (sr/len)/255.0;
        sg = (sg/len)/255.0;
        sb = (sg/len)/255.0;
        if(sr <= 0.03928)
        {
            R = sr/12.92;
        }
        else
        {
            R = Math.pow((sr+0.055)/1.055, 2.4);
        }
        if(sg <= 0.03928)
        {
            G = sg/12.92;
        }
        else
        {
            G = Math.pow((sg+0.055)/1.055, 2.4);
        }
        if(sb <= 0.03928)
        {
            B = sb/12.92;
        }
        else
        {
            B = Math.pow((sb+0.055)/1.055, 2.4);
        }
        double y = 0.2126*R+0.7152*G+0.0722*B;
        if(y<0.07)
        {
            JOptionPane.showMessageDialog(null, "Night Photo", "Day Or Night", JOptionPane.INFORMATION_MESSAGE);
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Day Photo", "Day Or Night", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    public static void main(String[] args) {
        // TODO code application logic here
        DayNight dn = new DayNight();
        dn.setSize(700, 500);
        dn.setVisible(true);
        dn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
}