通过调用系统摄像头拍照,拍视频,然后写入文件
来源:互联网 发布:淘宝卖家中心登陆 编辑:程序博客网 时间:2024/04/27 14:06
通过调用系统摄像头拍照,拍视频,然后写入文件的
时间: 2010-06-08 16:50 点击:294 次
如果您的App里需要获得由系统自带的照相机、摄像机和录音软件所生成的文件。可以借鉴以下代码来调用iPhone摄像头拍照或者摄像的功能,并把获得的数据直接写入到文件。 //这一段是
如果您的App里需要获得由系统自带的照相机、摄像机和录音软件所生成的文件。可以借鉴以下代码来调用iPhone摄像头拍照或者摄像的功能,并把获得的数据直接写入到文件。
//这一段是,点击一个takePicture按钮的操作.
- (IBAction)takePicture:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
NSArray *temp_MediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
picker.mediaTypes = temp_MediaTypes;
picker.delegate = self;
picker.allowsImageEditing = YES;
}
[self presentModalViewController:picker animated:YES];
[picker release];
}
//下面两个函数是遵守 UIImagePickerControllerDelegate这个协议所实现的类.这样就能够完整的实现,获取照片或者视频,然后写入文件的过程.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if ([mediaType isEqualToString:@"public.image"]){
UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
NSLog(@"found an image");
NSString *imageFile = [documentsDirectory stringByAppendingPathComponent:@"temp.jpg"];
NSLog(@"%@", imageFile);
success = [fileManager fileExistsAtPath:imageFile];
if(success) {
success = [fileManager removeItemAtPath:imageFile error:&error];
}
imageView.image = image;
[UIImageJPEGRepresentation(image, 1.0f) writeToFile:imageFile atomically:YES];
//SETIMAGE(image);
//CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);
}
else if([mediaType isEqualToString:@"public.movie"]){
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSLog(@"%@", videoURL);
NSLog(@"found a video");
NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
/****************************************/
NSString *videoFile = [documentsDirectory stringByAppendingPathComponent:@"temp.mov"];
NSLog(@"%@", videoFile);
success = [fileManager fileExistsAtPath:videoFile];
if(success) {
success = [fileManager removeItemAtPath:videoFile error:&error];
}
[videoData writeToFile:videoFile atomically:YES];
//CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);
//NSLog(videoURL);
}
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissModalViewControllerAnimated:YES];
}
欢迎进入CocoaChina论坛参与讨论,本帖地址 http://www.cocoachina.com/bbs/read.php?tid-18588.html
//这一段是,点击一个takePicture按钮的操作.
- (IBAction)takePicture:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
NSArray *temp_MediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
picker.mediaTypes = temp_MediaTypes;
picker.delegate = self;
picker.allowsImageEditing = YES;
}
[self presentModalViewController:picker animated:YES];
[picker release];
}
//下面两个函数是遵守 UIImagePickerControllerDelegate这个协议所实现的类.这样就能够完整的实现,获取照片或者视频,然后写入文件的过程.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if ([mediaType isEqualToString:@"public.image"]){
UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
NSLog(@"found an image");
NSString *imageFile = [documentsDirectory stringByAppendingPathComponent:@"temp.jpg"];
NSLog(@"%@", imageFile);
success = [fileManager fileExistsAtPath:imageFile];
if(success) {
success = [fileManager removeItemAtPath:imageFile error:&error];
}
imageView.image = image;
[UIImageJPEGRepresentation(image, 1.0f) writeToFile:imageFile atomically:YES];
//SETIMAGE(image);
//CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);
}
else if([mediaType isEqualToString:@"public.movie"]){
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSLog(@"%@", videoURL);
NSLog(@"found a video");
NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
/****************************************/
NSString *videoFile = [documentsDirectory stringByAppendingPathComponent:@"temp.mov"];
NSLog(@"%@", videoFile);
success = [fileManager fileExistsAtPath:videoFile];
if(success) {
success = [fileManager removeItemAtPath:videoFile error:&error];
}
[videoData writeToFile:videoFile atomically:YES];
//CFShow([[NSFileManager defaultManager] directoryContentsAtPath:[NSHomeDirectory() stringByAppendingString:@"/Documents"]]);
//NSLog(videoURL);
}
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissModalViewControllerAnimated:YES];
}
欢迎进入CocoaChina论坛参与讨论,本帖地址 http://www.cocoachina.com/bbs/read.php?tid-18588.html
- 通过调用系统摄像头拍照,拍视频,然后写入文件
- 通过调用系统摄像头拍照,拍视频,然后写入文件的代码范例.
- 通过调用系统摄像头拍照,拍视频,然后写入文件的代码范例
- ios 通过调用系统摄像头拍照,拍视频,然后写入文件
- ios 通过调用系统摄像头拍照,拍视频,然后写入文件
- 通过调用系统摄像头拍照,拍视频
- ios 通过调用系统摄像头拍照,…
- android 调用 系统 相机 摄像机 拍照 拍视频
- Android 调用摄像头功能【拍照与视频】
- 调用系统摄像头拍照,获取原图
- iOS摄像头拍照或者摄像的数据写入到文件
- Android 使用系统摄像头拍相片与拍视频,并显示
- 用jmf调用摄像头视频拍照的例子
- 《OpenCV》Part2 OpenCV3.1.0调用摄像头显示视频并拍照
- android Camera摄像头-Surface view 预览拍照 并将拍的照片插入到系统图库
- Android中通过Intent 调用系统的图片、视频、音频、录音、拍照
- Android中通过Intent 调用系统的图片、视频、音频、录音、拍照
- android调用系统的相机拍照 、裁剪,然后保存
- Solr DataImportHandler 之一 关系数据库批量和增量导数据
- JModeltest使用说明
- 嵌入式文件系统工具的来源
- SAP JCo3安装在LINUX系统
- Solr DataImportHandler 之二 XML/http导入
- 通过调用系统摄像头拍照,拍视频,然后写入文件
- 知识工作的流程特点
- 博客开通了!
- 贪心算法(Greedy Algorithm)之霍夫曼编码(Huffman codes)
- linux下的第一个驱动程序 hello (for 2.4)
- ORA-01157: 无法标识/锁定数据文件 解决方案
- 如何彻底清除上网痕迹
- 在Android模拟器上安装和卸载APK包(转)
- 协议代码改进的步骤