PDA

View Full Version : Update Live Image from Camera



benz6699
10th July 2014, 06:37
I get one live image from camera each time I call the function "vc3d_GetImage(0,iTotalImage,true,&iRemainImg, pB)". After that I will call the function "loadImage(unsigned char *pB)" to display the image. However the image is not updated all the while. Only the last image is showed at the end. For example I call the function "vc3d_GetImage(0,iTotalImage,true,&iRemainImg, pB)" 5 times. The display is not updated 5 times. The display is only updated once (the fifth image) at the end. What has I done wrong?


void MainWindow::on_pBCapture_clicked()
{
ui->lImageCount->setText("0");
int iTotalImage = ui->sBTotalImage->value();

int iImgWidth, iImgHeight;
int count = 0;
unsigned char *pBuffer=NULL;
unsigned char *pBuffer2=NULL;

/* to get the width and height of image */
int iStatus = vc3d_GetImageSizeVr(0, &iImgWidth, &iImgHeight);

if (iStatus>0)
{
int iRemainImg = iTotalImage;
pBuffer = new unsigned char[iImgWidth*iImgHeight];
pBuffer2 = new unsigned char[iImgWidth*iImgHeight];
// Get one image (or more when in a loop)
do
{
unsigned char *pB;
if (iRemainImg%2)
{
pB=pBuffer;
}
else
{
pB=pBuffer2;
}
/* Get the one image when this function is called */
iStatus = vc3d_GetImage(0,iTotalImage,true,&iRemainImg, pB);
if (iStatus == 1)
{
count++;
ui->lImageCount->setText(QString::number(count));
loadImage(pB);
}
//delete [] pBuffer;
}while (iRemainImg>0 && iStatus>0);
}
}

void MainWindow::loadImage(unsigned char *pB)
{
QGraphicsScene* scene = new QGraphicsScene(this);
ui->graphicsView->setScene(scene);
QImage image = QImage(pB, 1280, 1024, QImage::Format_Indexed8);
scene->clear();
QGraphicsPixmapItem* pi = scene->addPixmap(QPixmap::fromImage(image));
ui->graphicsView->fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
ui->graphicsView->update();
ui->graphicsView->viewport()->update();
ui->graphicsView->show();
}

stampede
10th July 2014, 09:18
Updating a display widget in a loop like that won't give it enough time to redraw. Create a slot to capture and update single image and then use a timer with reasonable timeout to capture the image.
Btw. creating new graphics scene just to display new image is not necessary, you can add first image with scene->addPixmap and then reuse the QGraphicsPixmapItem object by updating displayed pixmap with QGraphicsPixmapItem::setPixmap() method.