Hi.
I am having trouble with GetFrameImage and looked around all the similar topics without finding the case.
Could anyone help me please.
Here is the code.
---------------------------------------------------------------
INPCameraFrame *frame_;
[color:#33FF33]//Glab a frame.[/color]
if( S_OK == camera->GetFrame(0,&frame_) ){
[color:#33FF33]//Check if the frame is fine.[/color]
VARIANT_BOOL empty_,greyscale_,corrupt_;
frame_->get_IsEmpty(&empty_);
frame_->get_IsGreyscale(&greyscale_);
frame_->get_IsCorrupt(&corrupt_);
if( corrupt_ == VARIANT_FALSE &&
empty_ == VARIANT_FALSE &&
greyscale_ == VARIANT_TRUE && buf){
[color:#FF0000] //These codes are fine.So it's not a mem allocation failure.
[/color]printf( "%d %d %x\n", cam_w, cam_h, buf );
BYTE *p_=(BYTE*)buf;
for( int i_=0; i_GetFrameImage(frame_,cam_w,cam_h,0,buf );
---------------------------------------------------------------
As you see, I do not use MFC since I need no GUI.
I wonder if it's a problem.
Thank you.
GetFrameImage Crashes with access violation exception.
Re: GetFrameImage Crashes with access violation exception.
This looks like a code fragment since there is no camera enumeration code present. Please send the complete code to support@naturalpoint.com with a reference to this post included.
Re: GetFrameImage Crashes with access violation exception.
Ok, there are some simple rules you need to follow when using GetFrameImage() because it's potentially dangerous since you're telling OptiTrack where and how big your pixel buffer is that it's going to fill.
when call it make sure you have the following correct:
GetFrameImage(
pFrame -- pointer to the frame of interest. Make sure this is non-NULL.
PixelWidth -- pixel width of the buffer you're going to fill. Typically this would match the camera's width, but it doesn't have to. What it does need to match is the width of the memory you've allocated for the image.
PixelHeight -- pixel height of the buffer you're going to fill. Typically this would match the camera's height, but it doesn't have to. What it does need to match is the height of the memory you've allocated for the image.
ByteSpan -- number of bytes between the start of the one row to the next of the frame buffer you've allocated. Typically this is PixelWidth*BytesPerPixel, but you may have some other requirements if you're ultimately pushing this to a texture for example. So this parameter allows the span of lines to be different than the width.
BitsPerPixel -- Grayscale would be 8, RGB565 would be 16, RGB would be 24, RGBA would be 32. All are supported and take up 1,2,3, and 4 BytesPerPixel respectively.
Buffer -- A pointer to where you want the OptiTrack Library to raster the image. This buffer would have been previously allocated by the application and would typically be of size ByteSpan*PixelHeight. If this buffer isn't allocated, you pass a bad pointer, or you've told OptiTrack it's larger than the space you've allocated. It's going to crash with an access violation.
when call it make sure you have the following correct:
GetFrameImage(
pFrame -- pointer to the frame of interest. Make sure this is non-NULL.
PixelWidth -- pixel width of the buffer you're going to fill. Typically this would match the camera's width, but it doesn't have to. What it does need to match is the width of the memory you've allocated for the image.
PixelHeight -- pixel height of the buffer you're going to fill. Typically this would match the camera's height, but it doesn't have to. What it does need to match is the height of the memory you've allocated for the image.
ByteSpan -- number of bytes between the start of the one row to the next of the frame buffer you've allocated. Typically this is PixelWidth*BytesPerPixel, but you may have some other requirements if you're ultimately pushing this to a texture for example. So this parameter allows the span of lines to be different than the width.
BitsPerPixel -- Grayscale would be 8, RGB565 would be 16, RGB would be 24, RGBA would be 32. All are supported and take up 1,2,3, and 4 BytesPerPixel respectively.
Buffer -- A pointer to where you want the OptiTrack Library to raster the image. This buffer would have been previously allocated by the application and would typically be of size ByteSpan*PixelHeight. If this buffer isn't allocated, you pass a bad pointer, or you've told OptiTrack it's larger than the space you've allocated. It's going to crash with an access violation.
Re: GetFrameImage Crashes with access violation exception.
I need to apologize since the crash was actually a threading issue.My misunderstanding of COM model led me to the crash.
It had nothing to do with the SDK.
Thank all of you anyway.
I really appreciate.
It had nothing to do with the SDK.
Thank all of you anyway.
I really appreciate.
-
- Posts: 8
- Joined: Mon Feb 25, 2008 11:22 am
Re: GetFrameImage Crashes with access violation exception.
Hello.
I have no problems in using GetFrameImage() in C++. But now, I am migrating my app to C# and I always get the access violation error. I must be doing something wrong but I don't know what.
Based on your example provided:
Bitmap raw = new Bitmap(def.CAMERA_WIDTH, def.CAMERA_HEIGHT, PixelFormat.Format8bppIndexed);
BitmapData bmData = raw.LockBits(new Rectangle(0, 0, raw.Width, raw.Height),
ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
int stride = bmData.Stride;
System.IntPtr bufferPtr = bmData.Scan0;
unsafe
{
byte* buffer = (byte*)(void*)bufferPtr;
camera[0].GetFrameImage(frame[0], bmData.Width, bmData.Height, bmData.Stride, 8, ref buffer[0]);
}
raw.UnlockBits(bmData);
There is a valid frame[0] and camera[0]...
This gives me an appliation error:
... The memory could no be "written"...
The problem was solved:
Using C# I must only GetFrame() when the Event of FrameAvailable is triggered.
I have no problems in using GetFrameImage() in C++. But now, I am migrating my app to C# and I always get the access violation error. I must be doing something wrong but I don't know what.
Based on your example provided:
Bitmap raw = new Bitmap(def.CAMERA_WIDTH, def.CAMERA_HEIGHT, PixelFormat.Format8bppIndexed);
BitmapData bmData = raw.LockBits(new Rectangle(0, 0, raw.Width, raw.Height),
ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
int stride = bmData.Stride;
System.IntPtr bufferPtr = bmData.Scan0;
unsafe
{
byte* buffer = (byte*)(void*)bufferPtr;
camera[0].GetFrameImage(frame[0], bmData.Width, bmData.Height, bmData.Stride, 8, ref buffer[0]);
}
raw.UnlockBits(bmData);
There is a valid frame[0] and camera[0]...
This gives me an appliation error:
... The memory could no be "written"...
The problem was solved:
Using C# I must only GetFrame() when the Event of FrameAvailable is triggered.