And*_*sen 7 imagemagick objective-c ios
我使用中引用的代码这一职位,但想切换到基于ImageMagick的C-API的解决方案,因为我想在一个单一的图像处理库规范,并且需要IM一些其他任务.
我可以找到大量使用转换命令行工具的示例,但没有关于如何在代码中进行单色转换的示例.
那里有任何示例代码?
您可以实现黑白转换,如所描述这里,与MagickQuantizeImage功能.我对抖动图像并不熟悉,但示例可能如下所示.
#include <wand/MagickWand.h>
int main(int argc, char **argv)
{
const size_t number_colors = 2;
const size_t treedepth = 1;
MagickWandGenesis();
MagickWand *wand = NULL;
wand = NewMagickWand();
MagickReadImage(wand,"source.png");
MagickQuantizeImage(
wand, // MagickWand
number_colors, // Target number colors
GRAYColorspace, // Colorspace
treedepth, // Optimal depth
MagickTrue, // Dither
MagickFalse // Quantization error
);
MagickWriteImage(wand,"out.png");
if(wand)wand = DestroyMagickWand(wand);
MagickWandTerminus();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这有时会给你一个相当斑点的图像.

调整深度,颜色编号和/或禁用抖动可能会使您的结果更接近您提供的示例所期望的结果.
MagickQuantizeImage(
wand, // MagickWand
number_colors, // Target number colors
GRAYColorspace, // Colorspace
treedepth, // Optimal depth
MagickFalse, // No-dither
MagickFalse // Quantization error
);
Run Code Online (Sandbox Code Playgroud)
像这样......

将示例代码移植到iOS不需要太多努力.NextStep/Objective-c方法与MagickWand库兼容.以下示例使用临时文件来存储单色图像,但我确信有更好的方法将Magick图像数据直接传递给UImage对象.
// MyViewController.h
#import <UIKit/UIKit.h>
#import <wand/MagickWand.h>
@interface MyViewController : UIViewController
@property (retain, nonatomic) IBOutlet UIImageView *imageView;
@property (retain, nonatomic) MagickWand *wand;
@end
// MyViewController.m
#import "MyViewController.h"
@implementation MyViewController
- (void)viewDidLoad
{
[super viewDidLoad];
MagickWandGenesis();
self.wand = NewMagickWand();
[self drawMonochromeImage:@"logo:"];
}
-(void)drawMonochromeImage:(NSString *)filePath
{
// Create temporary file
NSString *tempFilePath = [NSTemporaryDirectory()
stringByAppendingPathComponent:@"logo.jpg"];
// Read given image with C-string
MagickReadImage(self.wand,
[filePath cStringUsingEncoding:NSASCIIStringEncoding]
);
// Monochrome image
MagickQuantizeImage(self.wand,2,GRAYColorspace,1,MagickFalse,MagickFalse);
// Write to temporary file
MagickWriteImage(self.wand,
[tempFilePath cStringUsingEncoding:NSASCIIStringEncoding]
);
// Load UIImage from temporary file
UIImage *imgObj = [UIImage imageWithContentsOfFile:tempFilePath];
// Display on device
[self.imageView setImage:imgObj];
[self.imageView setContentMode:UIViewContentModeScaleAspectFit];
}
-(void)viewDidUnload
{
// Clean-up
if (self.wand)
self.wand = DestroyMagickWand(self.wand);
MagickWandTerminus();
}
@end
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
2802 次 |
| 最近记录: |