【问题标题】:Android bitmap operations using ndk使用ndk的Android位图操作
【发布时间】:2016-04-04 07:06:05
【问题描述】:

目前我正在开发一个涉及一些图像处理的 Android 应用程序。经过一些研究,我发现最好使用 Android NDK 进行位图操作以获得良好的性能。所以,我找到了一些像这样的基本示例:

static void myFunction(AndroidBitmapInfo* info, void* pixels){
    int xx, yy, red, green, blue;
    uint32_t* line;

    for(yy = 0; yy < info->height; yy++){
        line = (uint32_t*)pixels;
        for(xx =0; xx < info->width; xx++){

            //extract the RGB values from the pixel
            blue = (int) ((line[xx] & 0x00FF0000) >> 16);
            green = (int)((line[xx] & 0x0000FF00) >> 8);
            red = (int) (line[xx] & 0x00000FF );

            //change the RGB values

            // set the new pixel back in
            line[xx] =
                    ((blue << 16) & 0x00FF0000) |
                    ((green << 8) & 0x0000FF00) |
                    (red & 0x000000FF);
        }

        pixels = (char*)pixels + info->stride;
    }
}

我使用了这段代码,它对于基本操作非常有效,但我想做一个更复杂的代码,比如过滤器,我需要从当前像素访问上方和下方的像素。更具体地说,我给你举个例子:对于膨胀和侵蚀操作,我们移动像素,我们验证像素是否来自西北、北、东北、西、东、西南、南和东南(对于8 个邻居结构元素)是对象像素。我需要知道的是如何使用上面的代码访问南北像素的值。 我不太熟悉使用 C(指针等)进行图像处理。

谢谢!

【问题讨论】:

    标签: image-processing bitmap android-ndk java-native-interface


    【解决方案1】:

    我稍微编辑了你的函数,基本上是为了得到数组中的像素位置,公式是:

    位置 = y*width+x

    static void myFunction(AndroidBitmapInfo* info, void* pixels){
    int xx, yy, red, green, blue;
    uint32_t* px = pixels;
    
    for(yy = 0; yy < info->height; yy++){
        for(xx =0; xx < info->width; xx++){
    
            int position = yy*info->width+xx;//this formula gives you the address of pixel with coordinates (xx; yy) in 'px' 
    
            //extract the RGB values from the pixel
            blue = (int) ((line[position] & 0x00FF0000) >> 16);
            green = (int)((line[position] & 0x0000FF00) >> 8);
            red = (int) (line[position] & 0x00000FF );
    
            //change the RGB values
    
            // set the new pixel back in
            line[position] =
                    ((blue << 16) & 0x00FF0000) |
                    ((green << 8) & 0x0000FF00) |
                    (red & 0x000000FF);
             //so the position of the south pixel is (yy+1)*info->width+xx
             //and the one of the north is (yy-1)*info->width+xx
             //the left yy*info->width+xx-1
             //the right yy*info->width+xx+1
        }
    }
    

    假设您想读取/编辑坐标为 x,y 的像素

    您必须检查是否 0

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-31
      • 1970-01-01
      • 2015-03-17
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      • 1970-01-01
      相关资源
      最近更新 更多