Problem to grab and save image in oF - black image saved

I’m using a very simple code for save image from webcam. If i try to save a jpg, i see a black image. The code it’s really simple and i don’t know if i’m doing something wrong or there is some bug somewhere.
This is my code:

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup()
{
  camWidth          = 320;	// try to grab at this size.
  camHeight 		= 240;
  vidGrabber.setDeviceID(0);
  vidGrabber.setDesiredFrameRate(60);
  vidGrabber.initGrabber(camWidth,camHeight);
  finalImage.allocate(camWidth, camHeight, OF_IMAGE_COLOR);
  saveImage = false;
}

void ofApp::update()
{
  vidGrabber.update();
  if (vidGrabber.isFrameNew())
  {
    finalImage.getTextureReference().loadData(vidGrabber.getPixels(), camWidth, camHeight, GL_RGB);
    if(saveImage)
    {
      saveImage = false;
      finalImage.saveImage("images/test.jpg");
    }
  }
}

void ofApp::draw(){
  ofSetColor(255);
  vidGrabber.draw(0,0);
  finalImage.draw(camWidth,0);
}

void ofApp::keyPressed  (int key)
{
  if (key == 's')
    saveImage = true;
}

you are uploading the pixels from the video to the internal texture of the image but what the image uses to save is it’s pixels not the texture. something like:

if (vidGrabber.isFrameNew())
  {
    if(saveImage)
    {
      finalImage.getPixelsRef() = vidGrabber.getPixelsRef();
      saveImage = false;
      finalImage.saveImage("images/test.jpg");
    }
  }

should work

1 Like

Thanks Arturo, now i understood my error.