我正在使用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] 删除。
我来说两句