Saturday, 25 January 2014

ARDUINO AND MATLAB SERIAL COMMUNICATION


                                                       I think gesture recognition would be more exciting if you could control things in real world with your gestures... So here's how to do it using Arduino. Since it just uses serial communication, this can also be done with some usb to serial adapters.... 



                                                       This simulation actually shows how to control of an led (to get started with) with some clicking over the screen with fingers in the air..... 

Click here to download matlab source...

Here's a Demo video....



And here's the arduino code...

//Initializing variables...
int incomingByte = 0;
int led = 12;
int blnk = 0;
int del = 500;
void setup() {                
  pinMode(led, OUTPUT);     
  Serial.begin(9600);
}

void loop() {
//Main loop...  
  if (Serial.available() > 0) {
 incomingByte = Serial.read();    
    switch (incomingByte) {
    //ASCII value for 'A'
      case 65:
 digitalWrite(led,HIGH);
 blnk = 0;
      break;
    case 66:
 digitalWrite(led,LOW) ;
 blnk = 0;
      break;
      case 67:
 blnk = 1;
      break;   
  }
        }
        if (blnk==1){
         digitalWrite(led,HIGH);
         delay(del);
         digitalWrite(led,LOW);
         delay(del);
        }
}

You can also check the following link where you can find library over this....

Sunday, 19 January 2014

HAND GESTURE NUMBER RECOGNITION





Click here to download the Source

                                                   Just do the gestures and it will recognize what you have shown as gesture. This simulation identifies the number of fingers shown by a person. It uses simple binary image processing recognizing the hand based mostly on size specifications.



 Then after identifying the hand, it clips off the lower part of hand in such a way that only fingers are left. Then the number of objects in the image would be the number of fingers shown.





Here’s a demo video… 


Saturday, 18 January 2014

SHORTEST PATH FINDER FROM A MAZE IMAGE

                             This matlab code finds the shortest path that can be traversed to go through a maze from starting pointing to required ending point. The algorithm used is simple and straight forward. This code first looks for one of the possible path from starting point to destination. This path is actually obtained by looking for a way to make the next move in a specific direction (here down first) and moves in the direction if there is a possible way. If not then it checks for left and moves in that direction if there is a possible way and finally right if there isn't any path in the down and left direction.

Here are some images showing possible ways that the object can move in different situations.

1
Object moves left
2
Moves down
3
Moves back twice and then right

Now that we found one of the possible path we go now in the reverse direction and find all possible paths. Since we have all possible paths all we need to do is find the shortest among them by simple calculations.
The following video shows how all the possible paths are obtained.....




Here's the source. Just unzip it and use it in whatever way you want...

Wednesday, 15 January 2014

MEDIA PLAYER CONTROL WITH GESTURES


                                         This simulation uses the same red object tracking shown in previous posts to interact with a GUI. This GUI controls the media player.

A video...




Source....

clear all;
clc;
close all
%setting the first song
i = 1;
%Accessing media player
actx = actxcontrol('WMPlayer.ocx.7');
%Creating playlist
media(1) = actx.newMedia('C:\Manji\Backstreet Boys - Show Me The Meaning.mp3');
media(2) = actx.newMedia('C:\Manji\Baby.mp3');
media(3) = actx.newMedia('C:\Manji\pussycat dolls - jai ho .mp3');
%getting the number of tunes
imax1 = size(media);
imax = imax1(2);
%Playing the tune
actx.CurrentMedia = media(i);
actx.Controls.play
%Accesing the camera and initializing the variables...
vid = videoinput('winvideo', 1, 'YUY2_640x480');
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 1;
    start(vid)
    count = 0;
    mem = 2;
    bc(2,mem) = 0;
    bc(1,mem) = 0;
    %Setting the graphics
r = ['r', 'g', 'r'];
p1 = 'previous';
p2 ='pause';
p3 = 'next';
I(480,640) = 255;
I(:,:) = 255;
imshow(I)
hold on
 rectangle('Position',[320,200,100,50],'FaceColor',r(2))
rectangle('Position',[150,200,100,50],'FaceColor',r(1))
rectangle('Position',[480,200,100,50],'FaceColor',r(3))
 a(1) = text(170,220,p1);
set(a(1), 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'b');
a(2) = text(340,220,p2);
set(a(2), 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'b');
a(3) = text(500,220,p3);
set(a(3), 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'b');
%Identifying the blue object
while(1)
    data = getsnapshot(vid);
    data(:,:,1) = fliplr(data(:,:,1));
    data(:,:,2) = fliplr(data(:,:,2));
    data(:,:,3) = fliplr(data(:,:,3));
        %Color processing...
    diff_im = imsubtract(data(:,:,1), rgb2gray(data));
    diff_im = medfilt2(diff_im, [3 3]);
    diff_im = im2bw(diff_im,0.18);
    diff_im = bwareaopen(diff_im,300);  
    bw = bwlabel(diff_im, 8);
    stats = regionprops(bw, 'Centroid');
    if ~isempty(stats)
        count = count+1;
           bc(1,1) = bc(1,mem);
           bc(2,1) = bc(2,mem);
      bc(1,mem) = stats(1).Centroid(1);
      bc(2,mem) = stats(1).Centroid(2);
      %Button logic toggle if the previous cordinate is not inside the box
      %and present coordinate is inside the box
       if (((bc(1,mem)<420)&&(bc(1,mem)>320))&&((bc(2,mem)<250)&&(bc(2,mem)>200))&&(((bc(1,mem-1)>420)||(bc(1,mem-1)<320))||((bc(2,mem-1)>250)||(bc(2,mem-1)<200))))
           %Play and pause buttun logic
          if (r(2) == 'g')
              r(2) = 'r';
              p2 = 'play';
              actx.Controls.pause
               rectangle('Position',[320,200,100,50],'FaceColor',r(2))
              a(2) = text(340,220,p2);
              set(a(2), 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'b');
          else
              r(2) = 'g';
              p2 = 'pause';
              actx.Controls.play
               rectangle('Position',[320,200,100,50],'FaceColor',r(2))
              a(2) = text(340,220,p2);
              set(a(2), 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'b');
          end
       end
       if (((bc(1,mem)<250)&&(bc(1,mem)>150))&&((bc(2,mem)<250)&&(bc(2,mem)>200))&&(((bc(1,mem-1)>250)||(bc(1,mem-1)<150))||((bc(2,mem-1)>250)||(bc(2,mem-1)<200))))
           %Previous button logic
                        r(1) = 'g';
                        i = i - 1;
                       rectangle('Position',[150,200,100,50],'FaceColor',r(1))
                       a(1) = text(170,220,p1);
set(a(1), 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'b');                    
    if(i == 0)
       i = imax;
    end
    if(i == imax+1)
       i = 1;
    end
    actx.CurrentMedia = media(i);
pause(1);
actx.Controls.play
r(1) = 'r';
rectangle('Position',[150,200,100,50],'FaceColor',r(1))
                       a(1) = text(170,220,p1);
set(a(1), 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'b');
       end
       if (((bc(1,mem)<580)&&(bc(1,mem)>480))&&((bc(2,mem)<250)&&(bc(2,mem)>200))&&(((bc(1,mem-1)>580)||(bc(1,mem-1)<480))||((bc(2,mem-1)>250)||(bc(2,mem-1)<200))))
          %next button logic
           r(3) = 'g';
          i = i + 1;
          rectangle('Position',[480,200,100,50],'FaceColor',r(3))
          a(3) = text(500,220,p3);
set(a(3), 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'b');
    if(i == 0)
       i = imax;
    end
    if(i == imax+1)
       i = 1;
    end
    actx.CurrentMedia = media(i);
pause(1);
actx.Controls.play
r(3) = 'r';
rectangle('Position',[480,200,100,50],'FaceColor',r(3))
          a(3) = text(500,220,p3);
set(a(3), 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'b');
       end
       %Plot the point to show the location of cursor
      hold on
      plot(bc(1,mem),bc(2,mem), '-m+')
   
      if ((~(count == 1))&&(~(count == 0)))
          %Remove the previous Cursor plot
        plot(bc(1,mem-1),bc(2,mem-1), '-m+','Color','w')
      end
    end
end
   hold off
stop(vid);
flushdata(vid);
clear all

Tuesday, 14 January 2014

VIRTUAL MOUSE


                                                   Today I will be showing how to make a virtual mouse with two color caps one for mouse movement and other for mouse click. This will use java classes "java.awt.Robot" and "java.awt.event.*" for simulating mouse movements and mouse clicks. You can refer http://docs.oracle.com/javase/6/docs/api/java/awt/Robot.html for more details regarding these java classes.

As usual a demo video....




and the code...

%Accessing video frames..
vid = videoinput('winvideo', 1, 'YUY2_640x480');
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
%This sets the Frame rate to 15fps
vid.FrameGrabInterval = 2;
    start(vid)
    %Some memory variables to avoid unwanted click errors
    count = 0;
    bc(2,5) = 0;
    bc(1,5) = 0;
    brec(1,3) = 0;
    brec(1,:) = 0;
    click = 0;
    %Importing java classes to use functions like mousemove and mouse click
    import java.awt.Robot;
import java.awt.event.*;
mouse = Robot;
%Processing the frames to find red and blue objects
while(vid.FramesAcquired<=10000)
    data = getsnapshot(vid);
    %Rotating the frame left to right
    data(:,:,1) = fliplr(data(:,:,1));
    data(:,:,2) = fliplr(data(:,:,2));
    data(:,:,3) = fliplr(data(:,:,3));
    %diff_im is data regarding Red object and diff_imb regarding Blue
    diff_im = imsubtract(data(:,:,1), rgb2gray(data));
    diff_imb = imsubtract(data(:,:,3), rgb2gray(data));
    diff_im = medfilt2(diff_im, [3 3]);
    diff_imb = medfilt2(diff_imb, [3 3]);    
    diff_im = im2bw(diff_im,0.15);
    diff_imb = im2bw(diff_imb,0.05);
    diff_im = bwareaopen(diff_im,300);    
    diff_imb = bwareaopen(diff_imb,300);    
    bw = bwlabel(diff_im, 8);
    bwb = bwlabel(diff_imb, 8);
    stats = regionprops(bw, 'Centroid');
    statsb = regionprops(bwb, 'Centroid');
    %cycling the memory variables to update the memory
    if ~isempty(stats)
        count = count+1;
        for j = 2:5
      bc(1,j-1) = bc(1,j);
      bc(2,j-1) = bc(2,j);
        end
      bc(1,5) = stats(1).Centroid(1);
      bc(2,5) = stats(1).Centroid(2); 
      %Finding number of red and blue objects
    end
    ar = size(stats);
    ab = size(statsb);
        brec(1,2) = brec(1,3);
        brec(1,1) = brec(1,2);
    brec(1,3) = ab(1,1);
    %Performing a click only if object was avaible in the previous three
    %frames
    if ( (brec(1,3) == 1)&&((brec(1,2)==1)||(brec(1,1)==1)) )
        click = 2;
    elseif ab(1,1) == 1
        click = 1;
    else click = 0;
    end
    imshow(data)
    hold on
    %Showing no of frames
    at = text(490,460,strcat('Frame count:', num2str(vid.FramesAcquired)));
   set(at, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'black');
   %Displaying Number of objects
    ared = text(5,15, num2str(ar(1,1)));
    ablue = text(15,15,num2str(ab(1,1)));
   set(ared, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'red');
   set(ablue, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'blue');
    hold off
    %Finally using the mouse move and mouse click operations when needed
     if ((~(count == 0))&&(ar(1,1)~=0))
         mouse.mouseMove(round((1366/640)*(bc(1,5))),round((768/480)*(bc(2,5))))
     end
     if ((~(count == 0)))
         if click == 1
             mouse.mousePress(InputEvent.BUTTON1_MASK)
         elseif click ==0
             mouse.mouseRelease(InputEvent.BUTTON1_MASK)
         end
     end
end
stop(vid);
flushdata(vid);
close all;
clear all;
clc;

Monday, 13 January 2014

PAINT WITH YOUR FINGER ON YOUR PC SCREEN


                                       This simulation in matlab allows you to paint in a matlab figure with the help of of a blue colored cap attached to your finger. This will also also contain a small button which can be used as an eraser and as a brush whenever needed. For more details see here. So here's how to do it.....

Here is a demo video showing this...





And here's the matlab code...

%Accesing the camera and initializing the variables...
vid = videoinput('winvideo', 2, 'YUY2_640x480');
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 1;
    start(vid)
    count = 0;
    mem = 2;
    bc(2,mem) = 0;
    bc(1,mem) = 0;
r = 'g'; 
p = 'paint on';
k = getsnapshot(vid);
k(:,:,:) = 255;
imshow(k)
hold on
%Identifying the blue object
while(vid.FramesAcquired<=2000)
    data = getsnapshot(vid);
    data(:,:,1) = fliplr(data(:,:,1));
    data(:,:,2) = fliplr(data(:,:,2));
    data(:,:,3) = fliplr(data(:,:,3));
    %Showing the Button 
        rectangle('Position',[100,200,50,10],'FaceColor',r)
        text(105,205,p)
        %Color processing...
    diff_im = imsubtract(data(:,:,3), rgb2gray(data));
    diff_im = medfilt2(diff_im, [3 3]);
    diff_im = im2bw(diff_im,0.18);
    diff_im = bwareaopen(diff_im,300);    
    bw = bwlabel(diff_im, 8);
    stats = regionprops(bw, 'Centroid');
    if ~isempty(stats)
        count = count+1;   
           bc(1,1) = bc(1,mem);
           bc(2,1) = bc(2,mem);
      bc(1,mem) = stats(1).Centroid(1);
      bc(2,mem) = stats(1).Centroid(2);  
      %Button logic toggle if the previous cordinate is not inside the box
      %and present coordinate is inside the box
       if (((bc(1,mem)<150)&&(bc(1,mem)>100))&&((bc(2,mem)<210)&&(bc(2,mem)>200))&&(((bc(1,mem-1)>150)||(bc(1,mem-1)<100))||((bc(2,mem-1)>210)||(bc(2,mem-1)<200))))
          if (r == 'r')
              r = 'g';
              p = 'paint on';
          else
              r = 'r';
              p = 'paint off';
          end
       end
       %Plot the point to show the location of cursor
      plot(bc(1,mem),bc(2,mem), '-m+')
      if ((~(count == 1))&&(~(count == 0)))
          %Remove the previous Cursor plot
        plot(bc(1,mem-1),bc(2,mem-1), '-m+','Color','w')
      end
    end
    if ((~(count == 1))&&(~(count == 0))&&(r=='g'))
        %Drawing a line between the previous coordinate and next
        %coordinate.....
   line([bc(1,mem) bc(1,(mem-1))],[bc(2,mem) bc(2,(mem-1))],'Color','r','LineWidth',2);
    end
end
hold off
stop(vid);
flushdata(vid);
clear all

Sunday, 12 January 2014

IDENTIFYING A RED OBJECT IN MATLAB


                                            I first got inspired to do real-time image processing after looking at this. Here is a small program originally taken from above mentioned link and modified a little for the my own purpose to track a red object using a webcam. This uses Image Aquisition Toolbox and Computer Vision Tollbox to get the image and identify objects. You can also track Blue and Green objects easily by just changing a value. But you need to do a little extra work to identify other colors.

Here are some of the functions you might want to look in Matlab help file if you are getting started.

imaqhwinfo, videoinput, getsnapshot, fliplr, imsubtract, medfilt2, im2bw, bwareaopen, bwlabel, regionprops, imshow, stop, flushdata.


Red Object


  1. And here's the source


%Image aquisiton
%Creating video input object
obj = videoinput('winvideo',1,'YUY2_640x480');

% Setting the required properties
set(obj, 'FramesPerTrigger', Inf);
set(obj, 'ReturnedColorspace', 'rgb')
obj.FrameGrabInterval = 2;
%Starting the video aquisition
start(obj)
for k = 1:100
%Current frame
    image = getsnapshot(obj);
 
    %Rotating the image left to right since we always get a mirror image
    %by doing above mentioned tasks
    image(:,:,1) = fliplr(image(:,:,1));
    image(:,:,2) = fliplr(image(:,:,2));
    image(:,:,3) = fliplr(image(:,:,3));
 
    %Sbtracting the R values from grayscale image to get the Pixels in red
    %color
    %By using image(:,:,2) or 3 you can track Blue and Green Objects
    imgdiff = imsubtract(image(:,:,1), rgb2gray(image));
 
    %Noise filtering
    imgdiff = medfilt2(imgdiff, [3 3]);
 
    %Converting into binary image to do the required calculations like
    %Centroid of object with some threshold.
    imgdiff = im2bw(imgdiff,.25);
 
    %Removing smaller objects
    imgdiff = bwareaopen(imgdiff,200);
 
    %Label all the connected components in image
    bw = bwlabel(imgdiff, 8);
 
    %Getting the statistics data about the labelled regions
    data = regionprops(bw, 'BoundingBox', 'Centroid');
 
    %Show the image
    imshow(image)
 
    hold on
 
    %This is a loop to bound the red objects in a rectangular box.
    if(~isempty(stats))
        xl = length(stats);
    for object = 1:xl
        bb = stats(object).BoundingBox;
        bc = stats(object).Centroid;
        rectangle('Position',bb)
        plot(bc(1),bc(2), '-m+')
        a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), '    Y: ', num2str(round(bc(2)))));
        text(10,10,num2str(xl))
        set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
    end
 
    hold off
    end

end
% Both the loops end here.

% Stop the video aquisition.
stop(vid);

% Flush all the image data stored in the memory buffer.
flushdata(vid);

%Close all figures
close all

% Clear all variables
clear all

Thursday, 9 January 2014

A METHOD TO GET SCREEN SHOTS TO YOUR MAIL


                                              If you want to spy your own laptop while you are not using it, by getting screen shots then this would be really. If you want to do it only by Autoit, problem is that you can't upload some files using it. So the other method I found was to convert the image to text (Pixel values) and then mail the text. Then you can easily decode the text into image by using similar functions.

This is how the mail looked like..



I just combined the previous gmail application thing with the functions given here. So here's how to do it.

;Libraries.....
#include <WinAPI.au3>
#include <IE.au3>
#include <GUIConstants.au3>
#include <EditConstants.au3>
#include <GDIPlus.au3>
#include <Array.au3>
#include <File.au3>
#include <ScreenCapture.au3>
;Variables.....

Global $sUser = "manji3691@gmail.com", $sPwd = "", $sTo = "manji369@gmail.com", $sSubject = "screen capture", $sBody

Global $sFilePath = "C:\Manji\img.txt"

Opt("MustDeclareVars", 1)

 _ScreenCapture_Capture("C:\Manji\11.jpg")
_GDIPlus_Startup()
Dim $pixelarray
Local $file_in = "C:\Manji\11.jpg"
Local $file_out = "C:\Manji\1.jpg"
_FileImageToArray($file_in, $pixelarray)
_FileWriteFromArray($sFilePath, $pixelarray)
_GDIPlus_Shutdown()
Global $chars = FileRead($sFilePath, -1)
$sBody = $chars
;Navigating to Internet Explorer to login and send email..
;You can set the 3rd argument to 0 from 1 if you donot wish to see the mail being sent...
;Like opening of browser.. .
Global $oIE, $oForm, $oUser, $oPwd, $oChkbx, $oButton
$oIE = _IECreate("https://accounts.google.com/ServiceLogin?service=mail&?ui=html&zy=h")
;Waiting till it loads
        _IELoadWait($oIE)
;Getting the form fields
$oForm = _IEFormGetObjByName($oIE, "gaia_loginform")
$oUser = _IEFormElementGetObjByName($oForm, "Email")
        $oPwd = _IEFormElementGetObjByName($oForm, "Passwd")
$oChkbx = _IEFormElementGetObjByName($oForm, "PersistentCookie")
$oButton = _IEFormElementGetObjByName($oForm, "signIn")
;Setting the username, pwd ....
_IEFormElementSetValue($oUser, $sUser)
_IEFormElementSetValue($oPwd, $sPwd)
_IEAction($oChkbx, "click")
_IEAction($oButton, "click")
 _IELoadWait($oIE)
 ;Navigating to "compose mail"
 _IENavigate($oIE,"https://mail.google.com/mail/h/?&v=b&pv=tl&cs=b")
 _IELoadWait($oIE)
 Global $oForm1, $oTo, $oSubject, $oBody, $oSendButton
 ;Getting the fields to fill in for sending the mail
 $oForm1 = _IEFormGetObjByName($oIE, "f")
 $oTo = _IEFormElementGetObjByname($oForm1, "to")
 $oSubject = _IEFormElementGetObjByname($oForm1, "subject")
$oBody = _IEFormElementGetObjByname($oForm1, "body")
$oSendButton = _IEFormElementGetObjByname($oForm1, "nvp_bu_send")
;Entering the details of mail to be sent
 _IEFormElementSetValue($oTo, $sTo)
_IEFormElementSetValue($oSubject, $sSubject)
_IEFormElementSetValue($oBody, $sBody)
_IEAction($oSendButton, "click")
_IELoadWait($oIE)
;Quit IE
_IEQuit($oIE)


; code by Malkey: thanks man!

Func _FileImageToArray($filename, ByRef $aArray)
    Local $Reslt, $stride, $format, $Scan0, $iW, $iH, $hImage
    Local $v_Buffer, $width, $height
    Local $i, $j

    $hImage = _GDIPlus_ImageLoadFromFile($filename)

    $iW = _GDIPlus_ImageGetWidth($hImage)
    $iH = _GDIPlus_ImageGetHeight($hImage)
    $Reslt = _GDIPlus_BitmapLockBits($hImage, 0, 0, $iW, $iH, $GDIP_ILMREAD, $GDIP_PXF32ARGB)

    ;Get the returned values of _GDIPlus_BitmapLockBits ()

    $width = DllStructGetData($Reslt, "width")
    $height = DllStructGetData($Reslt, "height")
    $stride = DllStructGetData($Reslt, "stride")
    $format = DllStructGetData($Reslt, "format")
    $Scan0 = DllStructGetData($Reslt, "Scan0")

    Dim $aArray[$width][$height]

    For $i = 0 To $iW - 1
        For $j = 0 To $iH - 1
            $v_Buffer = DllStructCreate("dword", $Scan0 + ($j * $stride) + ($i * 4))
            $aArray[$i][$j] = Hex(DllStructGetData($v_Buffer, 1), 8)
        Next
    Next
    _GDIPlus_BitmapUnlockBits($hImage, $Reslt)
    _GDIPlus_ImageDispose($hImage)
    Return
EndFunc ;==>_FileImageToArray

; code by Malkey: thanks again ;)

Func _FileArrayToImage($filename, $aArray)
    Local $iW = UBound($aArray, 1), $iH = UBound($aArray, 2), $sResult = ""
    Local $hBMP, $hImage1, $Reslt, $width, $height, $stride, $format, $Scan0
    Local $sResult, $v_BufferA
    Local $i, $j
    
    $hBMP = _WinAPI_CreateBitmap($iW, $iH, 1, 32)
    $hImage1 = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)
    $Reslt = _GDIPlus_BitmapLockBits($hImage1, 0, 0, $iW, $iH, $GDIP_ILMWRITE, $GDIP_PXF32ARGB)

    ;Get the returned values of _GDIPlus_BitmapLockBits ()

    $width = DllStructGetData($Reslt, "width")
    $height = DllStructGetData($Reslt, "height")
    $stride = DllStructGetData($Reslt, "stride")
    $format = DllStructGetData($Reslt, "format")
    $Scan0 = DllStructGetData($Reslt, "Scan0")

    $v_BufferA = DllStructCreate("byte[" & $height * $width * 4 & "]", $Scan0)

    ;$AllPixels = DllStructGetData($v_BufferA, 1)

    For $j = 0 To $height - 1

        For $i = 0 To $width - 1
         $sResult &= StringRegExpReplace($aArray[$i][$j],"(..)(..)(..)(..)","\4\3\2\1")
        Next
    Next


    DllStructSetData($v_BufferA, 1, "0x" & StringStripWS($sResult, 8))


    _GDIPlus_BitmapUnlockBits($hImage1, $Reslt)

    _GDIPlus_ImageSaveToFile($hImage1, $filename)

    _GDIPlus_ImageDispose($hImage1)

    _WinAPI_DeleteObject($hBMP)
    Return
EndFunc ;==>_FileArrayToImage
;Quit program
Exit 

Also get the source here.

Wednesday, 8 January 2014

EASY WAY TO CONTROL YOUR PC WITH MOBLIE PHONE BLUETOOTH

                         
                                     Well, there are a lot of java applications available in market to do this. But nevertheless it would be very useful if you can easily program it according to your use. All you need is a java bluetooth fie exlorer application on your mobile phone like BT FILEMANAGER and Autoit on your PC. So the commands you will give to control should be named as a folder in your PC's Bluetooth remote folder and on autoit we keep checking for the directory to be created and act upon.

Here's a sample code...

#include <Sound.au3>
$file1 = 'F:\New folder\xyz.mp3'
$aSound = _SoundOpen($file1)
_soundplay($aSound)
$file = 'C:\Users\Yourbluetoothsharingfolder\'
MsgBox(0,"","running")
while(1)

If FileExists($file & "pause") Then
_SoundPause($aSound)
sleep(200)
DirRemove ($file & "pause")
EndIf
If FileExists($file & "play") Then
_SoundResume($aSound)
Sleep(200)
DirRemove ($file & "play")
EndIf
WEnd
Exit


Tuesday, 7 January 2014

CHAIN REACTION FOR WINDOWS

                                           
Edit: Its been years since I wrote this code. After taking a look at this code, I realize how dirty my programming used to be. Now, believing that I might have improved my programming skills, I tried to make it in java. You can find the code here.

                                              Hey!! Ever played a game called Chain Reaction on Android? Well its a wonderful mind game. When I played it for the first time I thought of implementing its logic on Autoit and here's the consequence... I just tried to implement the logic, so no animations and number of players two.
Chain Reaction
Following is the source code and a video of this in action...




Code follows....

;Chain Reaction game logic implementation for desktop
;libraries...
#include <GUIConstantsEx.au3>
#include <GuiButton.au3>
;Variables
Global $xc = 9, $yc = 7, $sleep = 200
Global $btn[63], $iMemo, $val[63], $col[63], $click = 1, $color, $red = 0, $green = 0
;initializing variables..
For $k = 0 To 62
   $col[$k] = 0
Next
;Creating gui
GUICreate("Buttons", 350, 350) 
$iMemo = GUICtrlCreateEdit("", 230, 10, 100, 150)
   $n = -1
For $yy = 0 To $yc-1
   Local $y = 10
    For $x = 0 To $xc-1
  $n = $n + 1
        $btn[$n] = GUICtrlCreateButton( $val[$n], 10+($yy*30), $y, 30, 30)
        $y += 30
Next
  Next
  $kk = 0
  $kk1 = -1
GUISetState()
;Logic beigins here
While(1)
;Reading input via click
$kl =  GUIGetMsg()
If $kl>2 Then
  $kk = $kl-3
  $kk1 = $kk - 1
  ;Setting number of possible dots per each box as 3 by default and then they change when required
  $poss = 3
  If ((mod($kk1,9)==0)Or(mod($kk1,9)==8)Or($kk1-mod($kk1,9)==0)Or(($kk1-mod($kk1,9))/9==$yc-1)) Then $poss = 2
  If (($kk1==0)Or($kk1==8)Or($kk1==54)Or($kk1==62)) Then $poss = 1
  GUICtrlSetData($iMemo, $kk)
  ;If there is a click on a box and if the number of dots in that block less than or equal
  ;to the max possible dots then incrementing the value..
  If $val[$kk1] < $poss Then
  $val[$kk1] = $val[$kk1]+1
  ;
  ;setting the color variable
  Switch $click
Case 1
                $click = 2
$col[$kk1] = $click
$color = 0xff0000
Case 2
                $click = 1
$col[$kk1] = $click
$color = 0x00ff00
        EndSwitch
;Enabling and disabling buttons
;Disabling the buttons filled by previous color and enabling the buttons to be filled by current color
For $k = 0 To 62
  If (($col[$k]<>$click)And($col[$k]<>0)) Then 
 GUICtrlSetState($btn[$k],$GUI_ENABLE)
  EndIf
  If (($col[$k]==$click)) Then 
 GUICtrlSetState($btn[$k],$GUI_DISABLE)
 EndIf
  Next
  ;
  ;setting the data and color
  GUICtrlSetData($btn[$kk1],$val[$kk1])
  GUICtrlSetBkColor($btn[$kk1],$color)
  ;If chain reaction triggers...
Else 
  ;Calculating and showing the reaction
  Global $jk = 1
  GUICtrlSetData($iMemo, "wait")
  switch $click
Case 1
                $click = 2
$col[$kk1] = $click
$color = 0xff0000
Case 2
                $click = 1
$col[$kk1] = $click
$color = 0x00ff00
        EndSwitch
;Calling a function to update the values after reaction
  $val = update($val,$kk1,0)
  While(1)
 Local $jk = 1
 ;Check if any of the element has exceeded and update if exceeded
  For $k = 0 To 62
 $yon = checkifexceed($val[$k],$k)
 If $yon == 0 Then
$val = update($val,$k,1)
$jk = 0
EndIf
 Next
 If $jk == 1 Then ExitLoop
  WEnd
  ;Enabling and disabling the corresponding boxes
For $k = 0 To 62
  If (($col[$k]<>$click)And($col[$k]<>0)) Then 
 GUICtrlSetState($btn[$k],$GUI_ENABLE)
  EndIf
  If (($col[$k]==$click)) Then 
 GUICtrlSetState($btn[$k],$GUI_DISABLE)
 EndIf
  Next
  $red = 0
$green = 0
For $cl = 0 To 62
  If $col[$cl] == 1 Then
 $red = 1
  ElseIf $col[$cl] == 2 Then
 $green = 1
  EndIf
Next
  If $red == 0 And $green == 1 Then 
 MsgBox(0,"Game Over","Green Wins")
 Exit
  EndIf
 If $green == 0 And $red == 1 Then 
MsgBox(0,"Game Over","Red wins")
Exit
 EndIf
EndIf
EndIf
        Switch $kl
            Case $GUI_EVENT_CLOSE
                ExitLoop
        EndSwitch
GUICtrlSetData($iMemo, "play")
    WEnd
;Functions
;To update all the neighbouring boxes and current box when reaction occurs
Func update($val1,$kk1,$w)
  ;Sound indication that a reaction has occured
  Beep(1000,100)
  Local $nei[4] = [-1,-1,-1,-1]
  $nei = neigh($kk1)
  For $k = 0 To 3
 If $nei[$k]<>-1 Then
 $val1[$nei[$k]] += 1
 GUICtrlSetData($btn[$nei[$k]],$val1[$nei[$k]])
 GUICtrlSetBkColor($btn[$nei[$k]],$color)
 $col[$nei[$k]] = $click
 EndIf
  Next
  $poss = possval($kk1)
  If $w ==1 Then $poss += 1
    $val1[$kk1] -= $poss
GUICtrlSetData($btn[$kk1],$val1[$kk1])
GUICtrlSetBkColor($btn[$kk1],$color)
$col[$kk1] = $click
 If $val1[$kk1] == 0 Then
  GUICtrlSetBkColor($btn[$kk1],0xffffff)
  GUICtrlSetState($btn[$kk1],$GUI_ENABLE)
$col[$kk1] = 0
  EndIf
sleep($sleep)
Return $val1
 EndFunc 
 ;Function to find neighbours of a given box
 Func neigh($kk1)
Local $nei[4] = [-1,-1,-1,-1],$k = 0
  If Mod($kk1,9)<>0 Then
 $nei[$k] = $kk1-1
 $k += 1
 EndIf
  If Mod($kk1,9)<>$xc-1 Then
 $nei[$k] = $kk1+1
 $k += 1
 EndIf
  If ($kk1-Mod($kk1,9))<>0 Then
 $nei[$k] = $kk1-9
 $k += 1
 EndIf
  If ($kk1-mod($kk1,9))/9<>$yc-1 Then
 $nei[$k] = $kk1+9
 $k += 1
 EndIf
    
Return $nei
 EndFunc
 ;function to check if the value in a box has exceeded the limit
 Func checkifexceed($val,$kk1)
  local $yon = 1,$poss = 3
  If ((mod($kk1,9)==0)Or(mod($kk1,9)==8)Or($kk1-mod($kk1,9)==0)Or(($kk1-mod($kk1,9))/9==$yc-1)) Then $poss = 2
  If (($kk1==0)Or($kk1==8)Or($kk1==54)Or($kk1==62)) Then $poss = 1
 If $val > $poss Then $yon = 0
Return $yon
 EndFunc
 ;Max possible value in a box
Func possval($kk1)
  local $poss = 3
  If ((mod($kk1,9)==0)Or(mod($kk1,9)==8)Or($kk1-mod($kk1,9)==0)Or(($kk1-mod($kk1,9))/9==$yc-1)) Then $poss = 2
  If (($kk1==0)Or($kk1==8)Or($kk1==54)Or($kk1==62)) Then $poss = 1
Return $poss
 EndFunc 


You can also download it from here.

Monday, 6 January 2014

SIMPLE DESKTOP GMAIL APPLICATION

                   
                                        This is a simple app to send mail via gmail right from your desktop. Just you can enter your Username, Password, To, Subject, Body and send mail by just one click. So here we go!!

A Video demonstrating it.........



And here's the code....

;Libraries.....
#include <WinAPI.au3>
#include <IE.au3>
#include <GUIConstants.au3>
#include <EditConstants.au3>
;Variables.....
Global $sUser, $sPwd, $sTo, $sSubject, $sBody

;Creating GUI for inputs...

$Window = GUICreate("Gmail", 500, 400, 200, 100)
$Label1 = GUICtrlCreateLabel("Email ID:", 8, 8, 100, 17)
$Input1 = GUICtrlCreateInput("@gmail.com", 175, 8, 217, 31)
 GUICtrlSetState(-1, $GUI_DROPACCEPTED)
$Label2 = GUICtrlCreateLabel("Password", 8, 58, 1000, 17)
$Input2= GUICtrlCreateInput("", 175, 58, 217, 31, $ES_PASSWORD)
 GUICtrlSetState(-1, $GUI_DROPACCEPTED)
$Label3 = GUICtrlCreateLabel("To", 8, 108, 1000, 17)
$Input3 = GUICtrlCreateInput("", 175, 108, 217, 31)
$Label4 = GUICtrlCreateLabel("Subject", 8, 158, 1000, 17)
$Input4 = GUICtrlCreateInput("", 175, 158, 217, 31)
$Label5 = GUICtrlCreateLabel("Body", 8, 208, 1000, 17)
$Input5 = GUICtrlCreateInput("", 175, 208, 217, 151)
$Go = GUICtrlCreateButton("Send", 395, 8, 89, 25, 0)
GUISetState(@SW_SHOW)

;Reading Inputs....

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
         Case $Go
$sUser = GUICtrlRead($Input1)            
$sPwd = GUICtrlRead($Input2)    
$sTo = GUICtrlRead($Input3)    
$sSubject = GUICtrlRead($Input4)    
$sBody = GUICtrlRead($Input5)    
    ExitLoop
    EndSwitch
    WEnd

;Navigating to Internet Explorer to login and send email..
;You can set the 3rd argument to 0 from 1 if you donot wish to see the mail being sent...
;Like opening of browser.. .
$oIE = _IECreate("https://accounts.google.com/ServiceLogin?service=mail&?ui=html&zy=h",0,1)
;Waiting till it loads
        _IELoadWait($oIE)
;Getting the form fields
$oForm = _IEFormGetObjByName($oIE, "gaia_loginform")
$oUser = _IEFormElementGetObjByName($oForm, "Email")
        $oPwd = _IEFormElementGetObjByName($oForm, "Passwd")
$oChkbx = _IEFormElementGetObjByName($oForm, "PersistentCookie")
$oButton = _IEFormElementGetObjByName($oForm, "signIn")
;Setting the username, pwd ....
_IEFormElementSetValue($oUser, $sUser)
_IEFormElementSetValue($oPwd, $sPwd)
_IEAction($oChkbx, "click")
_IEAction($oButton, "click")
 _IELoadWait($oIE)
 ;Navigating to "compose mail"
 _IENavigate($oIE,"https://mail.google.com/mail/h/?&v=b&pv=tl&cs=b")
 _IELoadWait($oIE)
 ;Getting the fields to fill in for sending the mail
 $oForm1 = _IEFormGetObjByName($oIE, "f")
 $oTo = _IEFormElementGetObjByname($oForm1, "to")
 $oSubject = _IEFormElementGetObjByname($oForm1, "subject")
$oBody = _IEFormElementGetObjByname($oForm1, "body")
$oSendButton = _IEFormElementGetObjByname($oForm1, "nvp_bu_send")
;Entering the details of mail to be sent
 _IEFormElementSetValue($oTo, $sTo)
_IEFormElementSetValue($oSubject, $sSubject)
_IEFormElementSetValue($oBody, $sBody)
_IEAction($oSendButton, "click")
_IELoadWait($oIE)
;Quit IE
_IEQuit($oIE)
;Quit program
Exit 

You can also download the code from here
Powered by Blogger.

Blog Archive

 

© 2013 The Repository. All rights resevered. Designed by Templateism

Back To Top