Thursday 23 January 2014

Easy way to get money with Google Adsense

Google Adsense


I started writing blog last year just for fun, but after a while a see that my audience increases from day to day, I think how can I use that and then I find out for Google AdSense.

Google AdSense is great service for you to earn some extra money. If you have a blog or even your own site you can add AdSense. Trick is that you allow Google to add ads on your website and when someone click on it Google will pay you.

Everything you need to do is to write a post and sign up for AdSense. If Google recognize your potential you can start earning money from your home right now!



You may think that is hard, I need to write post every day, NO! You don't need to write a post every day you can write it every once a week like me. You can write about football, programing, future, past or anything and still earn money.

You don't worry about ads on your website because Google will add it for you for every person who comes on your website or blog. Google analyze person who comes on site and add ads specific to that person.

So if that person are looking for hotels and visit my post about hotel Google will add ads about hotels.
If you are looking for programming code for android Google will add ads about programing, you don't need to worry about that. You just need to relax clear your mind and write.

If you have some kind of adblock you can't see ads on my blog, if you see it you can think about what did you search last 10 days on internet, because ads will be about that.

Ads have different prices it depends from ads, they can be 0.02 cents up to 2.00 euro. You can track your income with easy adsense interface integrate in blog or website.

And to see that I don't write nonsense watch this youtube video about Google Adsense.





If you like this post please subscribe for more news or just like it share it, do whatever you want with it. Have a good one. M.L. :)


Friday 10 January 2014

Read Email in Java - Android

The easiest way to read email in Java


If you need to read email from Java app you will need mail library. Most famous library for reading email in java is javax.mail.jar and you can find it on this link. That work fine in simple Java app, but if you want to build Android app to read email you will have a big problem. 

There is a problem with mail.jar when you want to build Android app, some classes in mail.jar don't work and when you deploy your android app on phone you will get some error but code is fine. In order to solve that problem you need to use this jar files: activation.jar, additionnal.jar and mail.jar. Javax mail.jar include activation and additional jars but some classes in that jars aren't working in Android so you must download files from this link.

First you say what protocol you gonna use:
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");

Then you open a connection to your server:
Session session = Session.getInstance(props, null);
Store store = session.getStore();
store.connect("imap.gmail.com", "mail@gmail.com", "password");

Open inbox with read_write mode:
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);

If you want you can see how much message you have in inbox:
inbox.getMessageCount();

Get message:
Message msg = inbox.getMessage(inbox.getMessageCount());
 
Write all address:
Address[] in = msg.getFrom();
for (Address address : in) {
    System.out.println("FROM:" + address.toString());
} 
 
Write content of message on console:
Multipart mp = (Multipart) msg.getContent();
BodyPart bp = mp.getBodyPart(0);
 
System.out.println("SENT DATE:" + msg.getSentDate());
System.out.println("SUBJECT:" + msg.getSubject());
System.out.println("CONTENT:" + bp.getContent());


You need to put all that in try block and the code will work from simple Java app to Android app.
If you have any questions or need help with coding post a comment bellow.
And if you like this post please share it. Have a good one.

Friday 3 January 2014

Component Based Development - CBD - Java EJB 3.1 - Example

Blog platform in EJB 3.1


Assignment!
We need to create EJB layer for a blog site.

Clients communicate with stateful session bean. It is necessary to implement the method for search blogs and method for setting up a new blog.
In the case of passivation of the stateful session bean , we need just before passivation to print a message on the console about session bean is going to be pasive.
Singleton bean keeps customer information (name , username , password ... ) and data on blogs ( text, date , number of like- s) in ArrayList.
Singleton is NOT instantiated immediately upon deployment, but by the first call. By instantiate initialize ArrayList in which data need to be stored.
If we call the method that is used to search, interceptor intercepts that method and he update statistics (number of views for each blog).
Call method to set up a new blog interceptor intercepts that method and he update statistics on the number of blogs set up during the day
The statistic data is stored in the same Singleton which are stored and other information .
All interceptor's is required to be implemented in a separate class.
It is necessary to implement a timer that starting from 1.1.2014 . Every day at 00:00 timer need to print on console how many visit blog's have that day.

Please share this post if you like it.

Singleton Class

import java.util.ArrayList;
import java.util.Date;
import javax.ejb.LocalBean;
import javax.ejb.Singleton;

/**
 * Developed by codeMaker
 */

@Singleton
@LocalBean
public class Baza implements BazaLocal {

    public ArrayList<Klijent> listaKlijenata = new ArrayList<Klijent>();
    public ArrayList<Blog> listaBlogova = new ArrayList<Blog>();
   
    int brojPB = 0;
   
    public int getBrojPB() {
        return brojPB;
    }
    public void setBrojPB(int brojPB) {
        this.brojPB = brojPB;
    }

    public void setListaBlogova(ArrayList<Blog> listaBlogova) {
        this.listaBlogova = listaBlogova;
    }
    public void setListaKlijenata(ArrayList<Klijent> listaKlijenata) {
        this.listaKlijenata = listaKlijenata;
    }
    public ArrayList<Klijent> getListaKlijenata() {
        return listaKlijenata;
    }
    public ArrayList<Blog> getListaBlogova() {
        return listaBlogova;
    }
    public Baza() {
        // TODO Auto-generated constructor stub
        Klijent klijent = new Klijent("Niki", "Re", "user", "pass");
        listaKlijenata.add(klijent);
        Blog blog = new Blog("tekst", new Date());
        listaBlogova.add(blog);
    }
}


SingletonLocal interface

import java.util.ArrayList;
import javax.ejb.Local;

@Local
public interface BazaLocal {
    ArrayList<Klijent> getListaKlijenata();
    ArrayList<Blog> getListaBlogova();
    void setListaBlogova(ArrayList<Blog> lista);
    void setListaKlijenata(ArrayList<Klijent> lista);
    int getBrojPB();
    void setBrojPB(int brojPB);
}


Klijent class

import java.util.ArrayList;

public class Klijent {

    public String ime;
    public String prezime;
    public String username;
    public String pass;
   
    public ArrayList<Blog> listaBlogova;
   
    public Klijent(String ime, String prezime, String username, String pass) {
        this.ime = ime;
        this.prezime = prezime;
        this.username = username;
        this.pass = pass;
        listaBlogova = new ArrayList<Blog>();
    }

    public String getIme() {
        return ime;
    }
    public void setIme(String ime) {
        this.ime = ime;
    }
    public String getPrezime() {
        return prezime;
    }
    public void setPrezime(String prezime) {
        this.prezime = prezime;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPass() {
        return pass;
    }
    public void setPass(String pass) {
        this.pass = pass;
    }
    public ArrayList<Blog> getListaBlogova() {
        return listaBlogova;
    }
    public void setListaBlogova(ArrayList<Blog> listaBlogova) {
        this.listaBlogova = listaBlogova;
    }      
}



Blog class

import java.util.Date;

public class Blog {
  
    public String tekst;
    public Date datum;
    public int brojLajkova;
    public int brojPregleda;
   
    public Blog(String tekst, Date datum) {
        this.tekst = tekst;
        this.datum = datum;
        brojLajkova = 0;
        brojPregleda = 0;
    }
    public String getTekst() {
        return tekst;
    }
    public void setTekst(String tekst) {
        this.tekst = tekst;
    }
    public Date getDatum() {
        return datum;
    }
    public void setDatum(Date datum) {
        this.datum = datum;
    }
    public int getBrojLajkova() {
        return brojLajkova;
    }
    public void setBrojLajkova(int brojLajkova) {
        this.brojLajkova = brojLajkova;
    }
    public int getBrojPregleda() {
        return brojPregleda;
    }
    public void setBrojPregleda(int brojPregleda) {
        this.brojPregleda = brojPregleda;
    }


Stateful bean Blogovanje - Clients communicate with him

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.PrePassivate;
import javax.ejb.Stateful;
import javax.interceptor.Interceptors;

@Stateful
@LocalBean
public class Blogovanje implements BlogovanjeRemote {
   
    @EJB BazaLocal baza;
   
    public Blogovanje() {
        

    }
   
    @Interceptors(PretragaInterceptor.class)
    public String pretraga(String upit) {
        List<Blog> lista = baza.getListaBlogova();
        for (Blog b : lista) {
            String tekst = b.getTekst();
            if (tekst.equalsIgnoreCase(upit)) {
                return "Blog je pronadjen!";
            }
        }
        return "Blog nije pronadjen.";
    }
   
    @Interceptors(PretragaInterceptor.class)
    public String postaviBlog(String username, String pass, String tekstBloga) {
       
        int uspjeh = 0;
        ArrayList<Klijent> lista = baza.getListaKlijenata();
        for(Klijent k : lista) {
            String user = k.getUsername();
            String p = k.getPass();
            if((username.equalsIgnoreCase(user)) && (pass.equalsIgnoreCase(p))) {
                Blog b = new Blog(tekstBloga, new Date());
                ArrayList<Blog> listaBlogova = baza.getListaBlogova();
                listaBlogova.add(b);
                baza.setListaBlogova(listaBlogova);
                k.getListaBlogova().add(b);
                uspjeh++;
            }
        }
        baza.setListaKlijenata(lista);
        if (uspjeh > 0) {
            return "Uspeh";
        } else {
            return "Nismo uspjeli da postavimo blog, user i pass ne odgovaraju!";
        }
    }
   
    @PrePassivate
    public void prijePasiviziranja() {
        System.out.println("Jagoda u supermarketu.");
    }
}


BlogovanjeRemote interface - For session bean
Methods in this class will be visible for clients

import javax.ejb.Remote;

@Remote
public interface BlogovanjeRemote {
    String pretraga(String upit);
    String postaviBlog(String username, String pass, String tekstBloga);
}



Interceptors in one class you can build it in seperate classes.

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;

public class PretragaInterceptor {
   
    @EJB BazaLocal baza;
   
    @AroundInvoke
    public Object brojPregledaStatistika(InvocationContext ic) throws Exception {
       
        String imeMetoda = ic.getMethod().getName();
       
        if (imeMetoda.equalsIgnoreCase("pretraga")) {
        Object [] listaO = ic.getParameters();
        String upit = (String) listaO[0];
       
        List<Blog> lista = baza.getListaBlogova();
        for (Blog b : lista) {
            String tekst = b.getTekst();
            if (tekst.equalsIgnoreCase(upit)) {
                b.setBrojPregleda(b.getBrojPregleda() + 1);
            }
        }
       
        return ic.proceed();
        }
       
        if (imeMetoda.equalsIgnoreCase("postaviBlog")) {
           
            Object [] listaO = ic.getParameters();
            String username = (String) listaO[0];
            String pass = (String) listaO[1];
           
            ArrayList<Klijent> lista = baza.getListaKlijenata();
            for(Klijent k : lista) {
                String user = k.getUsername();
                String p = k.getPass();
                if((username.equalsIgnoreCase(user)) && (pass.equalsIgnoreCase(p))) {
                    baza.setBrojPB(baza.getBrojPB() + 1);
                }
            }
            return ic.proceed();
        }
        return ic.proceed();
    }   
}


Timer class

import java.text.SimpleDateFormat;
import java.util.Date;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.Timeout;
import javax.ejb.TimerService;

@Startup
@Singleton
@LocalBean
public class IspisTimer {

    @EJB BazaLocal baza;
    @Resource TimerService ts;
   
    @Timeout
    public void ispsi() {
        System.out.println("Danas je postavljeno: " + baza.getBrojPB() + " blogova!");
        baza.setBrojPB(0);
    }
   
    @PostConstruct
    public void startuj(){
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm");
        Date date = null;
        try {
            date = sdf.parse("31-12-2013 23:59");
        } catch (Exception e) {    }
        ts.createTimer(date, 86400000, null);
    }
}



And Klijent app, so we can test it.

import java.util.Hashtable;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import binovi.Blogovanje;
import binovi.BlogovanjeRemote;

public class Klijent {

    static Context initialContext;

    public static void main(String[] args) {
        try {
            Context context = getInitialContext();
            String name = getLookUpName();
            BlogovanjeRemote bean = (BlogovanjeRemote) context.lookup(name);
            System.out.println();
            System.out.println(bean.postaviBlog("user", "pass", "text"));
            System.out.println(bean.postaviBlog("ssa2", "agawa", "text"));
            System.out.println(bean.pretraga("text"));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String getLookUpName() {

        String appName = "";
        String moduleName = "Blogovanje";
        String distinctName = "";
        String beanName = Blogovanje.class.getSimpleName();
        String interfaceName = BlogovanjeRemote.class.getName();

        String name = "ejb:" + appName + "/" + moduleName + "/" + distinctName
                + "/" + beanName + "!" + interfaceName+"?stateful";

        System.out.println(name);

        return name;
    }

    public static Context getInitialContext() {
           if (initialContext == null) {
            try {
                final Hashtable jndiProperties = new Hashtable();
                jndiProperties.put(Context.URL_PKG_PREFIXES,
                        "org.jboss.ejb.client.naming");
                initialContext = new InitialContext(jndiProperties);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return initialContext;
    }
}


If you have any questions or need help with coding post a comment bellow.
Please share this post if you like it.

Thursday 2 January 2014

Fast screen capture in Java - Example

 Read pixels from screen in Java


If you want to fast read pixels from screen you will need Java class called Robot.
The main purpose of class Robot is to generate native system inputs (mouse, keyboard inputs) for the purpose of test automation. 

If you need to read pixels from screen you can use Robot method called getPixelColor(int x, int y) x and y represents coordinates of pixels you want to grab. Method getPixelColor(...) returns object Color so if you need to now RGB you must use color.getBlue(), color.getGreen(), color.getRed();

This method is good if you want to read 2-3 pixels but if you need to reed a 10x10 pixels from screen i don't recommend using it. Main reason is because getPixelColor is slow, first you read pixel that you create object Color and then you can work with that pixel.

If you want to read 10x10 pixels from screen or more you can use Robot's method called createScreenCapture(Rectangle r); that method returns BufferedImage. First you may think why is he taking Rectangle and returns a image? Method createScreenCapture(...) need to know from which coordinates he must start (x,y) and how much weight and height go right and down.

So you get something like this:
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(new Rectangle(100, 100, 10, 10));

So you can easy capture 10x10 pixels from screen in one line of code, if you want to use getPixelColor in a loop to do that you will need 2 or 3 second for that. (that's a lot!!!)

When you have BufferedImage you can easy save it like "bmp":
ImageIO.write(image, "bmp", new File("Image.bmp"));

If you want to work with pixels you get in BufferedImage you can do it like this:
boolean fail = false;
for (int x = 0; !fail  && x < image.getHeight();  x++) {
                for (int y = 0; !fail  &&  y <
image.getWidth(); y++) {
                    if ( image.getRGB(x, y) != color.getRGB() ) {
                        System.out.println("Pixels are different stop!");
                        fail = true;
                        }
                    }
           }

}
color.getRGB() returns just one color if you need to check are two pictures the same you can use:
if ( image.getRGB(x, y) != image2.getRGB(x, y) 
So if pixels on x, y position on image and image2 are different we stop and write message for that, if we don't get that message fail will stay on false and we can after all this add this:
if (!fail) {
  System.out.println("Pictures are equal!");
}

Robot is powerful class, you can click mouse, keyboard or read pixels from screen.
If you want to learn more about Robot class you can read Java documentation on this link.

If you have any questions or need help with coding post a comment bellow.
And if you like this post please share it. :)