Advertisement
Guest User

View.m

a guest
Feb 9th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.15 KB | None | 0 0
  1. //
  2. // ViewController.m
  3. // Assignment2Template
  4. //
  5. // Created on 2/1/16.
  6. // Copyright © 2016 CMPE161. All rights reserved.
  7. //
  8.  
  9. #import "ViewController.h"
  10.  
  11. @interface ViewController ()
  12. @end
  13.  
  14. @implementation ViewController
  15.  
  16. #pragma mark - View methods
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19.  
  20. NSLog(@"PI constant: %f",M_PI);
  21.  
  22. //Initialize any variables
  23. _viewSize.x = self.view.frame.size.width;//Width of UIView
  24. _viewSize.y =self.view.frame.size.height;//Height of UIView
  25.  
  26. NSLog(@"Width of UIView: %f",_viewSize.x);
  27. NSLog(@"Height of UIView: %f",_viewSize.y);
  28.  
  29. //Initialize arrays
  30. _listOfCircles = [[NSMutableArray alloc]init];
  31. _listOfLines = [[NSMutableArray alloc] init];
  32.  
  33. //Initialize AVCaptureDevice
  34. [self initCapture];
  35.  
  36. }
  37.  
  38. - (void)didReceiveMemoryWarning {
  39. [super didReceiveMemoryWarning];
  40. // Dispose of any resources that can be recreated.
  41. }
  42.  
  43.  
  44.  
  45. #pragma mark - Camera
  46. - (void)initCapture {
  47.  
  48. AVCaptureDevice *theDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
  49. AVCaptureDeviceInput *captureInput = [AVCaptureDeviceInput
  50. deviceInputWithDevice:theDevice
  51. error:nil];
  52. /*We setupt the output*/
  53. AVCaptureVideoDataOutput *captureOutput = [[AVCaptureVideoDataOutput alloc] init];
  54. /*While a frame is processes in -captureOutput:didOutputSampleBuffer:fromConnection: delegate methods no other frames are added in the queue.
  55. If you don't want this behaviour set the property to NO */
  56. captureOutput.alwaysDiscardsLateVideoFrames = YES;
  57.  
  58. //We create a serial queue to handle the processing of our frames
  59. dispatch_queue_t queue;
  60. queue = dispatch_queue_create("cameraQueue", NULL);
  61. [captureOutput setSampleBufferDelegate:self queue:queue];
  62.  
  63. // Set the video output to store frame in YpCbCr planar so we can access the brightness in contiguios memory
  64. NSString* key = (NSString*)kCVPixelBufferPixelFormatTypeKey;
  65. // choice is kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange or RGBA
  66.  
  67. NSNumber* value = [NSNumber numberWithInt:kCVPixelFormatType_32BGRA] ;
  68. NSDictionary* videoSettings = [NSDictionary dictionaryWithObject:value forKey:key];
  69. [captureOutput setVideoSettings:videoSettings];
  70.  
  71. //And we create a capture session
  72. self.captureSession = [[AVCaptureSession alloc] init];
  73.  
  74. //You can change this for different resolutions
  75. self.captureSession.sessionPreset = AVCaptureSessionPresetHigh;
  76.  
  77. //We add input and output
  78. [self.captureSession addInput:captureInput];
  79. [self.captureSession addOutput:captureOutput];
  80.  
  81. //Initialize and add imageview
  82. self.imageView = [[UIImageView alloc] init];
  83.  
  84. #warning Initialize to size of the screen. You need to select the right values and replace 100 and 100
  85. //TODO: select right width and height value
  86. self.imageView.frame = CGRectMake(0, 0,375.0,667.0);
  87.  
  88. //Add subviews to master view
  89. //The order is important in order to view the button
  90. [self.view addSubview:self.imageView];
  91.  
  92. #warning Add any UI elements here
  93. [self.view addSubview:_object2DSelectionSegmentControl];//Adding the segment control
  94.  
  95.  
  96. //Once startRunning is called the camera will start capturing frames
  97. [self.captureSession startRunning];
  98. }
  99.  
  100. - (void)captureOutput:(AVCaptureOutput *)captureOutput
  101. didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
  102. fromConnection:(AVCaptureConnection *)connection
  103. {
  104. CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
  105. /*Lock the image buffer*/
  106. CVPixelBufferLockBaseAddress(imageBuffer,0);
  107.  
  108. uint8_t *baseAddress = (uint8_t *)CVPixelBufferGetBaseAddress(imageBuffer);
  109.  
  110. // Get the number of bytes per row for the pixel buffer
  111. size_t bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer);
  112.  
  113. // Get the pixel buffer width and height
  114. self.width = CVPixelBufferGetWidth(imageBuffer);
  115. self.height = CVPixelBufferGetHeight(imageBuffer);
  116.  
  117.  
  118. //----------------------------------------------------------------------------------------
  119. //If you need to do something on the image, uncomment the following
  120. ///////////////////////////
  121. // we copy imageBuffer into another chunk of memory
  122. // this is necessary otherwise it will slow things down
  123. // however, we should be able to allocate this buffer once and for all rather than creating it and destroying it at each frame
  124. //----------------------------------------------------------------------------------------
  125. // uint8_t *base = (uint8_t *) malloc(bytesPerRow * self.height * sizeof(uint8_t));
  126. // memcpy(base, baseAddress, bytesPerRow * self.height);
  127. // CVPixelBufferUnlockBaseAddress(imageBuffer,0);
  128. //
  129. // NSLog(@"%zu",self.height);
  130. // NSLog(@"%zu",self.width);
  131. // NSLog(@"Bytes per row: %zu",bytesPerRow);
  132. //
  133. // // just for fun, let's do something on this image
  134. // // swap color channels
  135. // uint8_t *tp1, *tp2;
  136. // tp1 = base;
  137. // for (int iy=0;iy<self.height; iy++,tp1 += bytesPerRow){
  138. // tp2 = tp1;
  139. // for (int ix=0 ;ix<bytesPerRow;ix+=4,tp2+=4){
  140. // uint8_t aux = *tp2;
  141. //
  142. // //NSLog(@"%d",*tp1);
  143. //
  144. // if (ix==1500) {
  145. // *tp2 = 0;
  146. // *(tp2+1)=0;
  147. // *(tp2+2) = 0;
  148. // } else {
  149. //
  150. // *tp2 = *(tp2+1);
  151. // *(tp2+1)=*(tp2+2);
  152. // *(tp2+2) = aux;
  153. // }
  154. //
  155. // }
  156. // }
  157. //
  158. // //Draw an image
  159. //
  160. // // copy the buffer back to imageBuffer
  161. // CVPixelBufferLockBaseAddress(imageBuffer,0);
  162. // memcpy(baseAddress, base, bytesPerRow * self.height);
  163. // free(base);
  164. //----------------------------------------------------------------------------------------
  165.  
  166. // Create a device-dependent RGB color space
  167. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  168.  
  169. // Create a bitmap graphics context with the sample buffer data
  170. CGContextRef context = CGBitmapContextCreate(baseAddress, self.width, self.height, 8, bytesPerRow, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedFirst);
  171.  
  172. //Context size
  173. _contextSize.x = self.width;
  174. _contextSize.y = self.height;
  175.  
  176. _contextSizeLine.origin.x = self.width;
  177. _contextSizeLine.origin.y = self.height;
  178. //See the values of the image buffer
  179. // NSLog(@"Self width: %zu",self.width);
  180. // NSLog(@"Self height: %zu",self.height);
  181.  
  182.  
  183. #warning Here is where you draw 2D objects
  184. //----------------------------------------------------------------------------------------
  185. //TODO: Here is where you draw 2D objects.
  186. //----------------------------------------------------------------------------------------
  187. //Draw axes
  188.  
  189.  
  190.  
  191.  
  192. //Draw circles
  193. if (_objectToDraw == CIRCLE) {
  194.  
  195. //Iterate through the list of circles and draw them all
  196. for (int i=0; i<[_listOfCircles count]; i++) {
  197.  
  198. //Mapping constant calculated to do the mapping from Points to Context
  199. [_listOfCircles[i] drawCircle:context:CGPointMake((_contextSize.y/_viewSize.x), (_contextSize.x/_viewSize.y))];
  200. }
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208. //Draw lines
  209. }else if (_objectToDraw == LINE){
  210.  
  211. CGRect rect = CGRectMake(1, 1, 100, 100);
  212.  
  213. CGFloat rect1= 100;
  214. CGFloat rect2=150;
  215.  
  216.  
  217.  
  218.  
  219.  
  220. //UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(10,10, self.view.frame.size.width, 1)];
  221. //lineView.backgroundColor = [UIColor greenColor];
  222. //[self.view addSubview:lineView];
  223.  
  224. //Iterate through the list of circles and draw them all
  225. //for (int i=0; i<[_listOfLines count]; i++) {
  226.  
  227. //Mapping constant calculated to do the mapping from Points to Context
  228. drawLine:context:CGPointMake(rect1, rect2);
  229.  
  230.  
  231. //Example of calling class method from Shape2D to rotate a vector by 90 degreesdraw
  232. //CGPoint results;
  233. //results = [Shape2D rotateVector:GLKVector3Make(4.0f, 3.0f, 0.0f) :90];
  234.  
  235. // }
  236.  
  237.  
  238.  
  239. }
  240. //----------------------------------------------------------------------------------------
  241. //----------------------------------------------------------------------------------------
  242.  
  243.  
  244.  
  245. // Create a Quartz image from the pixel data in the bitmap graphics context
  246. CGImageRef quartzImage = CGBitmapContextCreateImage(context);
  247.  
  248. // Unlock the pixel buffer
  249. CVPixelBufferUnlockBaseAddress(imageBuffer,0);
  250.  
  251. // Free up the context and color space
  252. CGContextRelease(context);
  253. CGColorSpaceRelease(colorSpace);
  254.  
  255. // Create an image object from the Quartz image
  256. // UIImage *image = [UIImage imageWithCGImage:quartzImage];
  257. UIImage *image = [UIImage imageWithCGImage:quartzImage scale:(CGFloat)1 orientation:UIImageOrientationRight];
  258.  
  259. // Release the Quartz image
  260. CGImageRelease(quartzImage);
  261.  
  262. //notice we use this selector to call our setter method 'setImg' Since only the main thread can update this
  263. [self.imageView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:YES];
  264. }
  265.  
  266.  
  267. #pragma mark - Actions
  268. #warning Example of using segmented controls
  269. - (IBAction)object2DSelectionSegmentChanged:(id)sender {
  270.  
  271. //Line selected
  272. if (_object2DSelectionSegmentControl.selectedSegmentIndex == LINE) {
  273.  
  274. //Let the captureOutput() know to draw lines only
  275. _objectToDraw = LINE;
  276.  
  277. CGRect location;
  278.  
  279. //Middle of the screen using iPhone6
  280. location.origin.x = 188.0f;
  281. location.origin.y = 334.0f;
  282.  
  283. //Create circle object with center location
  284. Line* line = [[Line alloc]initWithCGRect:location];
  285.  
  286. //Add it to list of circles
  287. [_listOfLines addObject:line];
  288.  
  289.  
  290.  
  291.  
  292. //Circle selected
  293. }else if (_object2DSelectionSegmentControl.selectedSegmentIndex == CIRCLE) {
  294.  
  295. //Let the captureOutput() know to draw circles only
  296. _objectToDraw = CIRCLE;
  297.  
  298. //Location of the circle on the screen
  299. //This is different according to your device
  300. //See: http://www.paintcodeapp.com/news/ultimate-guide-to-iphone-resolutions
  301. CGPoint location;
  302.  
  303. //Middle of the screen using iPhone6
  304. location.x = 188.0f;
  305. location.y = 334.0f;
  306.  
  307. //Create circle object with center location
  308. Circle* circle = [[Circle alloc]initWithCGPoint:location];
  309.  
  310. //Add it to list of circles
  311. [_listOfCircles addObject:circle];
  312. }
  313. }
  314. @end
  315.  
  316. //MAKE SURE TO CONVERT CGContext TO A UI IMAGE VIEW..... German may have already given us this code in the camera section
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement