run*_*mad 5 c objective-c uiimage cgimage ios
这是我想修剪的图像的一个例子.我想摆脱图像周围的边框(在这种情况下是顶部和底部黑条).

我在Github上找到了一个库:CKImageAdditions,但它似乎不起作用.当我传入UIColor(RGB颜色)时,它只返回相同的图像.
我可以找到许多示例和类别类来修剪UIImage,任何透明像素作为边框,但在这种情况下我需要修剪黑色.我已经对我的图像中的颜色进行了采样,它们的颜色值确实是255,但它似乎与上面的库所寻找的不匹配.
有没有人有他们使用的图书馆或任何见解?我搜索和搜索了CKImageAdditions,这是我唯一可以找到广告用颜色修剪的东西(虽然不幸的是在我的情况下不起作用).
我最终从CKImageAdditions中的一个函数定制了一个方法,该函数具有这种功能,但我无法让它工作.它只是不会修剪颜色,所以我改为检查像素的RGB值是否为\0(黑色).CKImageAdditions遗憾的是,正好找不到黑色像素.
由于我想要修剪的图像有时没有超级黑条(有时它们有一个带有较浅颜色的杂散像素或其他东西)我在方法中添加了GPUImage功能,这基本上只创建了一个黑白版本的具有强大滤镜的图像,因此任何暗色变为黑色,任何浅色变为白色,使得黑条边界更加突出,并且当我在该方法中查找它们时确保更好的结果.当然,我会根据黑/白图像的结果裁剪最终的原始图像.
这是我的代码:
typedef struct Pixel { uint8_t r, g, b, a; } Pixel;
+(UIImage*)trimBlack:(UIImage*)originalImage {
GPUImagePicture *stillImageSource = [[GPUImagePicture alloc] initWithImage:originalImage];
GPUImageLuminanceThresholdFilter *stillImageFilter = [[GPUImageLuminanceThresholdFilter alloc] init];
stillImageFilter.threshold = 0.1;
[stillImageSource addTarget:stillImageFilter];
[stillImageSource processImage];
UIImage *imageToProcess = [stillImageFilter imageFromCurrentlyProcessedOutput];
RMImageTrimmingSides sides = RMImageTrimmingSidesAll;
CGImageRef image = imageToProcess.CGImage;
void * bitmapData = NULL;
CGContextRef context = CKBitmapContextAndDataCreateWithImage(image, &bitmapData);
Pixel *data = bitmapData;
size_t width = CGBitmapContextGetWidth(context);
size_t height = CGBitmapContextGetHeight(context);
size_t top = 0;
size_t bottom = height;
size_t left = 0;
size_t right = width;
// Scan the left
if (sides & RMImageTrimmingSidesLeft) {
for (size_t x = 0; x < width; x++) {
for (size_t y = 0; y < height; y++) {
Pixel pixel = data[y * width + x];
if (pixel.r != '\0' && pixel.g != '\0' && pixel.b != '\0') {
left = x;
goto SCAN_TOP;
}
}
}
}
// Scan the top
SCAN_TOP:
if (sides & RMImageTrimmingSidesTop) {
for (size_t y = 0; y < height; y++) {
for (size_t x = 0; x < width; x++) {
Pixel pixel = data[y * width + x];
if (pixel.r != '\0' && pixel.g != '\0' && pixel.b != '\0') {
top = y;
goto SCAN_RIGHT;
}
}
}
}
// Scan the right
SCAN_RIGHT:
if (sides & RMImageTrimmingSidesRight) {
for (size_t x = width-1; x >= left; x--) {
for (size_t y = 0; y < height; y++) {
Pixel pixel = data[y * width + x];
if (pixel.r != '\0' && pixel.g != '\0' && pixel.b != '\0') {
right = x;
goto SCAN_BOTTOM;
}
}
}
}
// Scan the bottom
SCAN_BOTTOM:
if (sides & RMImageTrimmingSidesBottom) {
for (size_t y = height-1; y >= top; y--) {
for (size_t x = 0; x < width; x++) {
Pixel pixel = data[y * width + x];
if (pixel.r != '\0' && pixel.g != '\0' && pixel.b != '\0') {
bottom = y;
goto FINISH;
}
}
}
}
FINISH:
CGContextRelease(context);
free(bitmapData);
CGRect rect = CGRectMake(left, top, right - left, bottom - top);
return [originalImage imageCroppedToRect:rect];
}
Run Code Online (Sandbox Code Playgroud)
感谢上面代码中使用的库的开发人员所做的所有努力,当然所有的功劳都归功于他们!
| 归档时间: |
|
| 查看次数: |
1378 次 |
| 最近记录: |