亲宝软件园·资讯

展开

android camera yuv帧水平翻转 android camera yuv帧水平翻转实例

TedSmile 人气:0

Camera预览时需要对yuv帧做水平翻转处理,直接在onPreviewFrame(byte[] data, Camera camera)里调用Mirror函数,处理完后的图片和预览效果成水平翻转关系。

nv21的镜像算法:

  private void Mirror(byte[] src, int w, int h) { //src是原始yuv数组
    int i;
    int index;
    byte temp;
    int a, b;
    //mirror y
    for (i = 0; i < h; i++) {
      a = i * w;
      b = (i + 1) * w - 1;
      while (a < b) {
        temp = src[a];
        src[a] = src[b];
        src[b] = temp;
        a++;
        b--;
      }
    }
 
    // mirror u and v
    index = w * h;
    for (i = 0; i < h / 2; i++) {
      a = i * w;
      b = (i + 1) * w - 2;
      while (a < b) {
        temp = src[a + index];
        src[a + index] = src[b + index];
        src[b + index] = temp;
 
        temp = src[a + index + 1];
        src[a + index + 1] = src[b + index + 1];
        src[b + index + 1] = temp;
        a+=2;
        b-=2;
      }
    }
  }

i420的镜像算法:

private void Mirror(byte[] src, int w, int h) { //src是原始yuv数组
    int i;
    int index;
    byte temp;
    int a, b;
    //mirror y
    for (i = 0; i < h; i++) {
      a = i * w;
      b = (i + 1) * w - 1;
      while (a < b) {
        temp = src[a];
        src[a] = src[b];
        src[b] = temp;
        a++;
        b--;
      }
    }
    //mirror u
    index = w * h;//U起始位置
    for (i = 0; i < h / 2; i++) {
      a = i * w / 2;
      b = (i + 1) * w / 2 - 1;
      while (a < b) {
        temp = src[a + index];
        src[a + index] = src[b + index];
        src[b + index] = temp;
        a++;
        b--;
      }
    }
    //mirror v
    index = w * h / 4 * 5;//V起始位置
    for (i = 0; i < h / 2; i++) {
      a = i * w / 2;
      b = (i + 1) * w / 2 - 1;
      while (a < b) {
        temp = src[a + index];
        src[a + index] = src[b + index];
        src[b + index] = temp;
        a++;
        b--;
      }
    }

I420,YV12,NV12,NV21均属于YUV420,以下为四种格式的排列顺序:

I420: YYYYYYYY UUVV =>YUV420P
YV12: YYYYYYYY VVUU =>YUV420P
NV12: YYYYYYYY UVUV =>YUV420SP
NV21: YYYYYYYY VUVU =>YUV420SP

以上这篇android camera yuv帧水平翻转实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

加载全部内容

相关教程
猜你喜欢
用户评论