获取当前显示屏幕某一点的颜色
获取当前显示屏幕某一点的颜色
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, BlueCGContextRef 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 componentbitmapBytesPerRow,colorSpace,kCGImageAlphaPremultipliedFirst);if (context == NULL){free (bitmapData);fprintf (stderr, "Context not created!");}// Make sure and release colorspace before returningCGColorSpaceRelease( 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颜色确定状态栏背景颜色,保持一致风格
获取当前显示屏幕某一点的颜色相关推荐
- 利用windows 系统的画图工具获取图片上某一点的颜色RGB值
今天编写程序时,想模仿一款软件.包括外观颜色都要求很像,但是总是找不到一个与之相似的颜色,后来就想到了可以先获取RGB三色值,然后直接给控件或者窗体的背景赋值.再网上找到很多获取RGB值得工具,但是都 ...
- 利用OpenCV获取图片上某一点的颜色HSV值
在进行图像处理时我们经常需要根据图像的颜色提取特定的区域处理,很多时候 RGB 是不适合的,我们需要知道感兴趣区域的区域HSV各分量的范围. RGB 颜色使用 QQ 截图工具或者 Windows 画图 ...
- android 使用Photoshop获取图片某一点的颜色
今天,简单讲讲android里如何使用Photoshop获取图片某点的像素. 之前,做界面时需要模仿一款app的界面,所以需要获取界面的颜色值在app里使用.其实使用颜色截取器也可以,也很简单.但是但 ...
- 获取图片上任意一点的颜色值
点击屏幕上任意一点,获取点击处的颜色值(RGB),还可以将十进制颜色值转化为十六进制颜色值显示,下面是个程序的样例: package com.junto.text;import android.app ...
- (转)创建X509证书,并获取证书密钥的一点研究
创建X509证书,并获取证书密钥的一点研究 作者:肖波 个人博客:http://blog.csdn.net/eaglet ; http://www.cnblogs.com/eaglet 2007/7 ...
- C++关于利用_kbhit()函数持续获取键盘输入的一点心得。
#C++关于利用_kbhit()函数持续获取键盘输入的一点心得. 小白路过想写个贪吃蛇玩玩,当用链表把贪吃蛇结构连起来后自然就想到了移动控制的问题.写来写去代码总是出错,贪吃蛇只有一直按着方向键才能移 ...
- PCL:点云赋色 | 自定义点云中任意一点的颜色
文章目录 1 PCL中PointXYZRGB类型的点云 1.1 PCL中PointXYZRGB类型点云的r\g\b类型 1.2 PCL中PointXYZRGB类型点云的r\g\b的数值范围 2 自定义 ...
- java获取鼠标在屏幕位置的颜色RGB值
java获取鼠标在屏幕位置的颜色RGB值 import java.awt.AWTException; import java.awt.AWTKeyStroke; import java.awt.Col ...
- 【PyAutoGUI操作指南】05 屏幕截图与图像定位:截图+定位单个目标+定位全部目标+灰度匹配+像素匹配+获取屏幕截图中像素的RGB颜色
6 屏幕截图与图像定位 PyAutoGUI可以拍摄屏幕截图,将其保存到文件中,并在屏幕中定位图像.OSX使用操作系统附带的screencapture命令.Linux使用scrot命令,可以通过运行su ...
最新文章
- TreeSet集合(自然排序和比较器排序)
- 用ruby的net/ssh链接远程的服务器
- NSIS 的 Modern UI 教程(二)
- CPP第四版第五章:位操作符、sizeof及部分编程习题
- lua ue_slua unreal分析(二)LuaActor与lua表互访
- session同步问题
- 凭借左程云(左神)的这份 “程序员代码面试指南”我入职了字节
- J2EE快速入门之集合框架【01】
- 《手机传感器》参数与选择
- python opencv Shi-Tomasi 角点检测和特征跟踪
- android如何正确获取iccid,特别是中间带有字母的iccid
- 用Python爬取某网中小说内容代码分享
- Lora技术- 码片/码元/数据速率以及空中时间计算
- 软件工程之软件过程模型
- FPGA国内外发展现状
- 《Python 源码剖析》一些理解以及勘误笔记(3)
- BERT用的LayerNorm可能不是你认为的那个Layer Norm?
- 【Linux 内核 内存管理】内存映射原理 ① ( 物理地址空间 | 外围设备寄存器 | 外围设备寄存器的物理地址 映射到 虚拟地址空间 )
- Java系列--总纲
- Android基于G-Sensor的计步算法
热门文章
- CODESYS在智能生产线中的应用案例:使用机器人全自动组装LeanMotor转子
- 支付宝亿万级流量的高可用架构
- numpy 基础教程【清晰详细带思维导图】
- 21点代码python_python实现一个简单的21点游戏
- 【三星官方教程】如何为Gear VR 开发应用(二):创建Unity项目
- Android开发——短信电话拦截/接听电话
- 电脑语音识别软件_语音识别软件电脑版_电脑语音识别输入法 - 云+社区 - 腾讯云...
- VS2015+WDK 配置驱动开发环境
- python:打印九九乘法表、直角三角形、等腰三角形、正方形
- 利用键盘选定文本的快捷键