如何在现有的图片上添加水印

3
我找到了以下代码:

我发现了一些代码:

 UIGraphicsBeginImageContext(CGSizeMake(320, 480));
// This is where we resize captured image
[(UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage] drawInRect:CGRectMake(0, 0, 320, 480)];
// And add the watermark on top of it
[[UIImage imageNamed:@"Watermark.png"] drawAtPoint:CGPointMake(0, 0) blendMode:kCGBlendModeNormal alpha:WATERMARK_ALPHA];
// Save the results directly to the image view property
imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

但我不确定这是否是最佳方式。


2
我发现一段代码如下: - user493435
4个回答

7

请查看CGImageCreateWithMask函数。

将现有的图像和水印图像传递给此函数。

- (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {

    CGImageRef maskRef = maskImage.CGImage; 

    CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                             CGImageGetHeight(maskRef),
                                        CGImageGetBitsPerComponent(maskRef),
                                        CGImageGetBitsPerPixel(maskRef),
                                        CGImageGetBytesPerRow(maskRef),
                                        CGImageGetDataProvider(maskRef), NULL, false);

    CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);

    return [UIImage imageWithCGImage:masked];

}

2

水印分为可见和不可见两种类型。由于您没有明确要求可见水印,我将提供一种非可见水印的解决方案。这种方法的理论很简单:取最低优先级的位并在其中添加水印。

在iPhone编程中,可以这样实现:

CGContextRef context = [self createARGBBitmapContextFromImage:yourView.image.CGImage];
unsigned char* data = CGBitmapContextGetData (context);
size_t width = CGImageGetWidth(yourView.image.CGImage);
size_t height = CGImageGetHeight(yourView.image.CGImage);
for (int y=0; y<height; y++) {
  for (int x=0; x<width; x++) {
    int pos = y*width + x;
    int argb = data[pos];
    int r = (argb >> 16) & 0xff;
    int g = (argb >>  8) & 0xff;
    int b =  argb        & 0xff;

    //add watermark to the bits with the lowest priority
    unsigned char bit1 = 0x00 , bit2 = 0x01, bit3 = 0x00;
    //example adds false, true, false to every pixel - only 0x00 and 0x01 should be used here (1 bit)
    unsigned char mask = 0x01;

    r = (r - (r & mask)) + bit1;
    g = (g - (g & mask)) + bit2;
    b = (b - (b & mask)) + bit3;
    data[pos] = (0xFF<<24) | (r<<16) | (g<<8) | b;
  }
}

编码方式完全相反 - 您可以使用此代码在图像中存储宽度*高度*3位。例如,对于一个640x480的图像,这将是996字节。 它可以每个像素存储更多的位,但在这种情况下也会失去更多的细节(然后您需要更改掩码0x01)。 Alpha通道也可以用于存储一些位 - 为了简单起见,我在这里忽略了它...


你能解释/编辑上面的代码,用于在图像中插入水印文本,然后检索该带水印的文本吗? - Khalid Usman
你能展示一下这个方法吗?createARGBBitmapContextFromImage。 - Khalid Usman
@KhalidUsman createARGBBitmapContextFromImage 是苹果公司核心图形/Quartz 2D框架的一部分。请参见此处 - Constantin

1

我认为你不需要使用库来完成这个任务。对于这么简单的事情来说,这样做太过了。至少在我看来,OmnipotentEntity 提出的库也是如此。

你正在尝试的方法很简单而且不错。 即使它不起作用,你也可以自己动手。混合是一个非常简单的算法。

来自维基百科:

alt text

其中,Co是操作的结果,Ca是元素A中像素的颜色,Cb是元素B中像素的颜色,αa和αb分别是元素A和B中像素的alpha值。
我本来想写如何访问像素,但Constantin已经完成了!

1

可以参考 http://iphonesdksnippets.com/post/2010/01/29/ImageMagick-on-iPhone.aspx 了解详情。 - OmnipotentEntity

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接