2014-10-23 10 views
5

Kartvizit algılamak için CoreImage Framework kullanıyorum. Ben bir dikdörtgen tespit ederlerse (CIDetectorTypeRectangle) Ben bu yöntemi kullanarak bir bindirme çizmek: iOS 8 Core Görüntü bir Resim'nin bir bölümünü Swift aracılığıyla kaydetme

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

Şimdi otomatik almak seçilen alanın bir resim ve kaydedin gerekir. Bunu yapmayı bilen var mı? Teşekkürler!

+1

Kullanım CGImageCreateWithImageInRect Eğer soru için – Sandeep

+0

Teşekkür tam olarak istediğiniz kısmına bunu kırpmak: bir dikdörtgen algılandığında

//Variables var context: CIContext! var orientation: UIImageOrientation = UIImageOrientation.Right override func viewDidLoad() { super.viewDidLoad() //Initialize the CIContext context = CIContext(options:nil) ... } 

! Şimdi CIImage'ı Swift'de nasıl ekleyeceğimi biliyorum. Google, Apple'ın sitesini de yardım etmek için reddetti. ImageByCroppingToRect :) yöntemini kolayca arayabildiğinizde filtreleri kullanmaya çalıştım. – alexsalo

cevap

1

Çözümü buldum!

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 
    } 

Kullanımı:

//... 

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

Bunu yaptığınızdan emin değilsiniz, ancak Core Image'ın görüntüyü düzeltmesini istiyorsanız, CIPerspectiveCorrection filtresini kullanabilirsiniz. Harika çalışıyor. –

+0

@Ale Çözümünüzde özellik nedir? –

+0

Ancak bu ekinler dikdörtgendir. Unsurları nasıl kırpacağınız hakkında bir fikrin var mı? @AlokNair özelliği CIDetector'dan algılanan nesnedir. –