NDK使用多线程技术加速图像处理

最近工作挺有意思的,对优化技术进行了各种的尝试和研究,经过测试,多线程对图像处理优化有一定的功效,通过分散处理的区域,指派不同线程处理不同位置的像素数据:

Android.mk:

LOCAL_SHARED_LIBRARIES := libcutil
E:\dev\sdk\android-ndk-r8e\platforms\android-9\arch-arm\usr\include


#include <pthread.h>
// @2013-6-30 14:43:03 sonikk
void testThread(BmpData* data1, BYTE* pDataOut)
{
	pthread_t tid1, tid2;
	int ret1,ret2;
	st_tparam tparam1(0, 2, data1, pDataOut),tparam2(1, 2, data1, pDataOut);
	ret1 = pthread_create(&tid1, NULL, onThread, &tparam1);
	LOGI("thread 1 is started");
	ret2 = pthread_create(&tid2, NULL, onThread, &tparam2);
	LOGI("thread 2 is started");
	
	ret1 = pthread_join(tid1, NULL); 
	LOGI("thread 1 is exit !");
	ret2 = pthread_join(tid2, NULL);
	LOGI("thread 2 is exit !");
	if(ret1 == 0 && ret2 == 0)  // to wait both of 2 threads completed the task
	{
		LOGI("both 2 threads are exited!");
	}
}


// @2013-6-30 14:43:03 sonikk
void* onThread(void *arg)
{
	st_tparam *p;
	p = (st_tparam*)arg;
	LOGI("thread %d is runing", p->index);
	int w = p->data1->width;
	int h = p->data1->height;
	int s = p->data1->stride;
	BYTE* pDataOut = p->pDataOut;
	int thread_startY = p->index * h / p->subs;
	int thread_endY = thread_startY + h / p->subs;
	int pix = 0;
	int a, r, g, b, l;
	for (int y=thread_startY; y<thread_endY; y++)
	{
		for (int x=0; x<w; x++)
		{
			pix = (y*w + x)*s;
			r = p->data1->pData[pix];
			b = p->data1->pData[pix+1];
			g = p->data1->pData[pix+2];
			a = p->data1->pData[pix+3];
			l = r * 0.587 + g * 0.299 + b * 0.114;
			pDataOut[pix] = l;
			pDataOut[pix + 1] = l;
			pDataOut[pix + 2] = l;
			pDataOut[pix + 3] = 255;
		}
	}
	return NULL;
}




你可能感兴趣的:(thread,android,image,jni,optimization)