iOS 8 Core Image通过Swift保存图像的一部分

我正在使用CoreImage Framework检测名片。当我检测到矩形(CIDetectorTypeRectangle)时,我会使用以下方法绘制一个叠加层:

    func drawOverlay(image: CIImage, topLeft: CGPoint, topRight: CGPoint, bottomLeft: CGPoint, bottomRight: CGPoint) -> CIImage {

    var overlay = CIImage(color: CIColor(red: 0, green: 0, blue: 1.0, alpha: 0.3))
    overlay = overlay.imageByCroppingToRect(image.extent())
    overlay = overlay.imageByApplyingFilter("CIPerspectiveTransformWithExtent",
      withInputParameters: [
        "inputExtent": CIVector(CGRect: image.extent()),
        "inputTopLeft": CIVector(CGPoint: topLeft),
        "inputTopRight": CIVector(CGPoint: topRight),
        "inputBottomLeft": CIVector(CGPoint: bottomLeft),
        "inputBottomRight": CIVector(CGPoint: bottomRight)
      ])
    return overlay.imageByCompositingOverImage(image)
  }

现在,我需要自动拍摄所选区域的图片并保存。有人知道怎么做吗?谢谢!

我找到了解决方案!

func cropBusinessCardForPoints(image: CIImage, topLeft: CGPoint, topRight: CGPoint, bottomLeft: CGPoint, bottomRight: CGPoint) -> CIImage {

        var businessCard: CIImage
        businessCard = image.imageByApplyingFilter("CIPerspectiveTransformWithExtent",
            withInputParameters: [
                "inputExtent": CIVector(CGRect: image.extent()),
                "inputTopLeft": CIVector(CGPoint: topLeft),
                "inputTopRight": CIVector(CGPoint: topRight),
                "inputBottomLeft": CIVector(CGPoint: bottomLeft),
                "inputBottomRight": CIVector(CGPoint: bottomRight)
            ])
        businessCard = image.imageByCroppingToRect(businessCard.extent())

        return businessCard
    }

用法:

//Variables
    var context: CIContext!
    var orientation: UIImageOrientation = UIImageOrientation.Right



override func viewDidLoad() {
        super.viewDidLoad()

        //Initialize the CIContext
        context = CIContext(options:nil)

        ...
}

当检测到矩形时:

//...

let businessCardImage = cropBusinessCardForPoints(image, topLeft: feature.topLeft, topRight: feature.topRight, bottomLeft: feature.bottomLeft, bottomRight: feature.bottomRight)

//Convert CIImage to CGImage
let cgimg = context.createCGImage(businessCardImage, fromRect: businessCardImage.extent())

//Convert CGImage to UIImage
let newImage = UIImage(CGImage: cgimg, scale:1, orientation: orientation)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Swift iOS中获取mailto链接的一部分

检查图像的一部分是否存在于另一个图像iOS中(图像处理)

具有Core Data关系的iOS 8 Swift NSFetchResult谓词无法访问内部ID

获取字符串的一部分-C#.NET Core 3.1

表单仅在 ASP.NET Core 中发布模型的一部分

EF Core DBContext仅定位数据库的一部分

LINQ查询的一部分,用于分离方法(EF Core)

EF Core 在调用中获取字符串的一部分

Java的8过滤器列表基于条件从列表中的一部分

Windows 8或Nvidia问题:屏幕的一部分淡出为灰色

在iOS中可点击图片的一部分

CTFramesetterCreateFrame在iOS中削减了NSString的一部分

如何在iOS中将字符串的一部分加粗?

iOS:将IBAction添加到UILabel的一部分

iOS 13导航栏问题-导航栏的一部分变得透明

我缺少设置iOS背景的那一部分?

如何将登录模块实现为我的 iOS 框架的一部分

金属文件作为iOS框架的一部分

iOS coredata排序描述符使用日期的一部分?

iOS UIPageViewController-仅刷屏幕的一部分

Xcode作为iOS项目的一部分自动构建测试目标

一个框架包含另一个框架或其中的一部分 SpriteKit swift iOS

iOS 8 Core蓝牙未发现外围设备

新的Core Audio API的iOS 8带来麻烦

iOS 15 Swift UI VStack 動畫不會將行作為動畫的一部分

Xcode 8 Simulator iOS 8图像变形

ASP.NET Core 2.1 文件上传 DirectoryNotFoundException:找不到路径的一部分

发布.NET Core应用程序时出现错误“找不到路径的一部分”

Entity Framework Core:使用自动生成的外键作为复合主键的一部分