gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 08:07:15 24 4
gpt4 key购买 nike

我正在使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com