2017-01-30 5 views
0

J'essaye de créer une approche purement JXA-ObjC pour obtenir des couleurs de pixel des chemins d'image. Voici ce que j'ai actuellement:img.lockFocus est inaccessible via le pont ObjC?

ObjC.import('Foundation') 
ObjC.import('AppKit') 
var c_filePath = $(picturePath) 
var c_img = $.NSImage.alloc.initWithContentsOfFile(c_filePath) 
if(c_img==$()){ 
    return [] 
} 
var c_point = $.NSMakePoint(x,y) 
c_img.lockFocus()  //Error - Undefined is not a function...? 
var c_color = NSReadPixel(c_point) 
c_img.unlockFocus() //Error - Undefined is not a function...? 
c_img.release() 

var r; var g; var b; var a 
c_img.getRegGreenBlueAlpha($(r),$(g),$(b),$(a)) 

r = ObjC.unwrap(r) 
g = ObjC.unwrap(g) 
b = ObjC.unwrap(b) 
a = ObjC.unwrap(a) 

Ce code est fortement basé sur des the code found here. Cependant, comme indiqué ci-dessus, c_img.lockFocus() est indéfini selon JXA. Curieusement, je peux avoir accès à c_img.lockFocusFlipped(), mais je ne suis pas sûr de savoir comment l'utiliser et/ou s'il peut être utilisé dans le même but que lockFocus().

Y at-il un problème évident ici? Ou y a-t-il un meilleur moyen d'obtenir la couleur de pixel d'une image?

Toute aide serait reconnaissante.

Répondre

0

Il semble que je sois trop habitué aux méthodes nécessitant une parenthèse. TylerGaw cependant m'a dit que ce n'est pas forcément le cas.

ObjC.import('Foundation') 
ObjC.import('AppKit') 
var c_filePath = $(picturePath) 
var c_img = $.NSImage.alloc.initWithContentsOfFile(c_filePath) 
if(c_img==$()){ 
    return [] 
} 
var c_point = $.NSMakePoint(x,y) 
c_img.lockFocus 
var c_color = NSReadPixel(c_point) 
c_img.unlockFocus 
c_img.release 

semble fonctionner comme prévu.