iOS利用手机摄像头测心率

2020-01-17 21:54:12丽君

我目前是这样处理的,后面是用的前后两个峰值与 最低峰值 最接近的那个峰值的时间差,测了几次又和别的app比较了一下,基本都是正确的,最多也就是上下差1-2次每分钟。(在数据比较稳定的情况下,如果有更好的方法请推荐,谢谢)

5.画折线图 这里用到了 CoreGraphics

PS:首先,使用这个CoreGraphics要在View里面,并且要在View的 drawRect: 方法中使用,不然拿不到画布。我是为了封装单独建立了一个UIView的类。

a.首先还是数据,没有数据怎么画


@property (strong, nonatomic) NSMutableArray *points;
// 在init中初始化数组
self.points = [[NSMutableArray alloc]init];
// 这个可以翻译过来,也是在init中
self.clearsContextBeforeDrawing = YES;

// 外部调用方法
- (void)drawRateWithPoint:(NSNumber *)point {
  // 倒叙插入数组
  [self.points insertObject:point atIndex:0];

  // 删除溢出屏幕数据
  if (self.points.count > self.frame.size.width/6) {
    [self.points removeLastObject];
  }

  dispatch_async(dispatch_get_main_queue(), ^{
    // 这个方法自动调取 drawRect:方法
    [self setNeedsDisplay];
  });
}

之前调 setNeedsDisplay ,一直没有走 drawRect: 方法,或者就直走了一次,然后去百度是说 setNeedsDisplay 会在系统空闲的时候执行 drawRect: ,然后我尝试着回归到主线程中调用,就好了。具体原因不是很清楚,也可能是因为要在主线程中修改View。

b.画折线的方法,具体怎么调整看个人心情了。


CGFloat ww = self.frame.size.width;
  CGFloat hh = self.frame.size.height;
  CGFloat pos_x = ww;
  CGFloat pos_y = hh/2;
  // 获取当前画布
  CGContextRef context = UIGraphicsGetCurrentContext();
  // 折线宽度
  CGContextSetLineWidth(context, 1.0);
  //消除锯齿
  //CGContextSetAllowsAntialiasing(context,false);
  // 折线颜色
  CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
  CGContextMoveToPoint(context, pos_x, pos_y);
  for (int i = 0; i < self.points.count; i++) {
    float h = [self.points[i] floatValue];
    pos_y = hh/2 + (h * hh/2) ;
    CGContextAddLineToPoint(context, pos_x, pos_y);
    pos_x -=6;
  }
  CGContextStrokePath(context);

c.为了看起来好看,我还加了网格,当然也是在 drawRect: 中调用的

 


static CGFloat grid_w = 30.0f;
- (void)buildGrid {

  CGFloat wight = self.frame.size.width;
  CGFloat height = self.frame.size.height;

  // 获取当前画布
  CGContextRef context = UIGraphicsGetCurrentContext();

  CGFloat pos_x = 0.0f;
  CGFloat pos_y = 0.0f;

  // 在wight范围内画竖线
  while (pos_x < wight) {
    // 设置网格线宽度
    CGContextSetLineWidth(context, 0.2);
    // 设置网格线颜色
    CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
    // 起点
    CGContextMoveToPoint(context, pos_x, 1.0f);
    // 终点
    CGContextAddLineToPoint(context, pos_x, height);
    pos_x +=grid_w;
    //开始划线
    CGContextStrokePath(context);
  }

  // 在height范围内画横线
  while (pos_y < height) {

    CGContextSetLineWidth(context, 0.2);
    CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
    CGContextMoveToPoint(context, 1.0f, pos_y);
    CGContextAddLineToPoint(context, wight, pos_y);
    pos_y +=grid_w;
    CGContextStrokePath(context);
  }
  pos_x = 0.0f; pos_y = 0.0f;

  // 在wight范围内画竖线
  while (pos_x < wight) {
    CGContextSetLineWidth(context, 0.1);
    CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
    CGContextMoveToPoint(context, pos_x, 1.0f);
    CGContextAddLineToPoint(context, pos_x, height);
    pos_x +=grid_w/5;
    CGContextStrokePath(context);
  }

  // 在height范围内画横线
  while (pos_y < height) {
    CGContextSetLineWidth(context, 0.1);
    CGContextSetStrokeColorWithColor(context, [UIColor greenColor].CGColor);
    CGContextMoveToPoint(context, 1.0f, pos_y);
    CGContextAddLineToPoint(context, wight, pos_y);
    pos_y +=grid_w/5;
    CGContextStrokePath(context);
  }

}