#因为C会写得比较少,所以记录
这里我是异步做一个文件存储
if (mCurrentFrame != NULL ) {
pthread_t thread_id;
//
Frame frame;
frame.width = mFrameWidth;
frame.height = mFrameHeight;
uint8_t* frameData = reinterpret_cast<uint8_t*>(malloc(sizeof(uint8_t) * (frame.width * frame.height * 3 / 2)));
memset(frameData, 0, sizeof(frameData));
memcpy(mCurrentFrame, frameData, frame.width * frame.height * 3 / 2);
frame.frameData = frameData;
ALOGV("%s , frame width %d,frame height %d,frame size:%d", __FUNCTION__, frame.width, frame.height,sizeof(frame));
int ret = pthread_create(&thread_id, NULL, saveFile, &frame);
if (ret != 0)
{
ALOGE("%s: pthread_create failed", __FUNCTION__);
return ;
}
//
ALOGV("%s,thread start success", __FUNCTION__);
}
return ...
线程静态函数:
static void* saveFile(void* arg)
{
//
Frame* frame = (Frame*)arg;
int width = frame->width;
int height = frame->height;
ALOGV("%s ,frame width: %d frame height :%d", __FUNCTION__, width,height);
uint8_t* frameData = frame->frameData;
//ALOGE("%s: save pic width: %d ,height : %d failed", __FUNCTION__, width, height);
if (frameData != NULL) {
FILE* file = fopen("/data/take_pic_test.yuv", "wb");
fwrite(frameData, 1, static_cast<size_t>(width * height * 3 / 2), file);
}
else
{
ALOGV("%s can't get frame data", __FUNCTION__);
}
if (frame->frameData != NULL)
{
delete[] frame;
}
return 0;
}
在线程中,width 和 height 基本的数据都是没有成功拿到,输出为乱七八糟,显然存放地址被干没了。
查阅资料
其注意事项中,表明传递的参数如果在线程还没有工作就被回收,那将会出现获取不到参数。
这里我在函数体里面声明的对象frame:Frame frame;
刚好吻合注意事项,如果我把 frame申明在类里面,可规避。。。