NDK(3)最简单处理图片

C++速度真的比java快很多吗?

在做这个之前,我不知道C++和java的处理速度究竟是怎样的一个比例,以前做图片处理都是java代码实现,不过真正实用的都是NDK处理的,那我们就试一下,两者处理的差距吧。
图片的大小大概1M。
代码很简单先上java端代码

  public Bitmap convertGrayImg(int resID) {
        Bitmap img1 = ((BitmapDrawable) getResources().getDrawable(resID)).getBitmap();

        int w = img1.getWidth(), h = img1.getHeight();
        int[] pix = new int[w * h];
        img1.getPixels(pix, 0, w, 0, 0, w, h);

        int alpha = 0xFF << 24;
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                // 获得像素的颜色
                int color = pix[w * i + j];
                int red = ((color & 0x00FF0000) >> 16);
                int green = ((color & 0x0000FF00) >> 8);
                int blue = color & 0x000000FF;
                color = (red + green + blue) / 3;
                color = alpha | (color << 16) | (color << 8) | color;
                pix[w * i + j] = color;
            }
        }
        Bitmap result = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
        result.setPixels(pix, 0, w, 0, 0, w, h);
        return result;
    }

执行时间是1644mm;

然后是NDK

//声明 native方法 
 public native int[] getImgToGray(int[] data, int w, int h);
//java Bitmap转换成int数组然后传给NDK方法
 public Bitmap getJniBitmap() {
        Bitmap bitmap = ((BitmapDrawable) getResources().getDrawable(R.mipmap.tim)).getBitmap();
        int w = bitmap.getWidth(), h = bitmap.getHeight();
        int[] pix = new int[w * h];
        bitmap.getPixels(pix, 0, w, 0, 0, w, h);
        //把彩色像素转为灰度像素
        int[] resultInt = getImgToGray2(pix, w, h);
        Bitmap resultImg = Bitmap.createBitmap(w, h, Bitmap.Config.RGB_565);
        resultImg.setPixels(resultInt, 0, w, 0, 0, w, h);
        return resultImg;
    }

//C++代码和java代码很相似
extern "C"
JNIEXPORT jintArray JNICALL
Java_com_ssy_ndkapplication_Main4Activity_getImgToGray(JNIEnv *env, jobject instance,
                                                       jintArray data_, jint w, jint h) {
    jint *data;
    data = env->GetIntArrayElements(data_, NULL);
    if (data == NULL) {
        return 0; /* exception occurred */
    }
    int alpha = 0xFF << 24;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            // 获得像素的颜色
            int color = data[w * i + j];
            int red = ((color & 0x00FF0000) >> 16);
            int green = ((color & 0x0000FF00) >> 8);
            int blue = color & 0x000000FF;
            color = (red + green + blue) / 3;
            color = alpha | (color << 16) | (color << 8) | color;
            data[w * i + j] = color;
        }
    }
    int size = w * h;
    jintArray result = env->NewIntArray(size);
    env->SetIntArrayRegion(result, 0, size, data);
    env->ReleaseIntArrayElements(data_, data, 0);
    return result;
}

执行时间是575mm;
很可怕的一个比例大概是3:1;
所以这就说明了处理图片,音视频或者其他大数据的时候Java很无力,必须NDK开发才可以,至于使用C++还是C,看个人爱好,不过Google官方推荐的是C++;
其实有一个很大的问题是,网上关于NDK开发的教程大多都是入门级别的,很少有进阶的,就说明这一块的人才相对来说比较稀缺,也说明比较难,希望和大家一起学习,一起分享。共同进步。(我的代码其实也是参考别人的,然后理解,自己再敲出来的,并非原创,再次感谢前辈们的分享)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值