如何阅读屏幕以查看哪些像素为白色

时间:2012-08-01 06:44:28

标签: iphone ios image-processing screenshot

我有一个视图,用户可以使用this绘制一些行。

现在使用代码在点之间绘制线条:

- (void) renderLineFromPoint:(CGPoint)start toPoint:(CGPoint)end
{
    static GLfloat*     vertexBuffer = NULL;
    static NSUInteger   vertexMax = 64;
    NSUInteger          vertexCount = 0,
                        count,
                        i;

    //Allocate vertex array buffer
    if(vertexBuffer == NULL)
        vertexBuffer = malloc(vertexMax * 2 * sizeof(GLfloat));

    // Add points to the buffer so there are drawing points every X pixels
    count = MAX(ceilf(sqrtf((end.x - start.x) * (end.x - start.x) + (end.y - start.y) * (end.y - start.y)) / kBrushPixelStep), 1);
    for(i = 0; i < count; ++i) {
        if(vertexCount == vertexMax) {
            vertexMax = 2 * vertexMax;
            vertexBuffer = realloc(vertexBuffer, vertexMax * 2 * sizeof(GLfloat));
        }

        vertexBuffer[2 * vertexCount + 0] = start.x + (end.x - start.x) * ((GLfloat)i / (GLfloat)count);
        vertexBuffer[2 * vertexCount + 1] = start.y + (end.y - start.y) * ((GLfloat)i / (GLfloat)count);
        vertexCount += 1;
    }

    //Render the vertex array
    glVertexPointer(2, GL_FLOAT, 0, vertexBuffer);
    glDrawArrays(GL_POINTS, 0, vertexCount);

    // Display the buffer
    [self swapBuffers];
}

目标是阅读屏幕的绘图区域,该区域由以下代码启动:

PictureView * scratchPaperView = [[RecordedPaintingView alloc] initWithFrame:CGRectMake(0, 45, 320, 415)];
    [self.view addSubview:scratchPaperView];

我想找出线条的像素,即绘图区域中白色的所有像素。请告诉我如何从这里开始?

1 个答案:

答案 0 :(得分:1)

假设您可以从PictureView中获取UIImage.CGImage或CGImageRef,那么您将此图像渲染为CGBitMapContext。图像将告诉您组件的数量,以及它是否具有alpha和alpa的位置。很可能你会得到4字节像素(32位/像素)。然后你走每一行看每个像素。假设黑色背景(可能是255,0,0,0或0,0,0,255),当您接近或击中一条线时,您将看到非黑色像素。纯像素将是255,255,255,255。

我很确定你可以找到如何将图像渲染到上下文中的示例,以及如何通过Google搜索来检查像素。坦率地说,总能得到我的是令人困惑的像素布局属性 - 我通常最终打印出几个测试用例以确保我做对了。

相关问题