int i, j,k,l; // Bcl
int R, G, B ,color; // Recupère des infos
long activPixels; //noombre de pixel considéré comme différents
HDC hDC_tmp ; // Pour récupérer les pixels
HDC hdc, memdc; // DC
void *pBits ; // Tbl de pixel
char theTime[TIME_LENGTH]; // Tbl de pixel
HBITMAP hbmp ; // Image
HGDIOBJ hret ; // Pour récupérer l'image
hdc = Form1->HDCVideo ; // Va avec le hWnd
LPBYTE lpBits ;
while(true)
{
lpBits = new BYTE[4*WIDTH*HEIGHT];
activPixels = 0;
// C'est marqué
memdc = CreateCompatibleDC(hdc);
if(!(memdc))
{
return ;
}
// Création du bitmap
hbmp = CreateDIBSection(hdc, &(Form1->bmpinfo), DIB_PAL_COLORS, &pBits, NULL, 0) ;
// En cas d'erreur
if(!hbmp)
{ // Fait le ménage
DeleteObject(hbmp) ;
DeleteDC(memdc) ;
free(pBits) ;
return ;
}
// Sélectionne le mendc avec l'image
hret = SelectObject(memdc, hbmp) ;
if(!hret || (hret == HGDI_ERROR))
{
// Fait le ménage
DeleteObject(hbmp) ;
DeleteDC(memdc) ;
free(pBits) ;
return ;
}
// Colle l'image
if(!BitBlt(memdc, 0, 0, WIDTH, HEIGHT, hdc, 0, 0, SRCCOPY))
{
// Fait le ménage
DeleteObject(hbmp) ;
DeleteDC(memdc) ;
free(pBits) ;
Form1->Caption = "ERROR 3";
Application->Restore();
return ;
}
// Récupère le DC
hDC_tmp = GetDC(NULL) ;
// Récupère les pixels
GetDIBits(hDC_tmp, hbmp, 0, HEIGHT, lpBits, &(Form1->bmpinfo), DIB_RGB_COLORS) ;
ReleaseDC(NULL, hDC_tmp) ;
Form1->streamBitmap->Canvas->Lock();
Form1->motionBitmap->Canvas->Lock();
// Parcoure les pixels et les stocke dans un tbl
for(i=0; i<HEIGHT; i++)
{
k=4*WIDTH*(HEIGHT-1-i);
for(j=0; j<WIDTH; j++)
{
// Récupère chaque coef du RGB
l=k+4*j ;
// Calcul la couleur
R = lpBits[l+2];
G = lpBits[l+1];
B = lpBits[l] ;
color = (R + 256* G + 65536 * B);
Form1->streamBitmap->Canvas->Pixels[j][i]=(TColor)color;
G=(R+G+B)/3;
color = abs(G - lastGrey[j][i]);
if(color>10)
{
activPixels++;
}
lastGrey[j][i] = G;
B= 65792;
B = (Form1->detected)?B/256:B;
B = (Form1->protectionActived)?B/256:B;
Form1->motionBitmap->Canvas->Pixels[j][i]=(TColor)color*B;
}
}
Form1->MotionPicture->Canvas->Lock();
Form1->MotionPicture->Canvas->Draw(0,0,Form1->motionBitmap);
Form1->motionBitmap->Canvas->Unlock();
Form1->MotionPicture->Canvas->Unlock();
//création de la date et de l'heure pour l'affichage
createStringTime(theTime);
Form1->streamBitmap->Canvas->Font->Color = Form1->ColorBoxTime->Selected;
Form1->streamBitmap->Canvas->Brush->Color = Form1->ColorBoxTimeBG->Selected;
switch(Form1->ComboBoxTime->ItemIndex)
{
case 0:
case 2: i = 118;
break;
case 1:
case 3: i = 132;
break;
case 4:
case 6: i = 120;
break;
case 5:
case 7: i = 134;
break;
case 8:
case 10: i = 106;
break;
case 9:
case 11: i = 120;
break;
default: break;
}
Form1->streamBitmap->Canvas->TextOutA(WIDTH - i ,HEIGHT - 15,theTime);
//détection
motionDetection(activPixels *100/( WIDTH * HEIGHT));
if(Form1->detected)
{
Form1->streamBitmap->Canvas->Brush->Color = ( Form1->protectionActived )?clRed:clBlack;
Form1->streamBitmap->Canvas->Font->Color = ( Form1->protectionActived )?clBlack:clYellow;
Form1->streamBitmap->Canvas->TextOutA(2,2,(( Form1->protectionActived )?" Alert intrusion! ":" Motion Detected "));
}
//affichage de l'image de streaming
Form1->StreamPicture->Canvas->Lock();
Form1->StreamPicture->Canvas->Draw(0,0,Form1->streamBitmap);
Form1->streamBitmap->Canvas->Unlock();
Form1->StreamPicture->Canvas->Unlock();
// Fait le ménage
DeleteObject(hbmp) ;
DeleteDC(memdc) ;
free(lpBits) ;
this->Suspend();
}
Partager