簡體   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