我正在使用 Swift基础的MAC OS-X应用程序.用户将选择视频文件,NSView控制器将使用AVPlayerView播放视频.用户可以在AVPlayerView上绘制一个矩形,我已经实现了这个功能.如何将矩形中的所选区域视频显示在摄像机右下角的预览图层?

这是输出:

这是代码:

var videoURL = NSURL() // The video file URL by the file chooser
var videoSize = NSSize() // Here the video resolution size will be kept i.e. 720*480
var player:AVPlayerView! // The AVPlayerView to play video

var cameraPreviewLayer:AVPlayerLayer! //The right bottom camera Preview Layer
var rectangleLayer:AVPlayerLayer! // The rectangle that will draw over AVPlayer while mouse dragging

var isClicked = false
var startPoint = NSPoint() // holds the starting point location of mouse pointer while draging

var rect:NSRect!

override func viewDidLoad() {
    super.viewDidLoad()

    self.player = AVPlayerView()

    self.player.frame.origin = CGPoint(x: 0,y: 0)
    self.player.setFrameSize(self.videoSize)

    self.player.player = AVPlayer.playerWithURL(videoURL) as! AVPlayer
    self.view.addSubview(self.player)

    setupRectangle()
    setupCameraPreviewLayer()
}

// intially setup white color rectangle that will draged over AVPlayer
func setupRectangle() {
    self.rectangleLayer = AVPlayerLayer()
    self.rectangleLayer.backgroundColor = NSColor.clearColor().CGColor
    self.rectangleLayer.borderColor = NSColor.whiteColor().CGColor
    self.rectangleLayer.borderWidth = 1.5
    self.rectangleLayer.frame = self.player.bounds
}
// intially setup CameraPreview Layer that will show at right bottom of AVPlayer
func setupCameraPreviewLayer(){
    cameraPreviewLayer = AVPlayerLayer(player: self.player.player)

    //place the cameraPreview layer at right bottom
    cameraPreviewLayer.frame.origin.x = self.player.framesize.width-100
    cameraPreviewLayer.frame.origin.y = self.player.frame.origin.y-100
    cameraPreviewLayer.frame.size.width = 100
    cameraPreviewLayer.frame.size.height = 100
}

override func  mouseDown(theEvent: NSEvent) {
    startPoint = theEvent.locationInWindow
    isClicked = true
    removeCameraPreviewLayer()
}

override func mouseDragged(theEvent: NSEvent) {
    var endPoint = theEvent.locationInWindow

    if( isClicked ){
        rect = NSRect(x: startPoint.x,y: startPoint.y,width: -(startPoint.x-endPoint.x),height: -(startPoint.y-endPoint.y))
        drawCustomrect(rect)
    }
}

override func  mouseUp(theEvent: NSEvent) {
    var endPoint = theEvent.locationInWindow

    if( isClicked ){

// I think we have to do some magic code here
        addCameraPreviewLayer()
    }
    isClicked = false;
}

// redraw the white color rectange over avplayer
func drawCustomrect(rect: NSRect) {
    self.rectangleLayer.frame = rect
    self.player.layer?.addSublayer(self.rectangleLayer)
}

// add Camera PreviewLayer from AVPlayer
func addCameraPreviewLayer() {
    self.player.layer?.addSublayer(self.layer)
}

// remove Camera PreviewLayer from AVPlayer
func removeCameraPreviewLayer() {
    self.cameraPreviewLayer.removeFromSuperlayer()
}

这是我想要的输出图像.

假设视频具有720 * 480的尺寸,用户绘制一个矩形,其点是(x1,y1)(x2,y1)(x3,y3)(x4,y4).如何在相机预览图层(右下角)裁剪视频,显示与用户选择的矩形相同的视频?

任何一个帮助如何实现这个功能?我已经花了好几天的时间和疲惫.

注意:我可以通过使用ROI的视频处理在open-CV中执行,但要求是在本地SWIFT语言中执行.

解决方法

我将做的是创建容器图层作为预览图层,并创建预览视频图层作为其子图层.然后,您基本上可以总是调整视频预览图层的大小和位置,使其与矩形/大球员的比例相匹配.

为此,您可以尝试设置相机图层:

func setupCameraPreviewLayer(){
    // Create a camera preview layer container which will be used to mask the preview layer
    let cameraContainerLayer = AVLayer()
    cameraContainerLayer.maskToBounds = true

    //place the cameraContainerLayer at right bottom
    cameraContainerLayer.frame.size.width = 100 // Change this to whatever size you'd like your preview to be. The preview layer will have the same width/height ratio as the player layer
    cameraContainerLayer.frame.size.height = cameraContainerLayer.frame.size.width * self.player.framesize.height / self.player.framesize.width
    cameraContainerLayer.frame.origin.x = self.player.frame.size.width - cameraContainerLayer.frame.size.width
    cameraContainerLayer.frame.origin.y = self.player.frame.size.height - cameraContainerLayer.frame.size.height

    // Create the camera preview layer
    cameraPreviewLayer = AVPlayerLayer(player: self.player.player)

    //place the cameraPreview layer so that the container layer has the same size ratio as does the selected rectangle to the big preview layer
    let widthRatio = self.rectangleLayer.bounds.size.width / rect.size.width // rect is the CGRect of the rectangle that the user draw
    let heightRatio = self.rectangleLayer.bounds.size.height / rect.size.height // rect is the CGRect of the rectangle that the user draw

    cameraPreviewLayer.frame.size.width = cameraContainerLayer.bounds.size.width * widthRatio
    cameraPreviewLayer.frame.size.height = cameraContainerLayer.bounds.size.height * heightRatio
    cameraPreviewLayer.frame.origin.x = //calculate the right X in here based on the widthRatio
    cameraPreviewLayer.frame.origin.y = //calculate the right X in here based on the widthRatio 

    cameraContainerLayer.addSublayer(cameraPreviewLayer)
}

这样的事情应该做的伎俩

P.S:上面的代码没有被测试和/或完成.这只是你应该在正确的轨道上开始的事情

ios – 如何在Swift AVPlayer中获取特定的视频区域?的更多相关文章

  1. 基于EJB技术的商务预订系统的开发

    用EJB结构开发的应用程序是可伸缩的、事务型的、多用户安全的。总的来说,EJB是一个组件事务监控的标准服务器端的组件模型。基于EJB技术的系统结构模型EJB结构是一个服务端组件结构,是一个层次性结构,其结构模型如图1所示。图2:商务预订系统的构架EntityBean是为了现实世界的对象建造的模型,这些对象通常是数据库的一些持久记录。

  2. js中‘!.’是什么意思

  3. InnoDB 和 MyISAM 引擎恢复数据库,使用 .frm、.ibd文件恢复数据库

  4. Error: Cannot find module ‘node:util‘问题解决

    控制台 安装 Vue-Cli 最后一步出现 Error: Cannot find module 'node:util' 问题解决方案1.问题C:\Windows\System32>cnpm install -g @vue/cli@4.0.3internal/modules/cjs/loader.js:638 throw err; &nbs

  5. yarn的安装和使用(全网最详细)

    一、yarn的简介:Yarn是facebook发布的一款取代npm的包管理工具。二、yarn的特点:速度超快。Yarn 缓存了每个下载过的包,所以再次使用时无需重复下载。 同时利用并行下载以最大化资源利用率,因此安装速度更快。超级安全。在执行代码之前,Yarn 会通过算法校验每个安装包的完整性。超级可靠。使用详细、简洁的锁文件格式和明确的安装算法,Yarn 能够保证在不同系统上无差异的工作。三、y

  6. 前端环境 本机可切换node多版本 问题源头是node使用的高版本

    前言投降投降 重头再来 重装环境 也就分分钟的事 偏要折腾 这下好了1天了 还没折腾出来问题的源头是node 使用的高版本 方案那就用 本机可切换多版本最终问题是因为nodejs的版本太高,导致的node-sass不兼容问题,我的node是v16.14.0的版本,项目中用了"node-sass": "^4.7.2"版本,无法匹配当前的node版本根据文章的提

  7. 宝塔Linux的FTP连接不上的解决方法

    宝塔Linux的FTP连接不上的解决方法常见的几个可能,建议先排查。1.注意内网IP和外网IP2.检查ftp服务是否启动 (面板首页即可看到)3.检查防火墙20端口 ftp 21端口及被动端口39000 - 40000是否放行 (如是腾讯云/阿里云等还需检查安全组)4.是否主动/被动模式都不能连接5.新建一个用户看是否能连接6.修改ftp配置文件 将ForcePassiveIP前面的#去掉 将19

  8. 扩展element-ui el-upload组件,实现复制粘贴上传图片文件,带图片预览功能

  9. 微信小程序canvas实现水平、垂直居中效果

    这篇文章主要介绍了小程序中canvas实现水平、垂直居中效果,本文图文实例代码相结合给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

  10. 使用HTML5做的导航条详细步骤

    这篇文章主要介绍了用HTML5做的导航条详细步骤,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

随机推荐

  1. iOS实现拖拽View跟随手指浮动效果

    这篇文章主要为大家详细介绍了iOS实现拖拽View跟随手指浮动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  2. iOS – genstrings:无法连接到输出目录en.lproj

    使用我桌面上的项目文件夹,我启动终端输入:cd然后将我的项目文件夹拖到终端,它给了我路径.然后我将这行代码粘贴到终端中找.-name*.m|xargsgenstrings-oen.lproj我在终端中收到此错误消息:genstrings:无法连接到输出目录en.lproj它多次打印这行,然后说我的项目是一个目录的路径?没有.strings文件.对我做错了什么的想法?

  3. iOS 7 UIButtonBarItem图像没有色调

    如何确保按钮图标采用全局色调?解决方法只是想将其转换为根注释,以便为“回答”复选标记提供更好的上下文,并提供更好的格式.我能想出这个!

  4. ios – 在自定义相机层的AVFoundation中自动对焦和自动曝光

    为AVFoundation定制图层相机创建精确的自动对焦和曝光的最佳方法是什么?

  5. ios – Xcode找不到Alamofire,错误:没有这样的模块’Alamofire’

    我正在尝试按照github(https://github.com/Alamofire/Alamofire#cocoapods)指令将Alamofire包含在我的Swift项目中.我创建了一个新项目,导航到项目目录并运行此命令sudogeminstallcocoapods.然后我面临以下错误:搜索后我设法通过运行此命令安装cocoapodssudogeminstall-n/usr/local/bin

  6. ios – 在没有iPhone6s或更新的情况下测试ARKit

    我在决定下载Xcode9之前.我想玩新的框架–ARKit.我知道要用ARKit运行app我需要一个带有A9芯片或更新版本的设备.不幸的是我有一个较旧的.我的问题是已经下载了新Xcode的人.在我的情况下有可能运行ARKit应用程序吗?那个或其他任何模拟器?任何想法或我将不得不购买新设备?解决方法任何iOS11设备都可以使用ARKit,但是具有高质量AR体验的全球跟踪功能需要使用A9或更高版本处理器的设备.使用iOS11测试版更新您的设备是必要的.

  7. 将iOS应用移植到Android

    我们制作了一个具有2000个目标c类的退出大型iOS应用程序.我想知道有一个最佳实践指南将其移植到Android?此外,由于我们的应用程序大量使用UINavigation和UIView控制器,我想知道在Android上有类似的模型和实现.谢谢到目前为止,guenter解决方法老实说,我认为你正在计划的只是制作难以维护的糟糕代码.我意识到这听起来像很多工作,但从长远来看它会更容易,我只是将应用程序的概念“移植”到android并从头开始编写.

  8. ios – 在Swift中覆盖Objective C类方法

    我是Swift的初学者,我正在尝试在Swift项目中使用JSONModel.我想从JSONModel覆盖方法keyMapper,但我没有找到如何覆盖模型类中的Objective-C类方法.该方法的签名是:我怎样才能做到这一点?解决方法您可以像覆盖实例方法一样执行此操作,但使用class关键字除外:

  9. ios – 在WKWebView中获取链接URL

    我想在WKWebView中获取tapped链接的url.链接采用自定义格式,可触发应用中的某些操作.例如HTTP://我的网站/帮助#深层链接对讲.我这样使用KVO:这在第一次点击链接时效果很好.但是,如果我连续两次点击相同的链接,它将不报告链接点击.是否有解决方法来解决这个问题,以便我可以检测每个点击并获取链接?任何关于这个的指针都会很棒!解决方法像这样更改addobserver在observeValue函数中,您可以获得两个值

  10. ios – 在Swift的UIView中找到UILabel

    我正在尝试在我的UIViewControllers的超级视图中找到我的UILabels.这是我的代码:这是在Objective-C中推荐的方式,但是在Swift中我只得到UIViews和CALayer.我肯定在提供给这个方法的视图中有UILabel.我错过了什么?我的UIViewController中的调用:解决方法使用函数式编程概念可以更轻松地实现这一目标.

返回
顶部