2014-10-23 4 views
5

Utilizzo CoreImage Framework per il rilevamento di biglietti da visita. Quando rilevo un rettangolo (CIDetectorTypeRectangle) ho disegnare una sovrapposizione con questo metodo:Immagine di iOS 8 Core che salva una sezione di un'immagine tramite 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) 
    } 

Ora ho bisogno di prendere automaticamente una foto dell'area selezionata e salvarlo. Qualcuno sa come farlo? Grazie!

+1

Usa CGImageCreateWithImageInRect per ritagliare alla porzione esatto che si desidera – Sandeep

+0

Grazie per la domanda! Ora so come ritagliare CIImage in Swift. Google ha rifiutato di aiutare così ha fatto il sito di Apple. Ho provato ad usare i filtri quando puoi facilmente chiamare il metodo imageByCroppingToRect :) – alexsalo

risposta

1

Ho trovato la soluzione!

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 
    } 

Usage:

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



override func viewDidLoad() { 
     super.viewDidLoad() 

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

     ... 
} 

Quando viene rilevato un rettangolo:

//... 

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

Non sei sicuro se lo stai facendo, ma se vuoi che Core Image raddrizzi anche l'immagine, puoi usare il filtro CIPerspectiveCorrection. Funziona alla grande. –

+0

@Ale Qual è la funzionalità della soluzione? –

+0

Ma questo si trasforma in rettangolo. Qualche idea su come ritagliare i punti caratteristica? La funzione @AlokNair è l'oggetto rilevato da CIDetector. –