how to add watermark on a exist image

前端 未结 4 491
有刺的猬
有刺的猬 2020-12-09 07:27

I found some code as follows:

 UIGraphicsBeginImageContext(CGSizeMake(320, 480));
// This is where we resize captured image
[(UIImage *)[info objectForKey:UI         


        
相关标签:
4条回答
  • 2020-12-09 07:36

    Check CGImageCreateWithMask.

    Pass the existing image and the watermark image to this function

    - (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];
    
    }
    
    0 讨论(0)
  • 2020-12-09 07:36

    I don't think you need a library for doing that. It is just too much for such a simple thing. At least the library proposed by OmnipotentEntity is too much in my opinion.

    The approach you are trying is simple and good. Even if it does not work you could do it yourself. Blending is a very simple algorithm.

    From WikiPedia:

    alt text

    where Co is the result of the operation, Ca is the color of the pixel in element A, Cb is the color of the pixel in element B, and αa and αb are the alpha of the pixels in elements A and B respectively

    I was going to write how to access pixels but Constantin already did it!

    0 讨论(0)
  • 2020-12-09 07:46

    There are different types of watermarking: visible and non visible watermarking. Because you didn't mentioned explicit you want a visible watermark, I will provide a solution for a non-visible watermark. The Theory of thiss kind of simple: Take the bits with the lowest priority and add your watermark there.

    In iPhone programming it would be something like this:

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

    The encoding would be vice-versa exactly the same - you could store with this code width*height*3 Bits in your image. I.e. for an 640x480 image that would be 996 Bytes It can store more bits per pixel, but will also loses more details in this case (then you need to change the mask 0x01). And the alpha channel could be also used to store a few bits as well - for simplicity I leaved that out here...

    0 讨论(0)
  • 2020-12-09 07:48

    Probably the most widely used library for this sort of thing is called Imagemagick. How to watermark.

    0 讨论(0)
提交回复
热议问题