Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-07-2010, 02:50 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 5
Teknoman117 is on a distinguished road
Default Avfoundation troubles

I develop for iPhone os 4.0 and I need the raw images from the camera on the phone for my app. I successfully have video input but the image is over exposed. My test device is an iPhone 3G and the continuous auto exposure in on but not helping.

This is the .h file
Code:
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <CoreVideo/CoreVideo.h>
#import <CoreMedia/CoreMedia.h>

@interface VideoTestViewController : UIViewController <AVCaptureVideoDataOutputSampleBufferDelegate> {
	AVCaptureSession *_captureSession;
	UIImageView *_imageView;
	CALayer *_customLayer;
	AVCaptureVideoPreviewLayer *_prevLayer;
}

@property (nonatomic, retain) AVCaptureSession *captureSession;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) CALayer *customLayer;
@property (nonatomic, retain) AVCaptureVideoPreviewLayer *prevLayer;

- (void)initCapture;

@end
This is the .m file
Code:
#import "VideoTestViewController.h"

@implementation VideoTestViewController

@synthesize captureSession = _captureSession;
@synthesize imageView = _imageView;
@synthesize customLayer = _customLayer;
@synthesize prevLayer = _prevLayer;

- (void)viewDidLoad {
    [super viewDidLoad];
	self.imageView = nil;
	self.prevLayer = nil;
	self.customLayer = nil;
	[self initCapture];
}

- (void)initCapture {
	AVCaptureDevice* captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
	//[captureDevice unlockForConfiguration];
	AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:nil];
	AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];
	captureOutput.alwaysDiscardsLateVideoFrames = YES;
	[captureOutput setSampleBufferDelegate:self queue:dispatch_get_main_queue()];
	NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
	NSNumber* value = [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA];
	NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
	[captureOutput setVideoSettings:videoSettings];
	self.captureSession = [[AVCaptureSession alloc] init];
	[self.captureSession addInput:captureInput];
	[self.captureSession addOutput:captureOutput];
	[self.captureSession startRunning];
	[captureDevice lockForConfiguration:nil];
	if([captureDevice isWhiteBalanceModeSupported:AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance]) {
		NSLog(@"Using decent white balance");
		[captureDevice setWhiteBalanceMode:AVCaptureWhiteBalanceModeContinuousAutoWhiteBalance];
	}
	if([captureDevice isExposureModeSupported:AVCaptureExposureModeContinuousAutoExposure]) {
		NSLog(@"Using decent exposure mode");
		[captureDevice setExposureMode:AVCaptureExposureModeContinuousAutoExposure];
	}
	if([captureDevice isFocusModeSupported:AVCaptureFocusModeContinuousAutoFocus]) {
		NSLog(@"USing decent focusing mode");
		[captureDevice setFocusMode:AVCaptureFocusModeContinuousAutoFocus];
	}
	self.imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
	[self.view addSubview:self.imageView];
}

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection {
    CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
    CVPixelBufferLockBaseAddress(imageBuffer,0);
    uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
    size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
    size_t width = CVPixelBufferGetWidth(imageBuffer);
    size_t height = CVPixelBufferGetHeight(imageBuffer);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef newContext = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
    CGImageRef newImage = CGBitmapContextCreateImage(newContext);
	CVPixelBufferUnlockBaseAddress(imageBuffer,0);
    CGContextRelease(newContext);
    CGColorSpaceRelease(colorSpace);
	UIImage *image= [UIImage imageWithCGImage:newImage scale:1.0 orientation:UIImageOrientationRight];
	self.imageView.image = image;
	CGImageRelease(newImage);
} 


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
	self.imageView = nil;
	self.customLayer = nil;
	self.prevLayer = nil;
}

- (void)dealloc {
	[self.captureSession release];
    [super dealloc];
}

Last edited by Teknoman117; 07-07-2010 at 04:01 PM. Reason: Adding code
Teknoman117 is offline   Reply With Quote
Old 07-07-2010, 11:53 PM   #2 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 5
Teknoman117 is on a distinguished road
Default

Sorry for the false alarm. The video is not being over exposed. The avcapturedeviceinput is returning an image with 128 for the alpha value. It can be fixed using this simple code snippet after it reads the settings in the delegate handler.

Code:
	for(int c=0;c<width*height;c++) {
		baseAddress[4*c+3] = 255;
	}
Teknoman117 is offline   Reply With Quote
Reply

Bookmarks

Tags
avfoundation, camera, exposure, raw, video

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 328
9 members and 319 guests
bignoggins, Chickenrig, firecall, givensur, iNet, michaelhansen, Objective Zero, PlutoPrime, stanny
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,118
Posts: 402,893
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:34 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0