1 #include "AndroidCameraCapture.h" 2 #include <android/log.h> 4 #define LOG_TAG "AndroidCameraCapture" 5 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__) 6 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__) 18 buffer = vsCreateImage(vsSize(width, height),VS_DEPTH_8U,3);
19 bufferN = vsCreateImage(vsSize(width, height),VS_DEPTH_8U,3);
20 bufferT = vsCreateImage(vsSize(height, width),VS_DEPTH_8U,3);
21 pthread_mutex_init(&mutex, NULL);
22 pthread_cond_init(&cond, NULL);
25 this->orientation = orientation;
28 this->height = height;
34 vsReleaseImage(&buffer);
35 vsReleaseImage(&bufferN);
36 vsReleaseImage(&bufferT);
37 pthread_mutex_destroy(&mutex);
38 pthread_cond_destroy(&cond);
43 pthread_mutex_lock(&mutex);
46 for (
int i=0;i<width*height;i++){
48 buffer->
imageData[i*3+1] = imageData[i*4+1];
49 buffer->
imageData[i*3+2] = imageData[i*4+2];
52 pthread_cond_signal(&cond);
53 pthread_mutex_unlock(&mutex);
56 void AndroidCameraCapture::WriteFrameYUV(
unsigned char* imageData)
58 pthread_mutex_lock(&mutex);
60 YUV_NV21_TO_RGB(imageData, buffer, width, height);
62 pthread_cond_signal(&cond);
63 pthread_mutex_unlock(&mutex);
72 int rc = gettimeofday(&tp, NULL);
73 ts.tv_sec = tp.tv_sec + 2;
75 pthread_mutex_lock(&mutex);
76 while (!frameArrived){
77 int ret_cond = pthread_cond_timedwait(&cond, &mutex, &ts);
78 if (ret_cond == ETIMEDOUT){
79 pthread_mutex_unlock(&mutex);
88 vsFlip(buffer, bufferN, 1);
90 vsCopy(buffer, bufferN);
94 vsTranspose(buffer, bufferT);
96 vsFlip(bufferT, bufferT, 1);
101 vsFlip(buffer, bufferN, 0);
103 vsFlip(buffer, bufferN, -1);
107 vsTranspose(buffer, bufferT);
109 vsFlip(bufferT, bufferT, -1);
111 vsFlip(bufferT, bufferT, 0);
116 pthread_mutex_unlock(&mutex);
120 void AndroidCameraCapture::YUV420toRGB(
unsigned char* data,
VsImage* buff,
int width,
int height){
121 int size = width*height;
123 int u, v, y1, y2, y3, y4;
128 for(
int i=0, k=0; i < size; i+=2, k+=2) {
131 y3 = data[width+i ]&0xff;
132 y4 = data[width+i+1]&0xff;
134 u = data[offset+k ]&0xff;
135 v = data[offset+k+1]&0xff;
144 if (i!=0 && (i+2)%width==0)
149 int AndroidCameraCapture::clamp(
int x) {
151 return !(y=x>>8) ? x : (0xff ^ (y>>24));
154 void AndroidCameraCapture::YUV_NV21_TO_RGB(
unsigned char* yuv,
VsImage* buff,
int width,
int height)
156 const int frameSize = width * height;
163 unsigned char* rgb = (
unsigned char*)buff->
imageData;
166 for (
int i = 0, ci = ii; i < height; ++i, ci += di)
168 for (
int j = 0, cj = ij; j < width; ++j, cj += dj)
170 int y = (0xff & ((int) yuv[ci * width + cj]));
171 int v = (0xff & ((int) yuv[frameSize + (ci >> 1) * width + (cj & ~1) + 0]));
172 int u = (0xff & ((int) yuv[frameSize + (ci >> 1) * width + (cj & ~1) + 1]));
179 int a0 = 1192 * (y - 16);
180 int a1 = 1634 * (v - 128);
181 int a2 = 832 * (v - 128);
182 int a3 = 400 * (u - 128);
183 int a4 = 2066 * (u - 128);
185 int r = (a0 + a1) >> 10;
186 int g = (a0 - a2 - a3) >> 10;
187 int b = (a0 + a4) >> 10;
196 void AndroidCameraCapture::convertYUVtoARGB(
int y,
int u,
int v,
char *r,
char *g,
char *b) {
198 rr = y + (int)1.402f*u;
199 gg = y - (int)(0.344f*v +0.714f*u);
200 bb = y + (int)1.772f*v;
201 *r = (rr)>255? 255 : (rr)<0 ? 0 : (rr);
202 *g = (gg)>255? 255 : (gg)<0 ? 0 : (gg);
203 *b = (bb)>255? 255 : (bb)<0 ? 0 : (bb);
void WriteFrame(unsigned char *imageData)
Method for writing imageData to buffer object.
char * imageData
Pointer to aligned image data.
VsImage * GrabFrame(long &timeStamp)
This function is called periodically to get the new video frame to process.
AndroidCameraCapture()
Constructor.
~AndroidCameraCapture(void)
Destructor.