IOS关于从照相机获取图片的问题 初步探究通过摄像头实现颜色与形状识别机制

转载自  http://www.it165.net/pro/html/201404/11530.html

 

ios通过摄像头获取特定数据

注释: 由于最近项目需求,需要一个能够实现对摄像头图片获取其中部分内容的功能,类似于二维码扫描。但是只需要获取特定位置的像素块进行简单计算即可,所以听上 去还是很简单的,但是经过实践,发现,现实似乎与想象中不一样,由于摄像头拍到的数据,采用的是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=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:

 

01.- (UIImage *)image:(UIImage *)image rotation:(UIImageOrientation)orientation
02.{
03.long double rotate = 0.0;
04.CGRect rect;
05.float translateX = 0;
06.float translateY = 0;
07.float scaleX = 1.0;
08.float scaleY = 1.0;
09. 
10.switch (orientation) {
11.case UIImageOrientationLeft:
12.rotate = M_PI_2;
13.rect = CGRectMake(0, 0, image.size.height, image.size.width);
14.translateX = 0;
15.translateY = -rect.size.width;
16.scaleY = rect.size.width/rect.size.height;
17.scaleX = rect.size.height/rect.size.width;
18.break;
19.case UIImageOrientationRight:
20.rotate = 3 * M_PI_2;
21.rect = CGRectMake(0, 0, image.size.height, image.size.width);
22.translateX = -rect.size.height;
23.translateY = 0;
24.scaleY = rect.size.width/rect.size.height;
25.scaleX = rect.size.height/rect.size.width;
26.break;
27.case UIImageOrientationDown:
28.rotate = M_PI;
29.rect = CGRectMake(0, 0, image.size.width, image.size.height);
30.translateX = -rect.size.width;
31.translateY = -rect.size.height;
32.break;
33.default:
34.rotate = 0.0;
35.rect = CGRectMake(0, 0, image.size.width, image.size.height);
36.translateX = 0;
37.translateY = 0;
38.break;
39.}
40. 
41.UIGraphicsBeginImageContext(rect.size);
42.CGContextRef context = UIGraphicsGetCurrentContext();
43.//做CTM变换
44.CGContextTranslateCTM(context, 0.0, rect.size.height);
45.CGContextScaleCTM(context, 1.0, -1.0);
46.CGContextRotateCTM(context, rotate);
47.CGContextTranslateCTM(context, translateX, translateY);
48. 
49.CGContextScaleCTM(context, scaleX, scaleY);
50.//绘制图片
51.CGContextDrawImage(context, CGRectMake(0, 0, rect.size.width, rect.size.height), image.CGImage);
52. 
53.UIImage *newPic = UIGraphicsGetImageFromCurrentImageContext();
54. 
55.return newPic;
56.}

结论:

这里的解决方案,就是对底层像素矩阵进行装换之后,在对里面数据截取一部分。可以解决截取部分内容的问题。

2:解决方案二:就是对rect进行装换,根据数据底层,进行rect的转换。

原文地址:https://www.cnblogs.com/allanliu/p/4203516.html