OpenGL -显示摄像头抓取图片

// ConsoleApplication2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "FlyCapture2.h"
#include <iostream>
#include "glew.h"
#include "wglew.h"
#include "glut.h"
#include "fstream"
using namespace FlyCapture2;
using namespace std;
void PrintBuildInfo() {
FC2Version fc2Version;
Utilities::GetLibraryVersion(&fc2Version);

cout << "FlyCapture2 library version: " << fc2Version.major << "." << fc2Version.minor << "." << fc2Version.type << "." << fc2Version.build;

cout << "Application build date: " << __DATE__ << " " << __TIME__ << endl;
}

void PrintCameraInfo(CameraInfo* pCamInfo) {
cout << endl;
cout << "*** CAMERA INFORMATION ***" << endl;
cout << "Serial number -" << pCamInfo->serialNumber << endl;
cout << "Camera model - " << pCamInfo->modelName << endl;
cout << "Camera vendor - " << pCamInfo->vendorName << endl;
cout << "Sensor - " << pCamInfo->sensorInfo << endl;
cout << "Resolution - " << pCamInfo->sensorResolution << endl;
cout << "Firmware version - " << pCamInfo->firmwareVersion << endl;
cout << "Firmware build time - " << pCamInfo->firmwareBuildTime << endl << endl;


}

void PrintFormat7Capabilities(Format7Info fmt7Info) {
cout << "Max image pixels: (" << fmt7Info.maxWidth << ", " << fmt7Info.maxHeight << ")" << endl;
cout << "Image Unit size: (" << fmt7Info.imageHStepSize << ", " << fmt7Info.imageVStepSize << ")" << endl;
cout << "Offset Unit size: (" << fmt7Info.offsetHStepSize << ", " << fmt7Info.offsetVStepSize << ")" << endl;
cout << "Pixel format bitfield: 0x" << fmt7Info.pixelFormatBitField << endl;

}

void PrintError(Error error) {
error.PrintErrorTrace();
}

unsigned char *imgBuf = NULL;
Camera cam_;
Error error;
Image rawImage;
GLuint tex;
void makeCheckImages(void)
{
cam_.RetrieveBuffer(&rawImage);
imgBuf = rawImage.GetData();
}

void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);

makeCheckImages();
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);

glEnable(GL_TEXTURE_2D);
}

void display(void)
{
makeCheckImages();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1920,
1080, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE,
imgBuf);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(-1.0, -1.0);
glTexCoord2f(1, 0);
glVertex2f(+1.0, -1.0);
glTexCoord2f(1, 1);
glVertex2f(+1.0, +1.0);
glTexCoord2f(0, 1);
glVertex2f(-1.0, +1.0);
glEnd();

glFlush();
}

void reshape(int w, int h)
{
/*glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 1.0, 30.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -3.6);*/
}

 

int main(int argc, char** argv)
{
if (!cam_.IsConnected()) {
PrintBuildInfo();
const Mode k_fmt7Mode = MODE_0;
const PixelFormat k_fmt7PixFmt = PIXEL_FORMAT_MONO8;

BusManager busMgr;
unsigned int numCameras;
error = busMgr.GetNumOfCameras(&numCameras);
if (error != PGRERROR_OK) {
PrintError(error);
return -1;
}

cout << "Number of cameras detected: " << numCameras << endl;

if (numCameras < 1) {
cout << "Insufficient number of cameras... exiting" << endl;
return -1;
}

PGRGuid guid;
error = busMgr.GetCameraFromIndex(0, &guid);
if (error != PGRERROR_OK) {
PrintError(error);
return -1;
}

// Connect to a camera
error = cam_.Connect(&guid);
if (error != PGRERROR_OK) {
PrintError(error);
return -1;
}

// Get the camera information
CameraInfo camInfo;
error = cam_.GetCameraInfo(&camInfo);
if (error != PGRERROR_OK) {
PrintError(error);
return -1;
}
PrintCameraInfo(&camInfo);

// Query for available Format 7 modes
Format7Info fmt7Info;
bool supported;
fmt7Info.mode = k_fmt7Mode;
error = cam_.GetFormat7Info(&fmt7Info, &supported);
if (error != PGRERROR_OK) {
PrintError(error);
return -1;
}

PrintFormat7Capabilities(fmt7Info);
if ((k_fmt7PixFmt & fmt7Info.pixelFormatBitField) == 0) {
// Pixel format not supported!
cout << "Pixel format is not supported" << endl;
return -1;
}

Format7ImageSettings fmt7ImageSettings;
fmt7ImageSettings.mode = k_fmt7Mode;
fmt7ImageSettings.offsetX = 0;
fmt7ImageSettings.offsetY = 0;
fmt7ImageSettings.width = 1920;
fmt7ImageSettings.height = 1080;
fmt7ImageSettings.pixelFormat = k_fmt7PixFmt;

bool valid;
Format7PacketInfo fmt7PacketInfo;

// Validate the settings to make sure that they are valid
error = cam_.ValidateFormat7Settings(
&fmt7ImageSettings,
&valid,
&fmt7PacketInfo);
if (error != PGRERROR_OK) {
PrintError(error);
return -1;
}

if (!valid) {
// Settings are not valid
cout << "Format7 settings are not valid" << endl;
return -1;
}
// Set the settings to the camera
error = cam_.SetFormat7Configuration(
&fmt7ImageSettings,
fmt7PacketInfo.recommendedBytesPerPacket);
if (error != PGRERROR_OK) {
PrintError(error);
return -1;
}
Property frmRate;
frmRate.type = FRAME_RATE;
frmRate.autoManualMode = false;
frmRate.absControl = true;
frmRate.absValue = 100;
frmRate.onOff = true;

error = cam_.SetProperty(&frmRate);
if (error != PGRERROR_OK) {
PrintError(error);
return -1;
}
// Start capturing images
error = cam_.StartCapture();
if (error != PGRERROR_OK) {
PrintError(error);
return -1;
}

}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1920, 1080);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
return 0;
}

posted @ 2016-05-30 15:33  bshaozi  阅读(975)  评论(0编辑  收藏  举报