CVImageBufferRef cvimgRef = CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBuffer cvimgRef = (CVPixelBuffer) sampleBuffer.getImageBuffer();
public void didOutputSampleBuffer(AVCaptureOutput captureOutput,
CMSampleBuffer sampleBuffer, AVCaptureConnection connection) {}
CVPixelBuffer cvimgRef = imageBuffer.as(CVPixelBuffer.class);
or CVPixelBuffer cvimgRef = imageBuffer.as(CVPixelBufferPtr.class).get();
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
CVImageBufferRef
cvimgRef
= CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(
cvimgRef
, 0);
size_t _width = CVPixelBufferGetWidth(
cvimgRef
);
size_t _height = CVPixelBufferGetHeight(
cvimgRef
);
....
}
--
You received this message because you are subscribed to the Google Groups "RoboVM" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robovm+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
+(CVPixelBufferRef) getPixelBuffer:(CMSampleBufferRef) value{
CVPixelBufferRef cvimgRef = CMSampleBufferGetImageBuffer(value);
return cvimgRef;
}
@Method(selector = "getPixelBuffer:")
public static native CVPixelBuffer getPixelBuffer(CMSampleBuffer value);
// process the frame of video
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
// if we're paused don't do anything
if(self.currentState==STATE_PAUSED) {
// reset our frame counter
self.validFrameCounter=0;
return;
}
// this is the image buffer
CVImageBufferRef cvimgRef = CMSampleBufferGetImageBuffer(sampleBuffer);
//CVPixelBufferRef cvimgRef = CMSampleBufferGetImageBuffer(sampleBuffer);
// Lock the image buffer
CVPixelBufferLockBaseAddress(cvimgRef,0);
// access the data
size_t width=CVPixelBufferGetWidth(cvimgRef);
size_t height=CVPixelBufferGetHeight(cvimgRef);
// get the raw image bytes
uint8_t *buf=(uint8_t *) CVPixelBufferGetBaseAddress(cvimgRef);
size_t bprow=CVPixelBufferGetBytesPerRow(cvimgRef);
// and pull out the average rgb value of the frame
float r=0,g=0,b=0;
for(int y=0; y<height; y++) {
for(int x=0; x<width*4; x+=4) {
b+=buf[x];
g+=buf[x+1];
r+=buf[x+2];
}
buf+=bprow;
}
r/=255*(float) (width*height);
g/=255*(float) (width*height);
b/=255*(float) (width*height);
//other code
}
I will add more info so anyone can help me solve this.
In those code:
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
CVImageBufferRef
cvimgRef
= CMSampleBufferGetImageBuffer(sampleBuffer);
CVPixelBufferLockBaseAddress(
cvimgRef
, 0);
size_t _width = CVPixelBufferGetWidth(
cvimgRef
);
size_t _height = CVPixelBufferGetHeight(
cvimgRef
);
....
}
CMSampleBufferGetImageBuffer() returns a CVImageBuffer!. And CVPixelBufferLockBaseAddress() takes a CVPixelBuffer! as its argument. In C/ObjC, CVPixelBufferRef is typedef'd to CVImageBufferRef. But in robovm binding CVPixelBuffer extend CVImageBuffer and there is no CVImageBufferRef and CVPixelBufferRef .
@Pointer long CVPixelBufferGetBaseAddress(CMSampleBuffer b
);
@Pointer long CVPixelBufferGetBaseAddress(CVPixelBuffer b);
buf.append((CVPixelBufferGetBaseAddress(cvimgRef)?.load(as: UInt8.self))!)
var bprow = CVPixelBufferGetBytesPerRow(cvimgRef)
// and pull out the average rgb value of the frame
var r = UInt8()
var g = UInt8()
var b = UInt8()
for y in 0..<height {
var x = 0
while x < width * 4 {
b += buf[x]
g += buf[x + 1]
r += buf[x + 2]
I am always getting index out of range and buf[0] = 0. Am I doing something wrong here?