在我的应用程序中,我使用Bitmap.getPixel很多,所以我想到使用NDK实现该功能并节省执行时间。 令我惊讶的是,CallIntMethod比普通java中的完全相同的函数需要更长的时间......只需通过位图宽度和长度来读取像素颜色并计算从该颜色到黑色的欧几里德距离。
Java函数:
//rSrc: bitmap dimension
for(int i=rSrc.left;i<rSrc.right;i++)
for(int j=rSrc.top;j<rSrc.bottom;j++)
{
int c=bm.getPixel(i,j);
int r=Color.red(c);
int g=Color.green(c);
int b=Color.blue(c);
double val1=Math.sqrt((cRed-r)*(cRed-r)+(cGreen-g)*(cGreen-g)+(cBlue-b)*(cBlue-b));
}
C函数:
int i;
int j;
for(i=rSrc.left;i<rSrc.right;i++)
for(j=rSrc.top;j<rSrc.bottom;j++)
{
int color=(*env)->CallIntMethod(env,b,bitmapGetPixel,i,j);
int cr=red(color);
int cg=green(color);
int cb=blue(color);
double val=sqrt((cRed-cr)*(cRed-cr)+(cGreen-cg)*(cGreen-cg)+(cBlue-cb)*(cBlue-cb));
}
java函数需要7秒才能运行,而本机函数需要10秒!
这怎么可能? 感谢。