2014-10-23 17 views
5

Tôi đang sử dụng CoreImage Framework để phát hiện danh thiếp. Khi tôi phát hiện một hình chữ nhật (CIDetectorTypeRectangle) tôi vẽ một lớp phủ bằng cách sử dụng phương pháp này:iOS 8 Core Image lưu một phần của một hình ảnh qua Swift

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) 
    } 

Bây giờ tôi cần phải tự động chụp ảnh khu vực đã chọn và lưu nó. Có ai biết cách làm điều đó không? Cảm ơn!

+1

Sử dụng CGImageCreateWithImageInRect để cắt nó vào phần chính xác mà bạn muốn – Sandeep

+0

Cám ơn câu hỏi! Bây giờ tôi biết cách cắt CIImage trong Swift. Google từ chối giúp đỡ trang web của Apple. Tôi đã cố gắng sử dụng các bộ lọc khi bạn có thể dễ dàng gọi phương thức imageByCroppingToRect :) – alexsalo

Trả lời

1

Tôi đã tìm thấy giải pháp!

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 
    } 

Cách sử dụng:

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



override func viewDidLoad() { 
     super.viewDidLoad() 

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

     ... 
} 

Khi một hình chữ nhật được phát hiện:

//... 

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) 
+1

Không chắc chắn liệu bạn có đang làm điều này hay không, nhưng nếu bạn muốn Core Image cũng thẳng hàng ảnh, bạn có thể sử dụng bộ lọc CIPerspectiveCorrection. Hoạt động tuyệt vời. –

+0

@Ale Tính năng nào trong giải pháp của bạn? –

+0

Nhưng loại cây này thành hình chữ nhật. Bất kỳ ý tưởng làm thế nào để cắt để tính năng điểm? Tính năng @AlokNair là đối tượng được phát hiện từ CIDetector. –

Các vấn đề liên quan