Monday 29 July 2013

Change date format from dd-mm-yyyy to yyyy-mm-dd in java


import java.util.Collections;

import java.util.Vector;



/**

 *

 * @author RajBharath

 */

public class nCrCombination{



    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        // TODO code application logic here

       

        int count = 5;

        Vector prev = new Vector();

        Vector my_list = new Vector();

        for(int tot_item = 0;tot_item<=count;tot_item++)

        {

            prev.add(tot_item);

            my_list.add(tot_item);

        }

        Vector prev1 = new Vector();

        for(int tot_item = 0;tot_item<=count;tot_item++)

        {

                //System.out.println("Size = "+prev.size());

                for(int k = 0;k<prev.size();k++)

                {

                    String tmp = prev.get(k).toString();



                    String tmp_val[] = tmp.split(",");

                    int start = (int)Double.parseDouble(tmp_val[tmp_val.length-1]);

                    for(int item = start+1;item<=count;item++)

                    {

                        prev1.add(tmp+","+item);

                        my_list.add(tmp+","+item);

                        System.out.println(tmp+","+item);

                    }

                }   

               

                prev.clear();

                //System.out.println("Size = "+prev1.size());

                prev.addAll(0,prev1);

                //prev = prev1;

                prev1.clear();

        }

        System.out.println("Tot size = "+my_list.size());

           

    }

   

}




Get all Possible nCr Combinations in Java

In below code to give the results of 1 2 3 4 5. 11 12 13 14 15 nCr combination values.


import java.util.Collections;

import java.util.Vector;



/**

 *

 * @author RajBharath

 */

public class nCrCombination{



    /**

     * @param args the command line arguments

     */

    public static void main(String[] args) {

        // TODO code application logic here

       

        int count = 5;

        Vector prev = new Vector();

        Vector my_list = new Vector();

        for(int tot_item = 0;tot_item<=count;tot_item++)

        {

            prev.add(tot_item);

            my_list.add(tot_item);

        }

        Vector prev1 = new Vector();

        for(int tot_item = 0;tot_item<=count;tot_item++)

        {

                //System.out.println("Size = "+prev.size());

                for(int k = 0;k<prev.size();k++)

                {

                    String tmp = prev.get(k).toString();



                    String tmp_val[] = tmp.split(",");

                    int start = (int)Double.parseDouble(tmp_val[tmp_val.length-1]);

                    for(int item = start+1;item<=count;item++)

                    {

                        prev1.add(tmp+","+item);

                        my_list.add(tmp+","+item);

                        System.out.println(tmp+","+item);

                    }

                }   

               

                prev.clear();

                //System.out.println("Size = "+prev1.size());

                prev.addAll(0,prev1);

                //prev = prev1;

                prev1.clear();

        }

        System.out.println("Tot size = "+my_list.size());

           

    }

   

}



Calling Matlab function from Java

 Download matlabcontrol-4.1.0.jar library from https://code.google.com/p/matlabcontrol/

then try below code



import matlabcontrol.*;

import matlabcontrol.MatlabProxyFactory;

import matlabcontrol.MatlabProxyFactoryOptions;



/**

 *

 * @author RajBharath

 */

public class MatlabCommunication{



    /**

     * @param args the command line arguments

     */

    public static void main(String[] args)  throws Exception

    {

         // create proxy

        MatlabProxyFactoryOptions options = new MatlabProxyFactoryOptions.Builder()

             .setUsePreviouslyControlledSession(true)

             .build();

        MatlabProxyFactory factory = new MatlabProxyFactory(options);

        MatlabProxy proxy = factory.getProxy();



        proxy.eval("disp('hello world')");



        // call user-defined function (must be on the path)

       



        // close connection

        proxy.disconnect();



    }



}


Java Code for Resource Allocation

Here we give the sample for resource allocation to processor.Its process based on resource execution time.


import java.io.*;

import java.util.Random;

/**

 *

 * @author RajBharath

 */

public class ResourceAllocation

{

     int job[]=null,process[]=null;

     int pointer=0;

    ResourceAllocation()

    {

        String str="";

        int n,n1,i;

      

        Random randomGenerator = new Random();

      

        //--------------Resource Allocation

      

        BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Enter the number of Resource : ");

        try

        {

            str = buffer.readLine();

        }

        catch(Exception e){}

        

        n = Integer.parseInt(str);

        job = new int[n];

        System.out.println("Each Resource Execution Time Randomly generated");

        System.out.println("-----------------------");

        for(i = 0;i<n;i++)

        {

            int randomInt = 1 + randomGenerator.nextInt(10);

            job[i] = randomInt;

            System.out.println(" Job - "+(i+1)+" : "+randomInt);

        }

      

        //--------------Processor Allocation

      

        System.out.println("Enter the number of processor : ");

        try

        {

        str = buffer.readLine();

        }

        catch(Exception e)

        {       

        }

        n1 = Integer.parseInt(str); 

        //--------------Tread Allocation

        

        for (i=0;i<n1;i++)

        {

            try

            {

                ThreadClass thread1 = new ThreadClass(job[pointer],n,n1,i);

                thread1.start();

                pointer++;

            }

           catch(Exception e)

            {

            }

        }

    }

  

    class ThreadClass extends Thread

    {

        String message;

        int n,n1,job1,i;

        //int pointer;

        ThreadClass(int job,int n,int n1,int i)

        {

                this.job1=job;

                //this.pointer=pointer;

                this.n=n-1;

                this.n1=n1-1;

                this.i=i;

        }

        public void run()

        {

            while(true)

            {

               try

                {

                     sleep(job1);

                     if (pointer>=n1)

                     {

                         System.out.println("processor="+ i +"--> job="+job1);

                         if (pointer>n)

                         {

                             break;

                         }

                         else

                         {

                         try

                         {

                             pointer++;

                             job1 = job[pointer];

                             this.start();

                            

                         }

                         catch(Exception e){}   

                         }

                    }

                }

                catch(Exception e)

                {

                    System.out.print("Thread Error");

                } 

             }

        }

    }

    public static void main(String arg[]) throws Exception

    {

       new ResourceAllocation();

    }



}




Sunday 28 July 2013

PHP get file contents utf8 example

<?php
 $url = "http://ta.wikipedia.org/wiki/%E0%AE%AE%E0%AF%81%E0%AE%A4%E0%AE%B1%E0%AF%8D_%E0%AE%AA%E0%AE%95%E0%AF%8D%E0%AE%95%E0%AE%AE%E0%AF%8D";
 $string = file_get_contents($url);
// echo $string;
 $string = mb_convert_encoding($string, 'HTML-ENTITIES', "UTF-8");
 $dom = new DOMDocument();
 $dom->preserveWhiteSpace = false;
 $dom->encoding = 'UTF-8';
 $dom->loadHTML($string);
 $mock = new DOMDocument();

 $body = $dom->getElementsByTagName('body')->item(0);
 foreach ($body->childNodes as $child){
  $mock->appendChild($mock->importNode($child, true));
 }
 $html_content = $mock->saveHTML();
 $html = preg_replace('#<script(.*?)>(.*?)</script>#is','', $html_content);
 $html = preg_replace('#<style(.*?)>(.*?)</style>#is','', $html);
 
 $cont = preg_replace("/<.*?>/","",$html);
 
 echo $cont ;
?>



Related Tags :  Php crawl hindi language html content , php crawl tamil html content , PHP crawl utf-8 content.

Thursday 25 July 2013

Matlab mex Gcc Configuration on Windows

Matlab mex Gcc Configuration on Windows

Following Steps to Configure Matlab gcc on windows platform.

1. Install Mingw from http://www.mingw.org/ if 64 bit os means , download 64 bit mingw.

2. Install a Cygwin environment http://www.cygwin.com/ if 64 bit os means , download 64 bit cygwin.

3. Download gnumex matlab packages from http://sourceforge.net/projects/gnumex/files/latest/download.

4. Then Extract the gunmex package on your cygwin root folder (ie c:\cygwin folder).

5. The open Matlab and set path to cygwin root folder(ie c:\cygwin).

6. Type gnumex on matlab command window.

7. Then one GUI windows comes.


8. Click Make options file.

9. Done


if shortpath74.mexw32 and uigetpath74.mexw32 problem means please recompile src package's on gumex.

>>mex shortpath.c
>>mex uigetpath.c


if any error means comment in below.

Matlab Wireless Network Simulation code

In below code to simulate a node on Wireless networks.


clc
clear all
close all
warning off

net_width = 10;
net_height = 80;
check = 1;
tot_nodes = 20;
traves_MS = 10;
hT = 4;


% Form Network area....
node_count = 1;
all_x = [];
all_y = [];
for j = 0:9:net_height
    if check==1
        start = 0;
        check = 0;
    else
        start = 5;
        check = 1;
    end
    for i = start:10.3:net_height
        circle([j,i],6,7,'-');
        hold on
        drawnow
        plot(j,i,j,i,'.','color','green');
        nodes(node_count).x = j;
        nodes(node_count).y = i;
        nodes(node_count).id = node_count;
        node_count = node_count+1;
        all_x = [all_x;i];
        all_y = [all_y;j];
    end
end
axis square;

min_x = min(all_x);
max_x = max(all_x);

min_y = min(all_y);
max_y = max(all_y);


% Making Random nodes....
for i = 1:tot_nodes
    x = round(min_x+(max_x-min_x)*rand);
    y = round(min_y+(max_y-min_y)*rand);
    pnt1 = [x y] ;
    best_node_inx = best_dist(pnt1,nodes);
    best_x = nodes(best_node_inx).x;
    best_y = nodes(best_node_inx).y;
    best_pnt = [best_x best_y];
    
    plot(x,y,x,y,'*','color','red');
    all_x = [pnt1(1) best_pnt(1)];
    all_y = [pnt1(2) best_pnt(2)];
    plot(all_x,all_y);
    hold on
    drawnow
end 


% Making Moving Nodes...
s_min_x = min_x+0.5;
s_min_y = min_y+0.5;
con_x = 1;
check = 1;
totms = 0;
traverse_node = [];

station_count = [];
station_data = [];
sel_nodes = [];
last_check_pnt = 1;
while(totms~=traves_MS)
    
    if s_min_x<=max_x&&check == 1
        check = 1;
    else
        check = 0;
        if s_min_x<=min_x && check == 0
            check = 1;
        end
    end
    if check ==1
        s_min_x = s_min_x+1;
        s_min_y = s_min_y+1;
    else
        s_min_x = s_min_x-1;
        s_min_y = s_min_y-1;
    end
    
    
    h1 = plot(s_min_x,s_min_y,s_min_x,s_min_y,'*','color','red');
    pnt1 = [s_min_x s_min_y] ;
    best_node_inx = best_dist(pnt1,nodes);
    best_x = nodes(best_node_inx).x;
    best_y = nodes(best_node_inx).y;
    best_pnt = [best_x best_y];
    hold on
    drawnow
    all_x = [pnt1(1) best_pnt(1)];
    all_y = [pnt1(2) best_pnt(2)];
    h2 = plot(all_x,all_y);
    pause(0.1);
    delete(h1);
    delete(h2);
end


Output


Read Landsat Image on Matlab

Here we give some example for read lan file on matlab.


clc
clear all
close all


file = 'lansetimgfile.lan';


IR1 = multibandread(file, [512, 512, 7], 'uint8=>uint8',...
                    128, 'bil', 'ieee-le', {'Band','Direct',[1]});

IR2 = multibandread(file, [512, 512, 7], 'uint8=>uint8',...
                    128, 'bil', 'ieee-le', {'Band','Direct',[2]});

IR3 = multibandread(file, [512, 512, 7], 'uint8=>uint8',...
                    128, 'bil', 'ieee-le', {'Band','Direct',[3]});

IR4 = multibandread(file, [512, 512, 7], 'uint8=>uint8',...
                    128, 'bil', 'ieee-le', {'Band','Direct',[4]});

IR5 = multibandread(file, [512, 512, 7], 'uint8=>uint8',...
                    128, 'bil', 'ieee-le', {'Band','Direct',[5]});

IR6 = multibandread(file, [512, 512, 7], 'uint8=>uint8',...
                    128, 'bil', 'ieee-le', {'Band','Direct',[6]});

IR7 = multibandread(file, [512, 512, 7], 'uint8=>uint8',...
                    128, 'bil', 'ieee-le', {'Band','Direct',[7]});

figure;imshow(IR);
title('1st Band Image');

figure;imshow(IR);
title('2rd Band Image');

figure;imshow(IR);
title('3rd Band Image');

figure;imshow(IR);
title('4th Band Image');

figure;imshow(IR);
title('5th Band Image');

figure;imshow(IR);
title('6th Band Image');

figure;imshow(IR);
title('7th Band Image');


Related Tags : Matlab code for read lan file, Read Landsat Image matlab code , read lan file in matlab example code.

Image Watermarking Matlab code

Here We Give the code of Image watermarking.


K=8;                                      
Q=50;                                     
W=im2bw(imread('s1.jpg'));                
Mm=size(W,1);                             
Nm=size(W,2);  
  
  
figure(1);  
subplot(321);  
imshow(W);  
title('the orginal watermark');  
  
I=imread('s12.jpg');  
subplot(322);  
imshow(I);  
title('the cover image');  
  
  
II=I;                   % to save the original image(I)  
  
blockrow=Mm;  
blockcol=Nm;  
  
for i=1:blockrow  
    for j=1:blockcol  
        x=(i-1)*K+1;  
        y=(j-1)*K+1;  
        BLOCK=II(x:x+K-1,y:y+K-1);  
        [U,S,V]=svd(double(BLOCK));  
  
        bit=W(i,j);                             %get the one bit wateramrk=bit  
        remainder=rem(S(1,1),Q);  
        if (bit==1)                             %embedding bit '1'  
            if (remainder<=Q/4)  
                S(1,1)=S(1,1)-remainder-Q/4;  
            else   
                S(1,1)=S(1,1)-remainder+3*Q/4;  
            end  
        else                                    %embedding bit '0'  
            if (remainder>=3*Q/4)  
                S(1,1)=S(1,1)-remainder+5*Q/4;  
            else  
                S(1,1)=S(1,1)-remainder+Q/4;  
            end  
        end  
  
        BLOCKW=U*S*V';                    
          
        II(x:x+K-1,y:y+K-1)=uint8(round(BLOCKW));  
          
    end  
end  
  
subplot(324);  
imshow(II);  
title('the watermarked image');  
  
% psnr1=PSNR(I,II)                            
  
A=double(II)-double(I);  
rsm=0;  
for i=1:size(A,1)  
    for j=1:size(A,2)  
        rsm=rsm+A(i,j)*A(i,j);  
    end  
end  
rsm=sqrt(rsm)/(size(A,1)*size(A,2))       
  
% to extracting the wateramrk from image(II)  
for i=1:blockrow  
    for j=1:blockcol  
        x=(i-1)*K+1;  
        y=(j-1)*K+1;  
        BLOCK=II(x:x+K-1,y:y+K-1);  
        [U,S,V]=svd(double(BLOCK));  
          
        remainder=rem(S(1,1),Q);  
        if (remainder>Q/2)  
            EW(i,j)=1;  
        else  
            EW(i,j)=0;  
        end  
    end  
end  
subplot(323);  
imshow(EW);  
title('the extracted wateramrk');  
  
% the watermarked image is attacked   
imwrite(uint8(II),'attack.jpg','jpeg','Quality',70);           %
II1=imread('attack.jpg');  
  

subplot(326);  
imshow(II1);  
title('the attacked image');  
  
for i=1:blockrow  
    for j=1:blockcol  
        x=(i-1)*K+1;  
        y=(j-1)*K+1;  
        BLOCK=II1(x:x+K-1,y:y+K-1);  
        [U,S,V]=svd(double(BLOCK));  
          
        remainder=rem(S(1,1),Q);  
        if (remainder>Q/2)  
            EW(i,j)=1;  
        else  
            EW(i,j)=0;  
        end  
    end  
end  
subplot(325);  
imshow(EW);  
title('the extracted wateramrk');  
  
corr2=NC(double(W),double(EW))              
psnr2=PSNR(I,II1)                           

Matlab Code for Client Server Communication

Server Configuration matlab code

data = 'Hai';
s = whos('data')
tcpipServer = tcpip('0.0.0.0',55000,'NetworkRole','Server');
set(tcpipServer,'OutputBufferSize',s.bytes);
fopen(tcpipServer);
fwrite(tcpipServer,data(:),'char');
fclose(tcpipServer);

Client Configuration matlab code

tcpipClient = tcpip('192.168.1.106',55000,'NetworkRole','Client')
set(tcpipClient,'InputBufferSize',7688);
fopen(tcpipClient);
rawData = fread(tcpipClient,961,'double');
fclose(tcpipClient);

Normalized Cross Correlation ,Normalized Mean Error , RMSE Calculation Matlab Code

Here We give the matlab code for Normalized Cross Correlation ,Normalized Mean Error and RMSE Calculations.

Normalized Cross Correlation matlab code


function NK = NormalizedCrossCorrelation(origImg, distImg)
if size(origImg,3)==1
    origImg = double(origImg);
    distImg = double(distImg);

    NK = sum(sum(origImg .* distImg)) / sum(sum(origImg^2));
else
    r_rm = NormalizedCrossCorrelation(origImg(:,:,1),distImg(:,:,1));
    g_rm = NormalizedCrossCorrelation(origImg(:,:,2),distImg(:,:,2));
    b_rm = NormalizedCrossCorrelation(origImg(:,:,3),distImg(:,:,3));
    NK = (r_rm+g_rm+b_rm)/3; 
end

 NormalizedMeanError matlab code


function NAE = NormalizedMeanError(origImg, distImg)
if size(origImg,3)==1
    origImg = double(origImg);
    distImg = double(distImg);

    error = origImg - distImg;

    NAE = sum(sum(abs(error))) / sum(sum(origImg));
else
    r_rm = NormalizedMeanError(origImg(:,:,1),distImg(:,:,1));
    g_rm = NormalizedMeanError(origImg(:,:,2),distImg(:,:,2));
    b_rm = NormalizedMeanError(origImg(:,:,3),distImg(:,:,3));
    NAE = (r_rm+g_rm+b_rm)/3; 

end


Root Mean Square Error (RMSE) Matlab code


function rmse_val = RMSE_calc(r,f)
if size(r,3)==1
    [m n] = size(r);
    rmse_val = sqrt(sum(double(r(:))-double(f(:))).^2)/(m*n);
else
    r_rm = RMSE_calc(r(:,:,1),f(:,:,1));
    g_rm = RMSE_calc(r(:,:,2),f(:,:,2));
    b_rm = RMSE_calc(r(:,:,3),f(:,:,3));
    rmse_val = (r_rm+g_rm+b_rm)/3;

end

Sunday 21 July 2013

PHP php_printer.dll Installation and Configuration


1. Download xampp Server from http://www.oldapps.com/xampp.php?old_xampp=44

2. Install it

3. Goto Xammp Home directory (ex C:\Xampp)

4. Then open php flder

5. Then open php.ini file.

6. Find ;extension=php_printer.dll

7. Then Remove ";"

8. Done

Without Xampp users

1. Download lates php_print.dll from http://us.php.net/get/pecl-5.2.6-Win32.zip/from/a/mirror

2. After download and extract, you get php_print.dll.

3. Copy this dll and goto your php\ext directory and paste it.

4. Then open php.ini file.

5. Find (extension_dir="C:\Program Files\PHP\ext") on that file.

6. The add this line (extension=php_printer.dll) line.

7. DOne


Example for php print to printer example.

<?php
$printer = "\\\\servername\\printername";
$handle = printer_open($printer);
        printer_set_option($handle, PRINTER_MODE, "raw");
printer_set_option($handle, PRINTER_PAPER_FORMAT, PRINTER_FORMAT_A5);
$output = "Print Contents";
printer_write($handle,$output);
    printer_close($handle);
?>

PHP Multithreading Example and php_pthreads Configurataion

If Php Support Multithreading means you must configure following things

1. Download Php with Thread Safe from http://windows.php.net/download/

2. After Php installation, download pthreads libraries from http://windows.php.net/downloads/pecl/releases/pthreads/

3. After download and extraction , copy php_pthreads.dll and pthreadVC2.dll this files.

4. Then goto your Php\ext directory and paste it.

5. Then  goto your PHP directory and find and rename "php.ini-development" file to "php.ini".

6. Open php.ini file in Notepad and find ;extension=php_shmop.dll line and add extension=php_pthreads.dll after that line.

7. Then Find ;extension_dir = "ext" line and remove ; from that line.

8. Finaly Save that php.ini file

9. Done

Then Run Below Multithreading Example.


PHP Multithreading Example

<?php
class MultiThread_EX extends Thread
{
    public function __construct($arg)
    {
        $this->arg = $arg;
    }

    public function run()
    {
        if($this->arg){
            printf("Hello %s\n", $this->arg);
        }
    }
}

$thread = new MultiThread_Ex("World");
if($thread->start())
$thread->join();



Matlab Simultaneous Plot

Here we give the sample code for Simultaneous Plot.


t = -pi:pi/300:pi;
axis([-1 1 -1 1 -1 2])
hold on
for i=1:2:length(t)-1
T=[t(i) t(i+1)];
plot3(sin(5*T),cos(3*T),T,'r')
plot3(sin(5*T),sin(3*T),T,'b')
pause(0.000001)
end
hold off

Output


Thursday 4 July 2013

YouTube Copyright School Question and Answers

This below some Q & A are faced by me.

If you are found to be a repeat infringer, you could lose your YouTube account.
You chose: True
Correct!
Users suspended for copyright infringement are forbidden from opening new accounts.
The following items are NOT protected by copyright:
You chose: People
Correct!
People are not protected by copyright. Sorry.
If someone alleges that you have infringed their copyright and you are certain that this is not the case, you may:
You chose: File a counter-notification
Correct!
If you believe your content was misidentified as infringing (as the result of a mistake or misidentification of the material claimed to be infringing), you may file a counter-notification.
If a video gets removed because of a copyright infringement notification, filing a counter-notification is the only possible way to restore the video.
You chose: True
Incorrect.
You may reach out to content owners and come to an agreement that they will retract their claim of copyright infringement. Should a claimant contact YouTube directly with a retraction, YouTube will be able to reinstate video content almost immediately.



If the original creator of the copyrighted work has died, the work is no longer protected by copyright.
You chose: False
Correct!
Copyright protection can continue even after the creator’s death.
Copyright infringement occurs when a copyrighted work is ________ without the permission of the copyright owner:
You chose: All of the above
Correct!
Copyright protects each of these individual rights.
Content that was once allowed by a content owner may be subsequently removed from YouTube.
You chose: True
Correct!
Owners can change their mind about how their content is displayed on YouTube. For this reason, creating completely original content is the best protection against copyright infringement claims.
If you claim fair use in the video description, your video can't be considered copyright infringement.
You chose: False
Correct!
Fair use cases can only be determined in court. You should seek legal advice from a qualified copyright attorney for help determining if your 
Related Posts Plugin for WordPress, Blogger...