2011-09-20 79 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的輸出應該是你期望的。