2011-09-20 16 views
1

我已经决定使用下面的ImageMagick库为iPhone开发的批处理文件脚本转换成Objective-C的:iPhone ImageMagick库 - 使用MagickWand API

https://github.com/marforic/imagemagick_lib_iphone

它工作的很好。示例项目编译得很好,但没有任何意义。

通常,我将使用ImageMagick for Windows随附的“convert.exe”通过命令行转换图像。所以,我会写一点批处理文件类似如下:

@echo off 

set SOURCE=image_to_convert.jpg 

del result.jpg 
del source_copy.jpg 

convert.exe %SOURCE% -fill "#fff8f2" -colorize 100%% fill.jpg 

copy %SOURCE% source_copy.jpg 

convert.exe %SOURCE% -modulate 100,0,100 -|^ 
convert.exe - source_copy.jpg -compose overlay -composite -|^ 
convert.exe source_copy.jpg - -compose dissolve -define compose:args=37,100 -composite -|^ 
convert.exe - -modulate 100,70,100 +level 3.5%%,100%% -|^ 
convert.exe - -channel red -level 0%%,89%% +level 9%%,100%% -|^ 
convert.exe - -channel green +level 3.5%%,100%% -|^ 
convert.exe - -channel blue -level 0%%,93%% +level 4.7%%,100%% -|^ 
convert.exe - -brightness-contrast -5x3 -|^ 
convert.exe fill.jpg - -compose multiply -gravity center -composite -|^ 
convert.exe - -level 3.5%%,100%%,0.91 +level 2.7%%,100%% -|^ 
convert.exe - -channel red +level 3.5%%,100%% -|^ 
convert.exe - -channel green -level 0%%,87%% +level 1%%,100%% -|^ 
convert.exe - -channel blue -level 0%%,100%%,0.94 +level 7%%,100%% -|^ 
convert.exe - -brightness-contrast -1x0 final_converted_image.jpg 

del source_copy.jpg 
del fill.jpg 

的问题是转换上面的批处理文件,旁边那个特定的库中使用。

- (void)convertImage { 
    MagickWandGenesis(); 
    magick_wand = NewMagickWand(); 
    //UIImageJPEGRepresentation([imageViewButton imageForState:UIControlStateNormal], 90); 
    NSData * dataObject = UIImagePNGRepresentation([UIImage imageNamed:@"iphone.png"]); 
    MagickBooleanType status; 
    status = MagickReadImageBlob(magick_wand, [dataObject bytes], [dataObject length]); 
    if (status == MagickFalse) { 
     ThrowWandException(magick_wand); 
    } 

    // Resize image. 
    ImageInfo *imageInfo = AcquireImageInfo(); 
    ExceptionInfo *exceptionInfo = AcquireExceptionInfo(); 

    // Get image from bundle. 
    char *input_image = strdup([[[NSBundle mainBundle] pathForResource:@"iphone" ofType:@"png"] UTF8String]); 
    char *output_image = strdup([[[NSBundle mainBundle] pathForResource:@"iphone" ofType:@"png"] UTF8String]); 
    char *argv[] = { "convert", input_image, "-resize", "100x100", output_image, NULL }; 

    // ConvertImageCommand(ImageInfo *, int, char **, char **, MagickExceptionInfo *); 
    status = ConvertImageCommand(imageInfo, 5, argv, NULL, exceptionInfo); 

    free(input_image); 
    free(output_image); 

    if (status == MagickFalse) { 
     ThrowWandException(magick_wand); // Always throws an exception here... 
    } 

    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]; 
} 

的问题是,status总是MagickFalse,这意味着它抛出异常。我也不确定我是否正确使用ConvertImageCommand()

任何帮助将不胜感激。提前致谢。

+0

你有没有试过为参数计数传递5?什么是例外? – Joe

回答

1

我最终使用MagickCommandGenesis()来达到预期的效果。

+2

你能分享一个例子吗? 这将是非常有用的 –

+0

我可以使用此代码将svg转换为pdf –

+0

我面对完全相同的问题,Fulvio Cusumano,请你分享一下如何使用MagickCommandGenesis()的代码? – FelipeOliveira

3

你可以找到ImageMagick的 here

该链接的使用对我来说非常有用的。

0

这些都不是正确的答案。上面的代码是完全错误的,并已复制和粘贴各地以某种形式在互联网或其他

http://www.imagemagick.org/discourse-server/viewtopic.php?t=19504 http://www.imagemagick.org/discourse-server/viewtopic.php?t=25430 iPhone ImageMagick Library - converting from batch file script to Objective-C using MagickWand API http://www.imagemagick.org/discourse-server/viewtopic.php?f=6&t=20527

不胜枚举

MagicWand是框架使用imagemagick API执行图像操作,而ConvertImageCommand直接调用convert命令的命令行界面。如果你想通过imagemagick的api来完成任务,然后使用MagicWand等,但是如果你想简单地调用命令行中使用的同一个方法,请致电ConvertImageCommand

它返回MagickFalse的事实是正确的。虽然我不声称已经阅读整个方法,知道一切它做或者为什么,如果你看一下convert.chttp://www.imagemagick.org/api/MagickWand/convert_8c_source.html,函数的最后几行是

3217 status&=WriteImages(image_info,image,argv[argc-1],exception); 
3218 if (metadata != (char **) NULL) 
3219  { 
3220  char 
3221   *text; 
3222 
3223  text=InterpretImageProperties(image_info,image,format); 
3224  if (text == (char *) NULL) 
3225   ThrowConvertException(ResourceLimitError,"MemoryAllocationFailed", 
3226   GetExceptionMessage(errno)); 
3227  (void) ConcatenateString(&(*metadata),text); 
3228  text=DestroyString(text); 
3229  } 
3230 DestroyConvert(); 
3231 return(status != 0 ? MagickTrue : MagickFalse); 
3232 } 

它说的是返回0( MagickFalse)在从命令行调用时成功,这是相当合理的。

上面的代码将没有所有的MagicWand东西就可以作为这样 -

// Resize image. 
    ImageInfo *imageInfo = AcquireImageInfo(); 
    ExceptionInfo *exceptionInfo = AcquireExceptionInfo(); 

    // Get image from bundle. 
    char *input_image = strdup([[[NSBundle mainBundle] pathForResource:@"iphone" ofType:@"png"] UTF8String]); 
    char *output_image = strdup([[[NSBundle mainBundle] pathForResource:@"iphone" ofType:@"png"] UTF8String]); 
    char *argv[] = { "convert", input_image, "-resize", "100x100", output_image, NULL }; 

    // ConvertImageCommand(ImageInfo *, int, char **, char **, MagickExceptionInfo *); 
    status = ConvertImageCommand(imageInfo, 5, argv, NULL, exceptionInfo); 

另外,如果您想直接使用API​​,那么网上很多样品并提供了如何使用示例魔法棒。

上面的代码试图混合和匹配两种不同的方法。上面的MagicWand实例没有做任何事情,如果你在上面的代码片段末尾查看NSData中的图像,它就是你开始使用的图像。 ConvertImageCommand的输出应该是你期望的。