iPhone ImageMagick Library - 使用MagickWand API从批处理文件脚本转换为Objective-C [英] iPhone ImageMagick Library - converting from batch file script to Objective-C using MagickWand API

查看:121
本文介绍了iPhone ImageMagick Library - 使用MagickWand API从批处理文件脚本转换为Objective-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定使用以下ImageMagick库进行iPhone开发:

I've decided to use the following ImageMagick library for iPhone development:

https://github.com/marforic/imagemagick_lib_iphone

效果很好。示例项目编译得很好,但没有任何意义。

It works quite well. The sample project compiles just fine, but doesn't make any sense whatsoever.

通常,我会使用 convert.exe 随ImageMagick for Windows一起通过命令行转换图像。所以我会写一个小批量文件,如下所示:

Usually, I'll use the "convert.exe" that comes with ImageMagick for Windows to convert images via the command line. So I'll write a little batch file like the following:

@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

问题在于转换以上批处理文件与该特定库一起使用。

The problem is converting the above batch file to be used alongside that particular library.

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

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

The problem is that status is always MagickFalse, which means it throws an exception. I'm also not sure whether I'm using ConvertImageCommand() in the correct way either.

任何帮助我将不胜感激。在此先感谢。

Any help would be greatly appreciated. Thanks in advance.

推荐答案

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

I ended up using MagickCommandGenesis() to achieve the desired result.

这篇关于iPhone ImageMagick Library - 使用MagickWand API从批处理文件脚本转换为Objective-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆