how to add watermark on a exist image

前端 未结 4 500
有刺的猬
有刺的猬 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: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> 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...

提交回复
热议问题