zoukankan      html  css  js  c++  java
  • ios调用摄像头

    //
    // CameraViewController.m
    // Camera
    //
    // Created by Dave Mark on 12/16/10.
    // Copyright 2010 __MyCompanyName__. All rights reserved.
    //

    #import "CameraViewController.h"
    #import <MobileCoreServices/UTCoreTypes.h>

    @interface CameraViewController ()
    static UIImage *shrinkImage(UIImage *original, CGSize size);
    - (void)updateDisplay;
    - (void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType;
    @end

    @implementation CameraViewController
    @synthesize imageView;
    @synthesize takePictureButton;
    @synthesize moviePlayerController;
    @synthesize image;
    @synthesize movieURL;
    @synthesize lastChosenMediaType;

    - (void)viewDidLoad {
    if (![UIImagePickerController isSourceTypeAvailable:
    UIImagePickerControllerSourceTypeCamera]) {
    takePictureButton.hidden = YES;
    }
    imageFrame = imageView.frame;
    }

    - (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self updateDisplay];
    }

    - (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
    }

    - (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.imageView = nil;
    self.takePictureButton = nil;
    self.moviePlayerController = nil;
    [super viewDidUnload];
    }

    - (void)dealloc {
    [imageView release];
    [takePictureButton release];
    [moviePlayerController release];
    [image release];
    [movieURL release];
    [lastChosenMediaType release];
    [super dealloc];
    }

    - (IBAction)shootPictureOrVideo:(id)sender {
    [self getMediaFromSource:UIImagePickerControllerSourceTypeCamera];
    }

    - (IBAction)selectExistingPictureOrVideo:(id)sender {
    [self getMediaFromSource:UIImagePickerControllerSourceTypePhotoLibrary];
    }

    #pragma mark UIImagePickerController delegate methods
    - (void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingMediaWithInfo:(NSDictionary *)info {
    self.lastChosenMediaType = [info objectForKey:UIImagePickerControllerMediaType];
    if ([lastChosenMediaType isEqual:(NSString *)kUTTypeImage]) {
    UIImage *chosenImage = [info objectForKey:UIImagePickerControllerEditedImage];
    UIImage *shrunkenImage = shrinkImage(chosenImage, imageFrame.size);
    self.image = shrunkenImage;
    } else if ([lastChosenMediaType isEqual:(NSString *)kUTTypeMovie]) {
    self.movieURL = [info objectForKey:UIImagePickerControllerMediaURL];
    }
    [picker dismissModalViewControllerAnimated:YES];
    }

    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissModalViewControllerAnimated:YES];
    }

    #pragma mark -
    static UIImage *shrinkImage(UIImage *original, CGSize size) {
    CGFloat scale = [UIScreen mainScreen].scale;
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    CGContextRef context = CGBitmapContextCreate(NULL, size.width * scale,
    size.height * scale, 8, 0, colorSpace, kCGImageAlphaPremultipliedFirst);
    CGContextDrawImage(context,
    CGRectMake(0, 0, size.width * scale, size.height * scale),
    original.CGImage);
    CGImageRef shrunken = CGBitmapContextCreateImage(context);
    UIImage *final = [UIImage imageWithCGImage:shrunken];

    CGContextRelease(context);
    CGImageRelease(shrunken);

    return final;
    }

    - (void)updateDisplay {
    if ([lastChosenMediaType isEqual:(NSString *)kUTTypeImage]) {
    imageView.image = image;
    imageView.hidden = NO;
    moviePlayerController.view.hidden = YES;
    } else if ([lastChosenMediaType isEqual:(NSString *)kUTTypeMovie]) {
    [self.moviePlayerController.view removeFromSuperview];
    self.moviePlayerController = [[[MPMoviePlayerController alloc]
    initWithContentURL:movieURL] autorelease];
    moviePlayerController.view.frame = imageFrame;
    moviePlayerController.view.clipsToBounds = YES;
    [self.view addSubview:moviePlayerController.view];
    imageView.hidden = YES;
    }
    }

    - (void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType {
    NSArray *mediaTypes = [UIImagePickerController
    availableMediaTypesForSourceType:sourceType];
    if ([UIImagePickerController isSourceTypeAvailable:
    sourceType] && [mediaTypes count] > 0) {
    NSArray *mediaTypes = [UIImagePickerController
    availableMediaTypesForSourceType:sourceType];
    UIImagePickerController *picker =
    [[UIImagePickerController alloc] init];
    picker.mediaTypes = mediaTypes;
    picker.delegate = self;
    picker.allowsEditing = YES;
    picker.sourceType = sourceType;
    [self presentModalViewController:picker animated:YES];
    [picker release];
    }
    else {
    UIAlertView *alert = [[UIAlertView alloc]
    initWithTitle:@"Error accessing media"
    message:@"Device doesn’t support that media source."
    delegate:nil
    cancelButtonTitle:@"Drat!"
    otherButtonTitles:nil];
    [alert show];
    [alert release];
    }
    }

    @end

  • 相关阅读:
    【未完成】【oracle】add_months与add_days
    【java】[null]的问题
    PID控制算法研究
    matlab函数:residue和residuez的用法
    Windows入门基础:2.vs2013中Icon显示
    关于for()循环使用过程中遇到的问题(俄罗斯方块游戏中遇到的问题)
    Windows入门基础:1.关于CreateWindow()函数使用中遇到的问题
    error C4996: 'swprintf': swprintf has been changed to conform with the ISO C standard,set _CRT_NON_CONFORMING_SWPRINT
    QT5.3.2与VS2013 fatal error LNK1112: 模块计算机类型“X86”与目标计算机类型“x64”冲突_(解决方案)
    Java如何访问private变量
  • 原文地址:https://www.cnblogs.com/mfryf/p/2377949.html
Copyright © 2011-2022 走看看