iOS   发布时间:2022-03-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 图像像素数据如何“扫描”图像像素?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
目标:

查找仅包含黑色和透明像素的图像左侧的第一个黑色像素.

是)我有的:

我知道如何获取像素数据并有一个黑色和透明像素数组(在这里找到它:https://stackoverflow.com/a/1262893/358480):

+ (NSArray*)getRGBAsFromImage:(UIImage*)image atX:(int)xx Andy:(int)yy count:(int)count
{
 NSMutableArray *result = [NSMutableArray arrayWithCapacity:count];

// First get the imagE into your data buffer
CGImageRef imageRef = [image CGImage];
NSUInteger width = CGImageGetWidth(imageRef);
NSUInteger height = CGImageGetHeight(imageRef);
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
unsigned char *rawData = malloc(height * width * 4);
NSUInteger bytesPerPixel = 4;
NSUInteger bytesPerRow = bytesPerPixel * width;
NSUInteger bitsPerComponent = 8;
CGContextRef context = CGBitmapContextCreate(rawData,width,height,bitsPerComponent,bytesPerRow,colorSpace,kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpacE);

CGContextDrawImage(context,CGRectMake(0,height),imageRef);
CGContextRelease(context);

// Now your rawData contains the image data in the RGBA8888 pixel format.
int byteIndex = (bytesPerRow * yy) + xx * bytesPerPixel;
for (int ii = 0 ; ii < count ; ++ii)
{
    NSUInteger alpha = (rawData[byteIndex + 3] * 1.0) / 255.0;
    byteIndex += 4;
    [result addObject:[NSnumber numberWithInt:alpha]];
}

free(rawData);

return result;
}

问题是什么 ?

我无法理解功能“扫描”图像的顺序.

我想要的是只获取图像的列,并找到列表1非透明像素的第一列.这样我就会知道如何裁剪图像左侧透明的一面?

如何按列获取像素?

谢谢

沙尼

@L_696_10@

字节从左到右,从上到下排序.所以要做你想做的事情,我想你想循环遍历rawData,如下所示:

int x = 0;
int y = 0;
BOOL found = NO;
for (x = 0; x < width; x++) {
    for (y = 0; y < height; y++) {
        unsigned char alphaByte = rawData[(y*bytesPerRow)+(x*bytesPerPixel)+3];
        if (alphaByte > 0) {
            found = YES;
            break;
        }
    }
    if (found) break;
}

NSLog(@"First non-transparent pixel at %i,%i",x,y);

然后,包含非透明像素的第一列将是列x.

大佬总结

以上是大佬教程为你收集整理的ios – 图像像素数据如何“扫描”图像像素?全部内容,希望文章能够帮你解决ios – 图像像素数据如何“扫描”图像像素?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。