You need to enable JavaScript to run this app.
最新活动
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何将cvPixelBuffer裁剪为CGRect的边界?

要将cvPixelBuffer裁剪为CGRect的边界,您可以使用Core Graphics框架中的函数来执行此操作。以下是一个示例代码,该代码使用AVFoundation和Core Graphics来裁剪cvPixelBuffer。

import AVFoundation
import CoreGraphics
import CoreVideo

func cropPixelBuffer(_ pixelBuffer: CVPixelBuffer, toRect rect: CGRect) -> CVPixelBuffer? {
    let width = CVPixelBufferGetWidth(pixelBuffer)
    let height = CVPixelBufferGetHeight(pixelBuffer)
    
    var cropRect = rect
    cropRect.origin.x *= CGFloat(width)
    cropRect.origin.y *= CGFloat(height)
    cropRect.size.width *= CGFloat(width)
    cropRect.size.height *= CGFloat(height)
    
    var croppedPixelBuffer: CVPixelBuffer?
    
    CVPixelBufferCreate(nil, Int(cropRect.width), Int(cropRect.height), CVPixelBufferGetPixelFormatType(pixelBuffer), nil, &croppedPixelBuffer)
    
    guard let cropped = croppedPixelBuffer else {
        return nil
    }
    
    CVPixelBufferLockBaseAddress(pixelBuffer, .readOnly)
    CVPixelBufferLockBaseAddress(cropped, [])
    
    let srcBaseAddress = CVPixelBufferGetBaseAddress(pixelBuffer)
    let destBaseAddress = CVPixelBufferGetBaseAddress(cropped)
    
    CVPixelBufferFillExtendedPixels(cropped)
    
    let srcBytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)
    let destBytesPerRow = CVPixelBufferGetBytesPerRow(cropped)
    
    let srcStart = Int(cropRect.origin.y) * srcBytesPerRow + Int(cropRect.origin.x) * 4
    let destStart = 0
    
    let srcAddress = srcBaseAddress?.advanced(by: srcStart)
    let destAddress = destBaseAddress?.advanced(by: destStart)
    
    let srcBuffer = vImage_Buffer(data: srcAddress, height: vImagePixelCount(cropRect.height), width: vImagePixelCount(cropRect.width), rowBytes: srcBytesPerRow)
    let destBuffer = vImage_Buffer(data: destAddress, height: vImagePixelCount(cropRect.height), width: vImagePixelCount(cropRect.width), rowBytes: destBytesPerRow)
    
    vImageScale_ARGB8888(&srcBuffer, &destBuffer, nil, vImage_Flags(kvImageNoFlags))
    
    CVPixelBufferUnlockBaseAddress(pixelBuffer, .readOnly)
    CVPixelBufferUnlockBaseAddress(cropped, [])
    
    return cropped
}

这个函数接受一个cvPixelBuffer和一个CGRect作为参数,并返回裁剪后的cvPixelBuffer。请注意,此示例假定输入的cvPixelBuffer的像素格式为kCVPixelFormatType_32BGRA。如果您的像素格式不同,请相应地更改代码。

使用此函数,您可以将cvPixelBuffer裁剪为特定的CGRect边界。例如:

let pixelBuffer: CVPixelBuffer = // 获取输入的cvPixelBuffer
let cropRect = CGRect(x: 0.25, y: 0.25, width: 0.5, height: 0.5) // 定义要裁剪的边界

if let croppedPixelBuffer = cropPixelBuffer(pixelBuffer, toRect: cropRect) {
    // 使用裁剪后的cvPixelBuffer进行进一步处理
    // ...
} else {
    // 裁剪失败
    // ...
}

希望这可以帮助您实现将cvPixelBuffer裁剪为特定CGRect边界的功能。

本文内容通过AI工具匹配关键字智能整合而成,仅供参考,火山引擎不对内容的真实、准确或完整作任何形式的承诺。如有任何问题或意见,您可以通过联系service@volcengine.com进行反馈,火山引擎收到您的反馈后将及时答复和处理。
展开更多
面向开发者的云福利中心,ECS 60元/年,域名1元起,助力开发者快速在云上构建可靠应用

社区干货

WebRTC 如何在安卓系统上采集视频数据 | 社区征文

new GlRectDrawer());videoCapturerSurfaceTextureHelper = SurfaceTextureHelper.create("VideoCapturerThread", eglBase.getEglBaseContext());```步骤六、设置采集数据回调方法,参考代码如下:```e... (TextureBufferImpl) frame.getBuffer(), /* mirror= */ isCameraFrontFacing, /* rotation= */ -cameraOrientation), ...

特惠活动

热门爆款云服务器

100%性能独享,更高内存性能更佳,学习测试、web前端、企业应用首选,每日花费低至0.55元
60.00/1212.00/年
立即购买

域名注册服务

cn/top/com等热门域名,首年低至1元,邮箱建站必选
1.00/首年起32.00/首年起
立即购买

DCDN国内流量包100G

同时抵扣CDN与DCDN两种流量消耗,加速分发更实惠
2.00/20.00/年
立即购买

如何将cvPixelBuffer裁剪为CGRect的边界?-优选内容

进阶功能
希望将推流 SDK 作为推流引擎使用,可参考以下外部采集源推流功能的使用方式。 外部视频源 推流引擎支持传入 OpenGL 纹理 、NSData 二进制数据、CVPixelBuffer 和 CMSampleBufferRef 格式的视频数据。 说明 二进制数... 决定了如何在画布中展示视频。分为三种模式: VeLivePusherRenderModeHidden 视窗填满优先:视频帧等比缩放,填满视窗,多余部分被裁剪。 VeLivePusherRenderModeFit 视频帧内容全部显示优先:将视频尺寸等比缩放,保证视...
Android
mRTCVideo.setVideoEncoderConfig(config); 订阅 RTC 本地音视频数据。 java // 订阅本地视频数据mRTCVideo.setLocalVideoSink(StreamIndex.STREAM_INDEX_MAIN, mVideoFrameListener, IVideoSink.PixelFormat.I4... final ByteBuffer dstBuffer = ByteBuffer.allocateDirect(bufferSize); YuvHelper.I420Rotate(videoFrame.getPlaneData(0), videoFrame.getPlaneStride(0), videoFrame.getPlaneDa...
运动健身
bef_ai_pixel_format pixel_format int image_width int image_height int image_stride bef_ai_rotate_type orientation bef_ai_action_recognition_start_pose_type type bef_ai_action_recognition_start_pose_... ByteBuffer buffer BytedEffectConstants.PixlFormat pixelFormat int width int height int stride BefActionRecognitionInfo.ActionRecognitionPoseType poseType BytedEffectConstants.Rotation orientation );...
进阶功能
截图截图功能允许您在播放器中截取当前直播画面并生成一张图片。 接入说明 通过调用播放器的 snapshot 接口可以实现截图功能。截图仅在播放器播放成功后可以生效。代码示例如下所示。 objectivec int result = [se... PixelFormat VeLivePlayerPixelFormatNV12 NV12 VeLivePlayerPixelFormatI420 I420 视频数据封装格式VeLivePlayerVideoBufferType VeLivePlayerVideoBufferTypePixelBuffer CVPixelBufferRef VeLivePlayerVi...

如何将cvPixelBuffer裁剪为CGRect的边界?-相关内容

进阶功能

PixelFormat 视频帧像素格式 width int 视频帧的宽度,单位为 px height int 视频帧的高度,单位为 px pts long 视频帧的渲染时间戳,单位为 ms texture VeLivePlayerVideoTexture 当 bufferType 为 VeLivePlayerVideoBufferTypeTexture 时的视频数据,承载用于 OpenGL 渲染的纹理数据 buffer ByteBufferbufferType 为 VeLivePlayerVideoBufferTypeByteBuffer 时的视频数据,承载用于 JNI 层的 Direct Buffer data byte[] 当 buff...

iOS(v4.1.0.0及以上)

它只能处理 2D 纹理并将结果输出到传入的 2D 纹理 outputTexture 中,如果当前的项目中无法直接拿到 2D 纹理,可以先试用 BEImageUtils 进行格式转换,这个类可以进行 CVPixelBuffer/Texture/Buffer 几种数据的转换,可... 如果自己项目中暂时没有将 CV SDK 处理后的图像绘制到屏幕上的方法,可以使用这个类。然后通过下面的方法绘制纹理: objc - (void)renderWithTexture:(unsigned int)name size:(CGSize)size ...

类型详情

音量随距离增大进行指数衰减 RTCVideoEngineParams csharp public struct bytertc.RTCVideoEngineParams引擎初始化参数 Defined in : IRTCVideo.cs 成员变量类型 名称 AppID 应用 ID。 Params 初始化参数。 AudioP... 为保证图片内容不变形,图片按短边缩放至与视频帧一致,使图片填满视频帧,对多出的高或宽进行剪裁。+ 自定义图片带有局部透明效果时,透明部分由黑色代替。 ZoomConfigType csharp public enum bytertc.ZoomConfigTyp...

热门爆款云服务器

100%性能独享,更高内存性能更佳,学习测试、web前端、企业应用首选,每日花费低至0.55元
60.00/1212.00/年
立即购买

域名注册服务

cn/top/com等热门域名,首年低至1元,邮箱建站必选
1.00/首年起32.00/首年起
立即购买

DCDN国内流量包100G

同时抵扣CDN与DCDN两种流量消耗,加速分发更实惠
2.00/20.00/年
立即购买

API 详情

CVPixelBufferRef copyPixelBuffer BOOL isSupportHDR BOOL isSupportSR void setFFmpegProtocol: void snapshot: 变量说明 playbackSpeedobjectivec @property (nonatomic, assign) CGFloat playbackSpeed;设置播... 静态函数返回 名称 BOOL debugToolIsShowing void showDebugTool: void showDebugTool:inRect: void refreshCurrentVideoEngine: void removeDebugTool 函数说明 debugToolIsShowingobjectivec + (BOOL)debugTool...

雪碧图

Interval 为 10,表示每 10 秒截取一张缩略小图,一共截取 27 张,由 CaptureNum 表示。// 每张雪碧大图包含 5*5 = 25 张缩略小图,所以需要 2 张雪碧大图。// 两张雪碧大图的 URL 可从 StoreUrls 参数中获取。[{ ... // 缩略小图总个数。@property (nonatomic, assign) NSInteger imageNum; // 每张缩略小图的宽,单位为 pixel。@property (nonatomic, assign) NSInteger imageXSize;// 每张缩略小图的高,单位为 pixel。@property ...

Native 端自定义视频渲染

@end@implementation CustomVideoRenderView/** * 视频帧回调 * @param pixelBuffer 视频的 PixelBuffer * @param rotation 视频旋转角度,参看 ByteRTCVideoRotation * @param contentType 视频内部类型 参看 Byte... CVImageBuffer:pixelBuffer]]; imageView.contentMode = UIViewContentModeScaleAspectFill; switch (rotation) { case VideoRotation_0: imageView.transform = CGAffin...

WebRTC 如何在安卓系统上采集视频数据 | 社区征文

new GlRectDrawer());videoCapturerSurfaceTextureHelper = SurfaceTextureHelper.create("VideoCapturerThread", eglBase.getEglBaseContext());```步骤六、设置采集数据回调方法,参考代码如下:```e... (TextureBufferImpl) frame.getBuffer(), /* mirror= */ isCameraFrontFacing, /* rotation= */ -cameraOrientation), ...

人脸检测与分析

buffer, BEF_AI_PIX_FMT_BGRA8888, width, height, ... bef_ai_pixel_format pixel_format, int image_width, int image_height, int image_stride, bef_ai_rotate_type orientation, unsigned long long detect_config, bef_ai_face_info *p_face_info);参数说明 参数名...

接口说明-特效

因此我们建议使用glTexture2D作为输入,从而减少数据在CPU内存和显存中进出导致的时间损耗;如果使用其他格式的数据作为输入,我们在底层使用Shader完成buffer数据向纹理的转换,再执行特效处理,这种方式会增加性能开销... bef_ai_pixel_format fmt_in, int image_width, int image_height, int image_stride, double timestamp);参数说明 参数名 参数类型 参数说明 handle bef_effect_handle_t 已...

特惠活动

热门爆款云服务器

100%性能独享,更高内存性能更佳,学习测试、web前端、企业应用首选,每日花费低至0.55元
60.00/1212.00/年
立即购买

域名注册服务

cn/top/com等热门域名,首年低至1元,邮箱建站必选
1.00/首年起32.00/首年起
立即购买

DCDN国内流量包100G

同时抵扣CDN与DCDN两种流量消耗,加速分发更实惠
2.00/20.00/年
立即购买

产品体验

体验中心

云服务器特惠

云服务器
云服务器ECS新人特惠
立即抢购

白皮书

一图详解大模型
浓缩大模型架构,厘清生产和应用链路关系
立即获取

最新活动

爆款1核2G共享型服务器

首年60元,每月仅需5元,限量秒杀
立即抢购

火山引擎增长体验专区

丰富能力激励企业快速增长
查看详情

数据智能VeDI

易用的高性能大数据产品家族
了解详情

一键开启云上增长新空间

立即咨询