用GPUImage开启相机并且开启滤镜效果

本文介绍如何利用GPUImage框架中的GPUImageVideoCamera类实现带有滤镜效果的视频捕捉功能。文章详细解释了初始化摄像头、设置视频分辨率、选择前后摄像头、调整输出方向等步骤,并演示了如何将滤镜效果应用到视频上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

GPUImage提供了GPUImageVideoCamera这么一个类,它的对象能够调用摄像头,并且加上滤镜的效果。

    //init VideoCamera

    //这里的两个参数可以设定拍摄录像的像素,还有拍摄录像的前后摄像头。不过要注意的是前后摄像头对像素的要求不同,1080P的录像就不可能在钱摄像头完成了哈

    videoCamera = [[GPUImageVideoCameraalloc] initWithSessionPreset:AVCaptureSessionPreset640x480cameraPosition:AVCaptureDevicePositionBack];

    //这个参数是确定摄像的方向

    videoCamera.outputImageOrientation = UIInterfaceOrientationPortrait;

 

    //开始摄像。这个方法可以放在任意地方,只要运行了这个方法就开始摄像了

    [videoCamerastartCameraCapture];

这些只是开始摄像的初始化工作,如果没有图像输出那么也是没有用的哈(虽然StartCameraCapture的作用是开启摄像头,但是如果摄像头上的图像没有传递到屏幕上那么是没有图像的)

    //把滤镜效果加给摄像头

    [videoCameraaddTarget:testFilter];

    //把摄像头上的图像给GPUImageView显示出来

    [testFilteraddTarget:imageView];

在这里的TestFilter已经是初始化过了的滤镜效果。
这样就可以成功摄像了

        //关闭摄像头

        [videoCamerastopCameraCapture];

 关闭摄像头也很简单,就不多说了哈

 

还有一个最重要的地方,开启摄像头需要完成以下接口才能正常运行:

#pragma mark - vidoe camera

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation

{

    // Map UIDeviceOrientation to UIInterfaceOrientation.

    UIInterfaceOrientation orient = UIInterfaceOrientationPortrait;

    switch ([[UIDevicecurrentDevice] orientation])

    {

        caseUIDeviceOrientationLandscapeLeft:

            orient = UIInterfaceOrientationLandscapeLeft;

            break;

            

        caseUIDeviceOrientationLandscapeRight:

            orient = UIInterfaceOrientationLandscapeRight;

            break;

            

        caseUIDeviceOrientationPortrait:

            orient = UIInterfaceOrientationPortrait;

            break;

            

        caseUIDeviceOrientationPortraitUpsideDown:

            orient = UIInterfaceOrientationPortraitUpsideDown;

            break;

            

        caseUIDeviceOrientationFaceUp:

        caseUIDeviceOrientationFaceDown:

        caseUIDeviceOrientationUnknown:

            // When in doubt, stay the same.

            orient = fromInterfaceOrientation;

            break;

    }

    videoCamera.outputImageOrientation = orient;

    

}

 

 

 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    returnYES; // Support all orientations.

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值