Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19184
  • 博文数量: 6
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 70
  • 用 户 组: 普通用户
  • 注册时间: 2011-11-04 10:38
个人简介

狂奔的蜗牛

文章分类

全部博文(6)

文章存档

2015年(6)

我的朋友

分类: Android平台

2015-04-03 10:45:46

一般用Camera横屏拍摄不会出现问题,竖屏拍摄会出现几个问题:

1 拍摄预览转向90度

可以通过android自带API 解决,很简单

  1. setCameraDisplayOrientation(Activity activityint cameraId, android.hardware.Camera camera)
2 通过onPreviewFrame获取的yuv数据byte[] data,这个是会旋转。
这个通过setDisplayOrientation/setRotation都是解决不了的

没办法只能手动旋转yuv数据了,注意YV12和NV21的转换函数是不一样的.

  1. private Camera.PreviewCallback previewCallback = new Camera.PreviewCallback() {
  2.         @Override
  3.         public void onPreviewFrame(byte[] data, Camera camera) {
  4.             if (!IsStarted() || data == null || mAvcEnc == null) {
  5.                 return;
  6.             }
  7.             byte[] dst = new byte[data.length];
  8.             if (iCameraCodecType == android.graphics.ImageFormat.YV12) {
  9.                 if (isBackCamera) {
  10.                     Utils.YUV420pRotate90(dst, data, WIDTH, HEIGHT);
  11.                 } else {
  12.                     Utils.YUV420pRotate270(dst, data, WIDTH, HEIGHT);
  13.                 }
  14.                 doSome(dst);
  15.             } else if (iCameraCodecType == android.graphics.ImageFormat.NV21) {
  16.                 if (isBackCamera) {
  17.                     Utils.YUV420spRotate90(dst, data, WIDTH, HEIGHT);
  18.                 } else {
  19.                     Utils.YUV420spRotateNegative90(dst, data, WIDTH, HEIGHT);
  20.                 }
  21.                 doSome(dst);
  22.             }
  23.         }
  24.     };


点击(此处)折叠或打开

  1. public static void YUV420spRotateNegative90(byte[] dst, byte[] src,
  2.             int srcWidth, int height) {
  3.         int nWidth = 0, nHeight = 0;
  4.         int wh = 0;
  5.         int uvHeight = 0;
  6.         if (srcWidth != nWidth || height != nHeight) {
  7.             nWidth = srcWidth;
  8.             nHeight = height;
  9.             wh = srcWidth * height;
  10.             uvHeight = height >> 1;// uvHeight = height / 2
  11.         }

  12.         // 旋转Y
  13.         int k = 0;
  14.         for (int i = 0; i < srcWidth; i++) {
  15.             int nPos = srcWidth - 1;
  16.             for (int j = 0; j < height; j++) {
  17.                 dst[k] = src[nPos - i];
  18.                 k++;
  19.                 nPos += srcWidth;
  20.             }
  21.         }

  22.         for (int i = 0; i < srcWidth; i += 2) {
  23.             int nPos = wh + srcWidth - 1;
  24.             for (int j = 0; j < uvHeight; j++) {
  25.                 dst[k] = src[nPos - i - 1];
  26.                 dst[k + 1] = src[nPos - i];
  27.                 k += 2;
  28.                 nPos += srcWidth;
  29.             }
  30.         }

  31.         return;
  32.     }

  33.     public static void YUV420spMirrorY(byte[] dst, byte[] src, int srcWidth,
  34.             int srcHeight) {
  35.         // 镜像Y
  36.         int k = 0;
  37.         int nPos = -1;
  38.         for (int j = 0; j < srcHeight; j++) {
  39.             nPos += srcWidth;
  40.             for (int i = 0; i < srcWidth; i++) {
  41.                 dst[k] = src[nPos - i];
  42.                 k++;
  43.             }
  44.         }

  45.         int uvHeight = srcHeight >> 1; // uvHeight = height / 2
  46.         for (int j = 0; j < uvHeight; j++) {
  47.             nPos += srcWidth;
  48.             for (int i = 0; i < srcWidth; i += 2) {
  49.                 dst[k] = src[nPos - i - 1];
  50.                 dst[k + 1] = src[nPos - i];
  51.                 k += 2;
  52.             }
  53.         }
  54.     }

  55.     public static void YUV420pRotate90(byte[] des, byte[] src, int width,
  56.             int height) {
  57.         int n = 0;
  58.         int hw = width / 2;
  59.         int hh = height / 2;
  60.         // copy y
  61.         for (int j = 0; j < width; j++) {
  62.             for (int i = height - 1; i >= 0; i--) {
  63.                 des[n++] = src[width * i + j];
  64.             }
  65.         }

  66.         // copy u
  67.         int uPos = width * height;
  68.         for (int j = 0; j < hw; j++) {
  69.             for (int i = hh - 1; i >= 0; i--) {
  70.                 des[n++] = src[uPos + hw * i + j];
  71.             }
  72.         }

  73.         // copy v
  74.         int vPos = uPos + width * height / 4;
  75.         for (int j = 0; j < hw; j++) {
  76.             for (int i = hh - 1; i >= 0; i--) {
  77.                 des[n++] = src[vPos + hw * i + j];
  78.             }
  79.         }
  80.     }

  81.     public static void YUV420spRotate90(byte[] des, final byte[] src, int width,
  82.             int height) {
  83.         int n = 0;
  84.         int hw = width / 2;
  85.         int hh = height / 2;
  86.         // copy y
  87.         for (int j = 0; j < width; j++) {
  88.             for (int i = height - 1; i >= 0; i--) {
  89.                 des[n++] = src[width * i + j];
  90.             }
  91.         }

  92.         int pos = width*height;
  93.         for (int j = 0; j < width; j+=2) {
  94.             for (int i = hh -1; i >= 0; i--) {
  95.                 des[n++] = src[pos + width*i + j];        // copy v
  96.                 des[n++] = src[pos + width*i + j + 1];    // copy u
  97.             }
  98.         }
  99.     }
  100.     
  101.     public static void YUV420pRotate180(byte[] des, byte[] src, int width,
  102.             int height) {
  103.         int n = 0;
  104.         int hw = width / 2;
  105.         int hh = height / 2;
  106.         // copy y
  107.         for (int j = height - 1; j >= 0; j--) {
  108.             for (int i = width; i > 0; i--) {
  109.                 des[n++] = src[width * j + i];
  110.             }
  111.         }

  112.         // copy u
  113.         int uPos = width * height;
  114.         for (int j = hh - 1; j >= 0; j--) {
  115.             for (int i = hw; i > 0; i--) {
  116.                 des[n++] = src[uPos + hw * i + j];
  117.             }
  118.         }

  119.         // copy v
  120.         int vPos = uPos + width * height / 4;
  121.         for (int j = hh - 1; j >= 0; j--) {
  122.             for (int i = hw; i > 0; i--) {
  123.                 des[n++] = src[vPos + hw * i + j];
  124.             }
  125.         }
  126.     }

  127.     public static void YUV420pRotate270(byte[] des, byte[] src, int width,
  128.             int height) {
  129.         int n = 0;
  130.         int hw = width / 2;
  131.         int hh = height / 2;
  132.         // copy y
  133.         for (int j = width - 1; j >= 0; j--) {
  134.             for (int i = 0; i < height; i++) {
  135.                 des[n++] = src[width * i + j];
  136.             }
  137.         }

  138.         // copy u
  139.         int uPos = width * height;
  140.         for (int j = hw - 1; j >= 0; j--) {
  141.             for (int i = 0; i < hh; i++) {
  142.                 des[n++] = src[uPos + hw * i + j];
  143.             }
  144.         }

  145.         // copy v
  146.         int vPos = uPos + width * height / 4;
  147.         for (int j = hw - 1; j >= 0; j--) {
  148.             for (int i = 0; i < hh; i++) {
  149.                 des[n++] = src[vPos + hw * i + j];
  150.             }
  151.         }
  152.     }

  153.     public static void YUV420pMirrorY(byte[] des, byte[] src, int width,
  154.             int height) {
  155.         int n = 0;
  156.         int hw = width / 2;
  157.         int hh = height / 2;
  158.         // copy y
  159.         for (int j = 0; j < height; j++) {
  160.             for (int i = width - 1; i >= 0; i--) {
  161.                 des[n++] = src[width * j + i];
  162.             }
  163.         }

  164.         // copy u
  165.         int uPos = width * height;
  166.         for (int j = 0; j < hh; j++) {
  167.             for (int i = hw - 1; i >= 0; i--) {
  168.                 des[n++] = src[uPos + hw * j + i];
  169.             }
  170.         }

  171.         // copy v
  172.         int vPos = uPos + width * height / 4;
  173.         for (int j = 0; j < hh; j++) {
  174.             for (int i = hw - 1; i >= 0; i--) {
  175.                 des[n++] = src[vPos + hw * j + i];
  176.             }
  177.         }
  178.     }

  179.     public static void YUV420pMirrorX(byte[] des, byte[] src, int width,
  180.             int height) {
  181.         int n = 0;
  182.         int hw = width / 2;
  183.         int hh = height / 2;

  184.         int nPos = width * height;
  185.         for (int j = 0; j < height; j++) {
  186.             nPos -= width;
  187.             for (int i = 0; i < width; i++) {
  188.                 des[n++] = src[nPos + i];
  189.             }
  190.         }

  191.         nPos = width * height + width * height / 4;
  192.         for (int j = 0; j < hh; j++) {
  193.             nPos -= hw;
  194.             for (int i = 0; i < hw; i++) {
  195.                 des[n++] = src[nPos + i];
  196.             }
  197.         }

  198.         nPos = width * height + width * height / 2;
  199.         for (int j = 0; j < hh; j++) {
  200.             nPos -= hw;
  201.             for (int i = 0; i < hw; i++) {
  202.                 des[n++] = src[nPos + i];
  203.             }
  204.         }
  205.     }



阅读(1542) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:ListView做评论功能

给主人留下些什么吧!~~