ios – 图像像素数据如何“扫描”图像像素?

ios – 图像像素数据如何“扫描”图像像素?,第1张

概述目标: 查找仅包含黑色和透明像素图像左侧的第一个黑色像素. 是)我有的: 我知道如何获取像素数据并有一个黑色和透明像素数组(在这里找到它:https://stackoverflow.com/a/1262893/358480): + (NSArray*)getRGBAsFromImage:(UIImage*)image atX:(int)xx andY:(int)yy count:(int)cou 目标:

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

是)我有的:

我知道如何获取像素数据并有一个黑色和透明像素数组(在这里找到它: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 bufferCGImageRef 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非透明像素的第一列.这样我就会知道如何裁剪图像左侧透明的一面?

如何按列获取像素?

谢谢

沙尼

解决方法 字节从左到右,从上到下排序.所以要做你想做的事情,我想你想循环遍历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 – 图像像素数据如何“扫描”图像像素?所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1051833.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存