如何将jpeg图像转换为nv21帧?

时间:2012-12-28 13:29:51

标签: android

如何在android中将图像(jpeg,png)转换为nv21 / YCbCr格式?

1 个答案:

答案 0 :(得分:1)

这就是我使用的:

static inline int _CLAMP(int x) {
    unsigned y;
    return !(y=x>>8) ? x : (0xff ^ (y>>24));
}

void rgbtoyuv(unsigned char * s_ptr,unsigned char* y_ptr, unsigned char* c_ptr, int width, int height) {

int i,j,r,g,b,y,u,v;
for(i=0;i<height;i++)
  for(j=0;j<width;j++) {
    r=*s_ptr++;
    g=*s_ptr++;
    b=*s_ptr++;
    s_ptr++;
    y=((77*r+150*g+29*b+128)>>8);
    *y_ptr++=_CLAMP(y);
    if(i%2 && j%2) {
      u=((-43*r-85*g+128*b+128)>>8)+128;
      v=((128*r-107*g-21*b+128)>>8)+128;
      *c_ptr++=_CLAMP(v);
      *c_ptr++=_CLAMP(u);
    }
  }
}
void Java_nativeBitmaptoYUV(JNIEnv* env, jobject obj, jobject bitmap, jint w, jint h,jbyteArray jdest) {
  char *src=0;
  jboolean isCopy;
  char* buffer = (*env)->GetByteArrayElements(env, jdest, &isCopy);
  AndroidBitmap_lockPixels(env, bitmap, (void**) &src);
  rgbtoyuv(src,buffer,buffer+w*h,w,h);
  AndroidBitmap_unlockPixels(env, bitmap);
  (*env)->ReleaseByteArrayElements(env, jdest, buffer, 0);
}