我正在使用
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:上面的代码没有被测试和/或完成.这只是你应该在正确的轨道上开始的事情