繁体   English   中英

ios - ImageMagick如何编写代码以在图像上应用ShepardsDistortion

[英]ios - ImageMagick How to code to apply ShepardsDistortion on image

我是ImageMagick的新手,我想在源图像上开发ShepardsDistortion的效果。 我经历了很多帖子和网站,但我没有找到在iOS中实现“ShepardsDistortion”的方法。

    MagickWand *mw = NewMagickWand();
    MagickSetFormat(mw, "png");

    UIImage *sourceImage=[_sourceImgView image];
    NSData *imgData=UIImagePNGRepresentation(sourceImage);
    MagickReadImageBlob(mw, [imgData bytes], [imgData length]);
    Image *image=GetImageFromMagickWand(mw);

    DistortImage(image, ShepardsDistortion, , ,);

我做到了这一点,但我不知道在DitortImage()中传递什么作为arg。 所以,如果有人知道,那么帮助我。

编辑:

-(void)distortImage{

    MagickWandGenesis();
    MagickWand * wand;
    MagickBooleanType status;

    wand = NewMagickWand();
    MagickSetFormat(wand, "png");
    status = MagickReadImage(wand,"chess.png");

    // Arguments for Shepards
    double points[8];
    points[0] = 250; // First X point (starting)
    points[1] = 250; // First Y point (starting)
    points[2] =  50; // First X point (ending)
    points[3] = 150; // First Y point (ending)
    points[4] = 500; // Second X point (starting)
    points[5] = 380; // Second Y point (starting)
    points[6] = 600; // Second X point (ending)
    points[7] = 460; // Second Y point (ending)


    MagickDistortImage(wand,ShepardsDistortion,8,points,MagickFalse);
    NSString * tempFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"out.png"];
    MagickWriteImage(wand,[tempFilePath cStringUsingEncoding:NSASCIIStringEncoding]);
    UIImage * imgObj = [UIImage imageWithContentsOfFile:tempFilePath];
    _resultImgView.image=imgObj;

//    
//    unsigned char * cBlob;
//    size_t data_size;
//    cBlob = MagickGetImageBlob(wand, &data_size);
//    NSData * nsBlob = [NSData dataWithBytes:cBlob length:data_size];
//    UIImage *uiImage = [UIImage imageWithData:nsBlob];
//    _resultImgView.image=uiImage;

    MagickWriteImage(wand,"out.png");
    wand=DestroyMagickWand(wand);
    MagickWandTerminus();
}

这可能有所帮助:

MagickWandGenesis();
magick_wand = NewMagickWand();
double points[24];
points[0] = 250; 
points[1] = 250; 
points[2] =  50; 
points[3] = 150; 
points[4] = 0; 
points[5] = 0; 
points[6] =  0; 
points[7] = 0; 
points[8] = self.frame.width; 
points[9] = 0; 
points[10] = self.frame.width; 
points[11] = 0; 
points[12] = self.frame.width; 
points[13] = self.frame.height; 
points[14] = self.frame.width; 
points[15] = self.frame.height; 
points[16] = self.frame.width; 
points[17] = self.frame.height; 
points[18] = self.frame.width; 
points[19] = self.frame.height;
points[20] = 0; 
points[21] = self.frame.height; 
points[22] = 0; 
points[23] = self.frame.height;  
NSData * dataObject = UIImagePNGRepresentation([UIImage imageNamed:@"Imagemagick-logo.png"]);//UIImageJPEGRepresentation([imageViewButton imageForState:UIControlStateNormal], 90);
MagickBooleanType status;
status = MagickReadImageBlob(magick_wand, [dataObject bytes], [dataObject length]);
if (status == MagickFalse) {
    ThrowWandException(magick_wand);
}

// posterize the image, this filter uses a configuration file, that means that everything in IM should be working great
status =  MagickDistortImage(magick_wand,ShepardsDistortion,24,points,MagickFalse);

//status = MagickOrderedPosterizeImage(magick_wand, "h8x8o");
if (status == MagickFalse) {
    ThrowWandException(magick_wand);
}

size_t my_size;
unsigned char * my_image = MagickGetImageBlob(magick_wand, &my_size);
NSData * data = [[NSData alloc] initWithBytes:my_image length:my_size];
free(my_image);
magick_wand = DestroyMagickWand(magick_wand);
MagickWandTerminus();
UIImage * image = [[UIImage alloc] initWithData:data];
[data release];

[imageViewButton setImage:image forState:UIControlStateNormal];
[image release];

参数作为双精度列表的开头和列表的大小信息传递给DistortImage。 例:

size_t SizeOfPoints = 8;
double Points[SizeOfPoints];
DistortImage(image,
             ShepardsDistoration,
             SizeOfPoints,
             Points,
             MagickFalse,
             NULL
            );

在你的榜样,你似乎是混合MagickWandMagickCore方法; 这似乎是不必要和令人困惑的。 我会保持这种失真简单,只使用MagickWandMagickDistortImage方法。 这是的一个例子

int main(int argc. const char **argv)
{
  MagickWandGenesis();
  MagickWand * wand;
  MagickBooleanType status;

  wand = NewMagickWand();
  status = MagickReadImage(wand,"logo:");

  // Arguments for Shepards
  double points[8];
  // 250x250 -> 50x150
  points[0] = 250; // First X point (starting)
  points[1] = 250; // First Y point (starting)
  points[2] =  50; // First X point (ending)
  points[3] = 150; // First Y point (ending)
  // 500x380 -> 600x460
  points[4] = 500; // Second X point (starting)
  points[5] = 380; // Second Y point (starting)
  points[6] = 600; // Second X point (ending)
  points[7] = 460; // Second Y point (ending)

  MagickDistortImage(wand,ShepardsDistortion,8,points,MagickFalse);
  MagickWriteImage(wand,"out.png");

  wand=DestroyMagickWand(wand);
  MagickWandTerminus();

  return 0;
}

导致扭曲的翻译图像( 细节

ShepardsDistortion示例

编辑

对于iOS,您可以使用NSTemporaryDirectory (如本答案中所述 ),或使用NSData动态创建图像(如本问题中所述 )。

临时路径示例:

NSString * tempFilePath = [NSTemporaryDirectory() 
                             stringByAppendingPathComponent:@"out.png"];
MagickWriteImage(self.wand,
    [tempFilePath cStringUsingEncoding:NSASCIIStringEncoding]);
UIImage * imgObj = [UIImage imageWithContentsOfFile:tempFilePath];

以及NSData + blob的示例

unsigned char * cBlob;
size_t data_size;
cBlob = MagickGetImageBlob(wand, &data_size);
NSData * nsBlob = [NSData dataWithBytes:cBlob length:data_size];
UIImage * uiImage = [UIImage imageWithData:nsBlob];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM