获取当前显示屏幕某一点的颜色

原创 2015年11月03日 09:57:55

获取当前显示屏幕某一点的颜色

1、 获取当前屏幕的截图

-(UIImage *)fullScreenshots{

    UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
    UIGraphicsBeginImageContext(screenWindow.frame.size);//全屏截图,包括window

    [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];

    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();


    return viewImage;

}

2、 通过图片获取点颜色

- (UIColor*) getPixelColorAtLocation:(CGPoint)point withImage:(UIImage*) image {
    UIColor* color = nil;
    CGImageRef inImage = image.CGImage;
    // Create off screen bitmap context to draw the image into. Format ARGB is 4 bytes for each pixel: Alpa, Red, Green, Blue
    CGContextRef cgctx = [self createARGBBitmapContextFromImage:inImage];
    if (cgctx == NULL) { return nil;  }

    size_t w = CGImageGetWidth(inImage);
    size_t h = CGImageGetHeight(inImage);
    CGRect rect = {{0,0},{w,h}};

    // Draw the image to the bitmap context. Once we draw, the memory
    // allocated for the context for rendering will then contain the
    // raw image data in the specified color space.
    CGContextDrawImage(cgctx, rect, inImage);

    // Now we can get a pointer to the image data associated with the bitmap
    // context.
    unsigned char* data = CGBitmapContextGetData (cgctx);
    if (data != NULL) {
        //offset locates the pixel in the data from x,y.
        //4 for 4 bytes of data per pixel, w is width of one row of data.
        @try {
            int offset = 4*((w*round(point.y))+round(point.x));
            NSLog(@"offset: %d", offset);
            int alpha =  data[offset];
            int red = data[offset+1];
            int green = data[offset+2];
            int blue = data[offset+3];
            NSLog(@"offset: %i colors: RGB A %i %i %i  %i",offset,red,green,blue,alpha);
            color = [UIColor colorWithRed:(red/255.0f) green:(green/255.0f) blue:(blue/255.0f) alpha:(alpha/255.0f)];
        }
        @catch (NSException * e) {
            NSLog(@"%@",[e reason]);
        }
        @finally {
        }

    }

    return color;
}
- (CGContextRef) createARGBBitmapContextFromImage:(CGImageRef) inImage {

    CGContextRef    context = NULL;
    CGColorSpaceRef colorSpace;
    void *          bitmapData;
    int             bitmapByteCount;
    int             bitmapBytesPerRow;

    // Get image width, height. We'll use the entire image.
    size_t pixelsWide = CGImageGetWidth(inImage);
    size_t pixelsHigh = CGImageGetHeight(inImage);

    // Declare the number of bytes per row. Each pixel in the bitmap in this
    // example is represented by 4 bytes; 8 bits each of red, green, blue, and
    // alpha.
    bitmapBytesPerRow   = (pixelsWide * 4);
    bitmapByteCount     = (bitmapBytesPerRow * pixelsHigh);

    // Use the generic RGB color space.
    colorSpace = CGColorSpaceCreateDeviceRGB();

    if (colorSpace == NULL)
    {
        fprintf(stderr, "Error allocating color spacen");
        return NULL;
    }

    // Allocate memory for image data. This is the destination in memory
    // where any drawing to the bitmap context will be rendered.
    bitmapData = malloc( bitmapByteCount );
    if (bitmapData == NULL)
    {
        fprintf (stderr, "Memory not allocated!");
        CGColorSpaceRelease( colorSpace );
        return NULL;
    }

    // Create the bitmap context. We want pre-multiplied ARGB, 8-bits
    // per component. Regardless of what the source image format is
    // (CMYK, Grayscale, and so on) it will be converted over to the format
    // specified here by CGBitmapContextCreate.
    context = CGBitmapContextCreate (bitmapData,
                                     pixelsWide,
                                     pixelsHigh,
                                     8,      // bits per component
                                     bitmapBytesPerRow,
                                     colorSpace,
                                     kCGImageAlphaPremultipliedFirst);
    if (context == NULL)
    {
        free (bitmapData);
        fprintf (stderr, "Context not created!");
    }
    // Make sure and release colorspace before returning
    CGColorSpaceRelease( colorSpace );

    return context;
}

3、 使用方法

- (void)webViewDidFinishLoad:(UIWebView *)webView {    
    if (_backView) {
        [self performSelector:@selector(changeColor) withObject:nil afterDelay:.5];
    }
}
- (void) changeColor {
    UIColor* color = [self getPixelColorAtLocation:CGPointMake(4, 22) withImage:[self fullScreenshots]];
    if (color && !CGColorEqualToColor(color.CGColor, _backView.backgroundColor.CGColor)) {
        _backView.backgroundColor = color;
    }
}

在url加载完成后,根据webview的navigationbar颜色确定状态栏背景颜色,保持一致风格

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/reylen/article/details/49612199

python 获取指定像素颜色

方法一:  # -*- coding: cp936 -*- from ctypes import * #引入winapi gdi32 = windll.gdi32 user32 = ...
  • tianyaweizhiren
  • tianyaweizhiren
  • 2012-03-14 09:24:22
  • 2254

如何获取屏幕上各颜色的红、绿、蓝值

在桌面上运行各种程序中,有很多色彩十分靓丽,我们有时也想使用它们的颜色,但苦于没有源码,所以很难准确知道它们的R、G、B颜色值。于是便产生了很多拾取屏幕颜色的工具,你想知道它们是怎么用程序实现的么?下...
  • u012187684
  • u012187684
  • 2014-05-05 09:31:56
  • 999

利用python打开摄像头及颜色检测

python-opencv实现颜色检测
  • xiao__run
  • xiao__run
  • 2017-07-29 17:05:50
  • 3084

Python(Pygame)颜色处理

像素的威力 凑近显示器,你能看到图像是由一个一个点构成,这就是像素。至于屏幕分辨率的意义,一个1280×1024的显示器,有着1310720个像素,一般的32位RGB系统,每个像素可以显示16....
  • zengxiantao1994
  • zengxiantao1994
  • 2017-03-03 11:35:59
  • 2573

颜色提取器------Palette

效果: Google提供的用来提取图像主要颜色的类,Palette从图像中提取突出的颜色,这样可以把色值赋给ActionBar、或者其他,可以让界面整个色调统一。 这个类能...
  • u011282069
  • u011282069
  • 2015-01-25 20:02:22
  • 1003

android 沉浸式状态栏(像ios那样的状态栏与应用统一颜色样式)

这个特性是andorid4.4支持的,最少要api19才可以使用。下面介绍一下使用的方法,非常得简单: [java] view plaincopy public class M...
  • jdsjlzx
  • jdsjlzx
  • 2015-07-06 20:15:51
  • 47997

P-1.4 学习使用PIL在屏幕中寻找颜色

PIL:Python Imaging Library,已经是Python平台事实上的图像处理标准库了。PIL功能非常强大,但API却非常简单易用。 Pillow : 是PIL的一个派生分支,但...
  • frankfrankflb
  • frankfrankflb
  • 2018-01-09 00:23:33
  • 116

利用QT实现截屏的四种方法

利用QT实现截屏的四种方法; 一:截取整个屏幕 (1)代码如下: void Test::slotGrabFullScreen() {     QScreen *screen = QG...
  • huangqi734044860
  • huangqi734044860
  • 2017-03-10 11:18:12
  • 2180

iOS获取UIView上某点的颜色值

项目需求中遇到获取`UIView`上某个坐标点的`RGB`颜色值的需求,现在把自己找到的解决方案简单总结记录一下,遇到了下面的情况:...
  • CaryaLiu
  • CaryaLiu
  • 2015-10-20 21:58:31
  • 6514

iOS开发 获取图片点击位置的颜色

综合了网上原有的方法,实现了获得图片点击位置的颜色的功能 @property(nonatomic,strong)UIImageView *imageView; @property(nonatomi...
  • developer_zhao
  • developer_zhao
  • 2016-02-25 13:12:01
  • 2278
收藏助手
不良信息举报
您举报文章:获取当前显示屏幕某一点的颜色
举报原因:
原因补充:

(最多只允许输入30个字)