image-processing - iOS 8 核心图像通过 Swift 保存图像的一部分

标签 image-processing swift ios8 crop core-image

我正在使用 CoreImage 框架来检测名片。当我检测到一个矩形 (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)

关于image-processing - iOS 8 核心图像通过 Swift 保存图像的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26526787/

相关文章:

xcode - 无法将带有 Storyboard 启动图像的应用程序提交到 App Store : Your binary is not optimized for iPhone 5

iOS 8 : options for presenting Action Extensions to users?

c++ - 如何在 C++ 中获取 "Padding"图像

python - 分割车牌字符

image-processing - 上传前在 DropZone 上旋转图像

swift - 分享按钮在 iPhone 上完美运行,但在 iPad 上崩溃

ios - 如何在 iOS 中的当前单元格之外进行触摸?(快速)

swift - ASPasswordCredential - 来自哪里?

java - 使用 GAE Image API 进行图像转换

iphone - ios 模拟器在 xcode 6 中出现并带有 UDID