首页 > 代码库 > 直接获取摄像头传回的图像数据(人脸、微笑、眨眼: 识别-->第一步):图像识别第一步
直接获取摄像头传回的图像数据(人脸、微笑、眨眼: 识别-->第一步):图像识别第一步
转:ios通过摄像头获取特定数据
文字太小可通过command ‘+’调大字体,温馨提示。注释: 由于最近项目需求,需要一个能够实现对摄像头图片获取其中部分内容的功能,类似于二维码扫描。但是只需要获取特定位置的像素块进行简单计算即可,所以听上去还是很简单的,但是经过实践,发现,现实似乎与想象中不一样,由于摄像头拍到的数据,采用的是QZ(也就是CG框架)进行绘制,所以涉及到坐标系与frame的坐标系不一致的问题。
1:尝试直接拿到摄像头数据,先输出看下。 code: 代码略。
总结: 直接使用摄像头数据再用imageView的方式显示出来的时刻,我们发现,数据是正确的==。所见即所得的方式。 需要注意的是: 当图片是横向拍摄的时刻,我们可以看到,相机会自动将图片进行90度旋转,也就是说,系统在你横向拍摄的时刻,会自动将图片旋转90度,满足你正常情况下的观看。
所以: 我们使用imageView对拿到的数据进行展示的时刻,数据并没有颠倒的现象。为了防止出现这个现象的原因是出于imageView的内部实现,我们使用CgimageRef的方式,再次验证 从摄像头拿到的数据真的和我们所看到的一样吗?
code:
CGImageRef oldImageRef=image.CGImage;
UIImage* newImage=[UIImage imageWithCGImage:oldImageRef];
注释:在这里,我们只是对拿到的Image对象,先转换成了CGimage,再使用UIImage的方法得到新的Image对象。
结果:我们发现,得到的图像果然就是旋转90度的图片,而不再是原图了。
UIImage* newImage=[UIImage imageWithCGImage:oldImageRef scale:.1 orientation:UIImageOrientationRight];
使用上面的语句,可以将图像旋转90度变成我们需要的样子。思考:
这么简单的变化中,图片会不会丢失数据? ---------
NSData* data=http://www.mamicode.com/UIImageJPEGRepresentation(image, 0);
NSLog(@"%lu",(unsigned long)data.length);
还好,数据没有丢失。既然数据没有丢失,那么我们应该可以获取到其中特定的一块数据得出,再显示出来才对。
问题来了: 我们要获取某一块数据的内容,我们应该怎么传递rect呢?因为我们默认的rect与CG的坐标系并不同。是否需要转换呢?
1:我们先试一下,先把数据转过来,再从里面拿一部分. code:
CGImageRef imageRef=image.CGImage;
CGImageRef newimage=CGImageCreateWithImageInRect(imageRef, rect);
结果发现: 拿到的数据还是倾斜了90度。也就是说,我们上面对数据这样的转换其实根本没有起到作用,数据在底层存储的方式就是使用CG的坐标系存储的。
经过试验发现,[UIImage imageWithCGImage:oldImageRef scale:.1 orientation:UIImageOrientationRight]中,对orientation的改变,不会对我们的实验结果产生任何影响。 也就是说,这个方法,其实并没有对底层的数据进行操作,而只不过是在初始化新的UIImage的时刻,对底层像素的读取,orientation不同,方向也不同而已。
思考:
因为,我们使用上面的方法,并没有操作到底层的像素矩阵,也就是我,我们如果想要解决这个问题,有两种方式, 1:将底层数据矩阵转换成我们需要的对应坐标系的内容。 这也就是CTM转换。 将Image对象内部数据进行转换。 code:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | - (UIImage *)image:(UIImage *)image rotation:(UIImageOrientation)orientation { long double rotate = 0.0 ; CGRect rect; float translateX = 0 ; float translateY = 0 ; float scaleX = 1.0 ; float scaleY = 1.0 ; switch (orientation) { case UIImageOrientationLeft: rotate = M_PI_2; rect = CGRectMake( 0 , 0 , image.size.height, image.size.width); translateX = 0 ; translateY = -rect.size.width; scaleY = rect.size.width/rect.size.height; scaleX = rect.size.height/rect.size.width; break ; case UIImageOrientationRight: rotate = 3 * M_PI_2; rect = CGRectMake( 0 , 0 , image.size.height, image.size.width); translateX = -rect.size.height; translateY = 0 ; scaleY = rect.size.width/rect.size.height; scaleX = rect.size.height/rect.size.width; break ; case UIImageOrientationDown: rotate = M_PI; rect = CGRectMake( 0 , 0 , image.size.width, image.size.height); translateX = -rect.size.width; translateY = -rect.size.height; break ; default : rotate = 0.0 ; rect = CGRectMake( 0 , 0 , image.size.width, image.size.height); translateX = 0 ; translateY = 0 ; break ; } UIGraphicsBeginImageContext(rect.size); CGContextRef context = UIGraphicsGetCurrentContext(); //做CTM变换 CGContextTranslateCTM(context, 0.0 , rect.size.height); CGContextScaleCTM(context, 1.0 , - 1.0 ); CGContextRotateCTM(context, rotate); CGContextTranslateCTM(context, translateX, translateY); CGContextScaleCTM(context, scaleX, scaleY); //绘制图片 CGContextDrawImage(context, CGRectMake( 0 , 0 , rect.size.width, rect.size.height), image.CGImage); UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext(); return newPic; } |
结论:
这里的解决方案,就是对底层像素矩阵进行装换之后,在对里面数据截取一部分。可以解决截取部分内容的问题。
-------------------
2:解决方案二:就是对rect进行装换,根据数据底层,进行rect的转换。
直接获取摄像头传回的图像数据(人脸、微笑、眨眼: 识别-->第一步):图像识别第一步
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。