-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIColor+CheckBorderColor.m
362 lines (220 loc) · 10.9 KB
/
UIColor+CheckBorderColor.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//
// UIColor+CheckBorderColor.m
// CeckColor
//
// Created by 美迅 on 16/9/21.
// Copyright © 2016年 ShouQu. All rights reserved.
//
#import "UIColor+CheckBorderColor.h"
@implementation UIColor (CheckBorderColor)
+ (BOOL) checkBorderWhite:(UIImage*) image
{
// 分配内存
const int imageWidth = image.size.width;
const int imageHeight = image.size.height;
size_t bytesPerRow = imageWidth * 4;
uint32_t* rgbImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight);
// 创建context
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpace,
kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), image.CGImage);
// 遍历像素
int pixelNum = imageWidth * imageHeight;
uint32_t* pCurPtr = rgbImageBuf;
BOOL colorBol = false;
for (int i = 0; i < pixelNum; i++, pCurPtr++)
{
if (i>imageWidth&&i<pixelNum-imageWidth) {
continue;
}
// //去除白色...将0xFFFFFF00换成其它颜色也可以替换其他颜色。
// if ((*pCurPtr & 0xFFFFFF00) >= 0xffffff00) {
//
// uint8_t* ptr = (uint8_t*)pCurPtr;
// ptr[0] = 0;
// }
//接近白色
//将像素点转成子节数组来表示---第一个表示透明度即ARGB这种表示方式。ptr[0]:透明度,ptr[1]:R,ptr[2]:G,ptr[3]:B
//分别取出RGB值后。进行判断需不需要设成透明。
uint8_t* ptr = (uint8_t*)pCurPtr;
if (ptr[1] > 240 && ptr[2] > 240 && ptr[3] > 240) {
//当RGB值都大于240则比较接近白色的都将透明度设为0.-----即接近白色的都设置为透明。某些白色背景具有杂质就会去不干净,用这个方法可以去干净
colorBol = YES;
}else{
colorBol = NO;
}
}
return colorBol;
// // 将内存转成image
//
// CGDataProviderRef dataProvider =CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight, nil);
//
//
//
// CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight,8, 32, bytesPerRow, colorSpace,
//
// kCGImageAlphaLast |kCGBitmapByteOrder32Little, dataProvider,
//
// NULL, true,kCGRenderingIntentDefault);
//
// CGDataProviderRelease(dataProvider);
//
// UIImage* resultUIImage = [UIImage imageWithCGImage:imageRef];
//
// // 释放
//
// CGImageRelease(imageRef);
//
// CGContextRelease(context);
//
// CGColorSpaceRelease(colorSpace);
}
+ (UIImage *)getBorderWhite:(UIImage*) image
{
NSMutableArray * arry = [[NSMutableArray alloc]init];
// 分配内存
const int imageWidth = image.size.width;
const int imageHeight = image.size.height;
size_t bytesPerRow = imageWidth * 4;
uint32_t* rgbImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight);
unsigned char *data = calloc(imageWidth * imageHeight * 4, sizeof(unsigned char)); // 取图片首地址
// 创建context
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(rgbImageBuf, imageWidth, imageHeight, 8, bytesPerRow, colorSpace,
kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), image.CGImage);
// 遍历像素
int pixelNum = imageWidth * imageHeight;
uint32_t* pCurPtr = rgbImageBuf;
for (int i = 0; i < pixelNum; i++, pCurPtr++)
{
// if (i>imageWidth&&i<pixelNum-imageWidth) {
// continue;
// }
// //去除白色...将0xFFFFFF00换成其它颜色也可以替换其他颜色。
// if ((*pCurPtr & 0xFFFFFF00) >= 0xffffff00) {
//
// uint8_t* ptr = (uint8_t*)pCurPtr;
// ptr[0] = 0;
// }
//接近白色
//将像素点转成子节数组来表示---第一个表示透明度即ARGB这种表示方式。ptr[0]:透明度,ptr[1]:R,ptr[2]:G,ptr[3]:B
//分别取出RGB值后。进行判断需不需要设成透明。
uint8_t* ptr = (uint8_t*)pCurPtr;
NSLog(@"%d %d %d",ptr[1],ptr[2],ptr[3]);
if ((ptr[1] < 55 && ptr[2] < 55 && ptr[3] < 55)||(ptr[1] < 70 && ptr[2] < 55 && ptr[3] < 55)) {
uint8_t* ptr2 = ptr +10 ;
ptr[3] = ptr2[3];
ptr[2] =ptr2[2];
ptr[1] = ptr2[1];
// ptr[0]=0;
//当RGB值都大于240则比较接近白色的都将透明度设为0.-----即接近白色的都设置为透明。某些白色背景具有杂质就会去不干净,用这个方法可以去干净
// if (i>0) {
//
// uint8_t* ptr = (uint8_t*)pCurPtr-1;
// if (ptr[1] == 255 && ptr[2] == 255 && ptr[3] ==255) {
//
// CGFloat x = (int)i/imageWidth;
// CGFloat y = (int)i%imageWidth;
//
// CGPoint point = CGPointMake(x, y);
//
// NSValue * value = [NSValue valueWithCGPoint:point];
//
// [arry addObject:value];
//
//
// }
//
//
//
// }
//
//
// }else{
//
//
//
}
}
// return arry;
// // 将内存转成image
//
CGDataProviderRef dataProvider =CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight, nil);
CGImageRef imageRef = CGImageCreate(imageWidth, imageHeight,8, 32, bytesPerRow, colorSpace,
kCGImageAlphaLast |kCGBitmapByteOrder32Little, dataProvider,
NULL, true,kCGRenderingIntentDefault);
CGDataProviderRelease(dataProvider);
UIImage* resultUIImage = [UIImage imageWithCGImage:imageRef];
// 释放
CGImageRelease(imageRef);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return resultUIImage;
}
+ (UIImage *)drawImage:(UIImage *)image
{
CGImageRef cgimage = [image CGImage];
size_t width = CGImageGetWidth(cgimage); // 图片宽度
size_t height = CGImageGetHeight(cgimage); // 图片高度
unsigned char *data = calloc(width * height * 4, sizeof(unsigned char)); // 取图片首地址
size_t bitsPerComponent = 8; // r g b a 每个component bits数目
size_t bytesPerRow = width * 4; // 一张图片每行字节数目 (每个像素点包含r g b a 四个字节)
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); // 创建rgb颜色空间
CGContextRef context =
CGBitmapContextCreate(data,
width,
height,
bitsPerComponent,
bytesPerRow,
space,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), cgimage);
for (size_t i = 0; i < height; i++)
{
for (size_t j = 0; j < width; j++)
{
size_t pixelIndex = i * width * 4 + j * 4;
unsigned char red = data[pixelIndex];
unsigned char green = data[pixelIndex + 1];
unsigned char blue = data[pixelIndex + 2];
if ((red < 120 && green < 120 && blue < 120)||(red <55 && green < 55 && blue < 75)||(red <100 && green < 55 && blue < 50)) {
size_t pixelIndex2 = i * width * 4 + (j+35) * 4;
unsigned char red2 = data[pixelIndex2];
unsigned char green2 = data[pixelIndex2 + 1];
unsigned char blue2 = data[pixelIndex2 + 2];
if ((red2 < 90 && green2 < 90 && blue2 < 90)||(red2 <55 && green2 < 55 && blue2 < 75)||(red2 <100 && green2 < 55 && blue2 < 50)) {
size_t pixelIndex3 = i * width * 4 + (j+10) * 4;
unsigned char red3 = data[pixelIndex3];
unsigned char green3 = data[pixelIndex3 + 1];
unsigned char blue3 = data[pixelIndex3 + 2];
// 修改颜色
if ((red3 < 90 && green3 < 90 && blue3 < 90)||(red3 <55 && green3 < 55 && blue3 < 75)||(red3 <100 && green3 < 55 && blue3 < 50)) {
size_t pixelIndex4 = i * width * 4 + (j-10) * 4;
unsigned char red4 = data[pixelIndex4];
unsigned char green4 = data[pixelIndex4 + 1];
unsigned char blue4 = data[pixelIndex4 + 2];
data[pixelIndex] = red4;
data[pixelIndex+1] = green4;
data[pixelIndex+2] = blue4;
}
else{
data[pixelIndex] = red3;
data[pixelIndex+1] = green3;
data[pixelIndex+2] = blue3;
}
}else
{
// 修改颜色
data[pixelIndex] = red2;
data[pixelIndex+1] = green2;
data[pixelIndex+2] = blue2;
}
}
}
}
cgimage = CGBitmapContextCreateImage(context);
return [UIImage imageWithCGImage:cgimage];
}
@end