美文网首页iOS十月份转载
利用GPUImage添加实时文字水印和gif图片水印

利用GPUImage添加实时文字水印和gif图片水印

作者: 改变自己_now | 来源:发表于2017-02-07 16:09 被阅读1319次

公司新闻视频直播需要添加实时文字和gif水印,在网上看了下大部分都是基于GPUImage来处理的,发现大部分添加水印都是静态图片,么有加载gif图片水印的。于是自己尝试实现,这里参考了落影loyinglin给视频添加水印的思路。

主要思路:

1、把gif图片中的每帧图片提取出来保存到数组中,用索引index记录当前帧的位置
2、再把每帧图片和文字转为GUPUIElement对象
3、在实时视频的filter中的setFrameProcessingCompletionBlock中去update,index++,当index等于帧数组count-1,重置为零。
这样就能看到水印为gif动图。

代码实现
GPUImageView *gpuImageView = (GPUImageView*)self.view;

GPUImageVideoCamera *videoCamera = [[GPUImageVideoCamera alloc] initWithSessionPreset:AVCaptureSessionPreset640x480 cameraPosition:AVCaptureDevicePositionBack];

videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;
_videoCamera = videoCamera;

//添加时间戳水印和图片水印
UIView *contentView = [[UIView alloc] initWithFrame:self.view.bounds];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy年MM月dd日hh:mm:ss"];
NSDate *currentDate = [NSDate date];
NSString *timeString = [formatter stringFromDate:currentDate];

UILabel *timestampLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 30, 300, 30)];
timestampLabel.text = timeString;
timestampLabel.textColor = [UIColor redColor];
[contentView addSubview:timestampLabel];

UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100, 300, 80)];



NSString *path = [[NSBundle mainBundle] pathForResource:@"dong.gif" ofType:nil] ;
NSData *imageData = [NSData dataWithContentsOfFile:path];
_gifImages = [NSArray array];
_gifImages = [UIImage yj_animatedGIFImagesWithData:imageData];
_duration = [UIImage yj_animatedGIFDurationWithData:imageData];
_currenIndex = 0;
imageV.image = _gifImages[_currenIndex];
[contentView addSubview:imageV];


//创建水印图形
GPUImageUIElement *uiElement = [[GPUImageUIElement alloc] initWithView:contentView];



//创建滤镜
GPUImageDissolveBlendFilter *filter = [[GPUImageDissolveBlendFilter alloc] init];
filter.mix = 0.5;

GPUImageFilter *videoFilter = [[GPUImageFilter alloc] init];
[videoCamera addTarget:videoFilter];

[videoFilter addTarget:filter];
[uiElement addTarget:filter];

// 添加滤镜

[filter addTarget:gpuImageView];

[videoCamera startCameraCapture];

// 这句代码很重要,没有的话不会更新
[videoFilter setFrameProcessingCompletionBlock:^(GPUImageOutput *output, CMTime time) {
   
    
//        CGRect frame =  imageV.frame;
//        frame.origin.x += 1;
//        frame.origin.y += 1;
//         imageV.frame = frame;
    

    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy年MM月dd日hh:mm:ss"];
    NSDate *currentDate = [NSDate date];
    NSString *timeString = [formatter stringFromDate:currentDate];
    timestampLabel.text = timeString;
    
    
    _currenIndex ++;
    imageV.image = _gifImages[_currenIndex];
    
    if (_currenIndex == _gifImages.count -1) {
        
        _currenIndex = 0;
    }
    
    [uiElement update];
    
}];

如果有更加好的思路,希望告知,谢谢!

相关文章

网友评论

  • 张芳涛:_gifImages = [UIImage yj_animatedGIFImagesWithData:imageData];
    _duration = [UIImage yj_animatedGIFDurationWithData:imageData];这两个方法都做了什么?
    改变自己_now:@张芳涛 第一个是把gif图片专为image图片数组,第二个是获取总时间
  • 聪zero:求个demo
    聪zero:@聪zero 大神,请问初始化的时候最低分辨率只有640*480,如果我想设置320*240,如何设置,还有码率跟帧数我知道在GPUImage内部的GPUImageMovieWriter好像可以改,但是尽量不改源码比较好,有没有办法可以在初始化的时候设置这些参数,另外我是实时录制的,不想通过在录制之后重新压制的方法来设置这些参数,求指教

    聪zero:@登低自高 yj_animatedGIFImagesWithData这个是什么
    改变自己_now:@聪zero 主要代码都贴了啊
  • wo不懂:水印模糊怎么处理的
    改变自己_now: @wo不懂 你应该可以直接把水印模糊处理,
    wo不懂:@为谁而鸣 18910381054加个微信
    改变自己_now: @wo不懂 你在GPUImage中看有没有相关的滤镜了

本文标题:利用GPUImage添加实时文字水印和gif图片水印

本文链接:https://www.haomeiwen.com/subject/lofmittx.html