IOS Swift - 自定义相机覆盖

2024-01-08

你好,我想在我的应用程序中打开一个摄像头,如下所示

我想仅在该部分的中间打开相机,以便用户只能在矩形部分中拍摄快照

我正在使用的代码是这样的

import UIKit
import AVFoundation

class TakeProductPhotoController: UIViewController {

    let captureSession = AVCaptureSession()
    var previewLayer : AVCaptureVideoPreviewLayer?

    // If we find a device we'll store it here for later use
    var captureDevice : AVCaptureDevice?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view, typically from a nib.
        captureSession.sessionPreset = AVCaptureSessionPresetHigh

        let devices = AVCaptureDevice.devices()

        // Loop through all the capture devices on this phone
        for device in devices {
            // Make sure this particular device supports video
            if (device.hasMediaType(AVMediaTypeVideo)) {
                // Finally check the position and confirm we've got the back camera
                if(device.position == AVCaptureDevicePosition.Back) {
                    captureDevice = device as? AVCaptureDevice
                    if captureDevice != nil {
                        print("Capture device found")
                        beginSession()
                    }
                }
            }
        }

    }
    func updateDeviceSettings(focusValue : Float, isoValue : Float) {
        let error: NSErrorPointer = nil

        if let device = captureDevice {
            do {
                try captureDevice!.lockForConfiguration()

            } catch let error1 as NSError {
                error.memory = error1
            }

                device.setFocusModeLockedWithLensPosition(focusValue, completionHandler: { (time) -> Void in
                    //
                })

                // Adjust the iso to clamp between minIso and maxIso based on the active format
                let minISO = device.activeFormat.minISO
                let maxISO = device.activeFormat.maxISO
                let clampedISO = isoValue * (maxISO - minISO) + minISO

                device.setExposureModeCustomWithDuration(AVCaptureExposureDurationCurrent, ISO: clampedISO, completionHandler: { (time) -> Void in
                    //
                })

                device.unlockForConfiguration()

        }
    }

    func touchPercent(touch : UITouch) -> CGPoint {
        // Get the dimensions of the screen in points
        let screenSize = UIScreen.mainScreen().bounds.size

        // Create an empty CGPoint object set to 0, 0
        var touchPer = CGPointZero

        // Set the x and y values to be the value of the tapped position, divided by the width/height of the screen
        touchPer.x = touch.locationInView(self.view).x / screenSize.width
        touchPer.y = touch.locationInView(self.view).y / screenSize.height

        // Return the populated CGPoint
        return touchPer
    }

    func focusTo(value : Float) {
        let error: NSErrorPointer = nil


        if let device = captureDevice {
            do {
                try captureDevice!.lockForConfiguration()

            } catch let error1 as NSError {
                error.memory = error1
            }

                device.setFocusModeLockedWithLensPosition(value, completionHandler: { (time) -> Void in
                    //
                })
                device.unlockForConfiguration()

        }
    }

    let screenWidth = UIScreen.mainScreen().bounds.size.width

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        //if let touchPer = touches.first {
            let touchPer = touchPercent( touches.first! as UITouch )
         updateDeviceSettings(Float(touchPer.x), isoValue: Float(touchPer.y))


        super.touchesBegan(touches, withEvent:event)
    }

   override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
      // if let anyTouch = touches.first {
           let touchPer = touchPercent( touches.first! as UITouch )
       // let touchPercent = anyTouch.locationInView(self.view).x / screenWidth
  //      focusTo(Float(touchPercent))
    updateDeviceSettings(Float(touchPer.x), isoValue: Float(touchPer.y))

    }

    func configureDevice() {
          let error: NSErrorPointer = nil
        if let device = captureDevice {
            //device.lockForConfiguration(nil)

            do {
                try captureDevice!.lockForConfiguration()

            } catch let error1 as NSError {
                error.memory = error1
            }

            device.focusMode = .Locked
            device.unlockForConfiguration()
        }

    }

    func beginSession() {
        configureDevice()
        var err : NSError? = nil

        var deviceInput: AVCaptureDeviceInput!
        do {
            deviceInput = try AVCaptureDeviceInput(device: captureDevice)

        } catch let error as NSError {
            err = error
            deviceInput = nil
        };


        captureSession.addInput(deviceInput)

        if err != nil {
            print("error: \(err?.localizedDescription)")
        }

        previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)

        self.view.layer.addSublayer(previewLayer!)
        previewLayer?.frame = self.view.layer.frame
        captureSession.startRunning()
    }
}

在此代码中,相机占据整个屏幕。


如果您想以自定义方式启动相机UIView,你需要改变AVCaptureVideoPreviewLayer。你可以改变它的边界,它的位置,你也可以给它添加遮罩。

对于您的问题,捕获层正在全屏显示,因为您有:

 previewLayer?.frame = self.view.layer.frame

将此行更改为覆盖框架

  previewLayer?.frame = self.overLayView.layer.frame 

或者,如果您想使用原始值手动定位相机图层:

  previewLayer?.frame = CGRectMake(x,y,width,height)

另请注意,如果您想在覆盖视图中启动相机,则需要将子视图添加到该覆盖视图中

所以这一行:

     self.view.layer.addSublayer(previewLayer!)

将是这样的:

    self.overLayView.layer.addSublayer(previewLayer!)

拉伸图层/适合预览图层:

  previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)

        var bounds:CGRect
         bounds=cameraView.layer.frame;
        previewLayer!.videoGravity = AVLayerVideoGravityResizeAspectFill;
        previewLayer!.bounds=bounds;
        previewLayer!.position=CGPointMake(CGRectGetMidX(bounds), CGRectGetMidY(bounds));

        self.view.layer.addSublayer(previewLayer!)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

IOS Swift - 自定义相机覆盖 的相关文章

随机推荐

  • 从另一个类文件访问 C# 表单文本框

    我想从另一个类文件 例如chartscopier cs 访问Form1元素 但我无法从chartscopier cs更改textbox1文本 我怎样才能做到这一点 这是我的代码 Form1 cs namespace TEST public
  • 插入带有标识列的 Oracle 表时如何使用 %ROWTYPE?

    我有一个 Oracle 12c 数据库 其中有一个包含标识列的表 CREATE TABLE foo id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY bar NUMBER 现在我想使用 P
  • Android:应用程序小部件配置活动的行为与活动不同(编辑自:Android:AppWidget 未嵌入主屏幕)

    在这里编辑 下面是原始帖子 我已将原来的问题缩小为 对于常规活动和应用程序小部件配置活动 主页键的行为根本不同 我用以下最小活动进行了测试 public class TestActivity extends Activity Logger
  • 如何在 Android 中为 ExoPlayer 设计自定义搜索栏和音量控制

    我努力了this https stackoverflow com questions 24772985 how can i design the custom seekbar in android and this http www and
  • 使用ImageMagick/ZBar读取二维码

    我有扫描的图像文件 我对其进行了一些预处理 并使它们看起来像这样 我手机的 ZBar 应用程序可以很好地读取此二维码 但是zbarimg似乎想不通 我在 ImageMagick 中尝试了各种方法以使其更流畅 smooth morpholog
  • 使用 OpenSSL 库在 C++ 中生成 SHA-3 哈希值

    我广泛搜索了使用 OpenSSL 最近实现的 SHA 3 算法进行散列的代码示例 但没有找到任何代码示例 有关于 SHA 1 和 SHA 3 的代码示例 但在库文件夹中快速搜索发现 OpenSSL v1 1 1 中甚至没有 SHA3 函数名
  • 另一台计算机上的目录 - 登录凭据

    我的应用程序需要访问远程计算机上的文件 需要用户名和密码才能访问它 我试图找出目录是否存在 使用 Directory Exists 来验证我可以建立 连接 使用远程目录时有没有办法提供用户名和密码 当前存在返回 false Cheers 不
  • 如何使用维基百科的 Web API 检索某人的传记信息?

    我正在努力通过维基百科的 Web API 从该人的维基百科页面检索该人的一些特定生物详细信息 我需要检索一个人的生物信息框 我找到了如何检索内容框 介绍段落等 下面的 URL 用于检索 wiki 网页的第一个介绍段落 https en wi
  • 在 super.init 之前的表达式中使用初始化属性时出现 Swift 错误

    这并不重要 并且有解决方法 但它令人困惑 请参阅下面的最小示例 我指的是一个已初始化的属性 但在调用 super init 之前 为什么下面所示的语句会出现编译错误 在表达式的右手中使用属性与在左手中使用属性有什么特别之处吗 我浏览了 Sw
  • 在 Webfaction 上设置 Redis

    设置需要哪些步骤Redis http redis io 数据库上网派 http www webfaction com affiliate xeli共享托管帐户 介绍 由于 Webfaction 服务器的特殊环境限制 安装说明并不那么简单 尽
  • New 与 Malloc,当重载 New 时

    我超载了new and delete实现我自己的小对象 线程安全分配器 问题是当我超载时new 我不能使用new不破坏普遍因果关系或至少不破坏编译器 我发现的大多数例子都在哪里new超载 使用Malloc 进行实际分配 但根据我对 C 的理
  • 从所有表中删除外键关系

    我有一个包含多个表的数据库 许多表的字段具有外键约束 我想截断表 然后用新数据重新填充它们 并且我还想删除外键 因为某些关系已经改变 基本上 我想再次从头开始构建 FK 约束 如何从所有表中删除当前的 FK 约束 您可以使用 informa
  • PostgreSQL 9.2.1 中具有可序列化隔离的谓词锁定

    我已经仔细阅读了关于事务隔离的 postgres 文档 http www postgresql org docs current static transaction iso html建议在我的其他问题 https stackoverflo
  • 从 CompletableFuture 捕获未捕获的异常

    我正在尝试捕获像这样的期货中未捕获的异常CompletableFuture runAsync gt throw new RuntimeException 我的目标是当开发人员忘记处理这些异常时 让它们不再沉默 Calling get or
  • 如何从提交的 Spark 应用程序步骤中获取 AWS EMR 集群 id 和步骤 id

    设想 我正在 AWS EMR 中运行 Spark Scala 作业 现在我的工作转储该应用程序特有的一些元数据 现在 为了转储 我正在位置 s3 bucket key 写入 其中 ApplicationId 是 val APPLICATIO
  • 将 TableRowSorter 与 scala.swing.Table 一起使用

    我正在开发具有 scala swing Table 组件的简单 UI 我想使用 java swing table TableRowSorter 对表行进行排序 Table 类不提供任何使用行排序器的 API 因此我尝试直接在对等点上设置它
  • 了解会话中有关登录变量的行

    下面这行是什么意思 放置布尔变量isLogin到您的会话 以便您在用户每次访问安全站点时检查会话 我想知道如何将变量放入会话中 我知道在抽象层面上 会话是半永久性的 互动信息交换 也称为对话 两人之间的谈话或会议 或更多通信设备 或 计算机
  • 跨源子帧中表单控件的自动对焦被阻止

    使用 Chrome 当我尝试更改位于我们服务器上另一个应用程序的 IFrame 中的输入值时 我在 Chrome 中收到错误 在跨源子框架中阻止对表单控件的自动对焦 在生产中 当两个应用程序托管在同一域上时 它正在工作 但在本地主机开发中我
  • 如何发现 Spark 数据框中列格式的异常?

    正如问题所说 我想找到大型数据集中列中值格式的异常 例如 如果我在包含 5 亿行的数据集中有一个日期列 我想确保该列中所有行的日期格式为 MM DD YYYY 我想找到此格式中存在异常的计数和值 我该怎么做呢 我可以使用正则表达式吗 有人可
  • IOS Swift - 自定义相机覆盖

    你好 我想在我的应用程序中打开一个摄像头 如下所示 我想仅在该部分的中间打开相机 以便用户只能在矩形部分中拍摄快照 我正在使用的代码是这样的 import UIKit import AVFoundation class TakeProduc