@Override public void onPreviewFrame(byte[] data, Camera camera) { if (mPusherRuning) { switch (screen) { case SCREEN_PORTRAIT: portraitData2Raw(data); break; case SCREEN_LANDSCAPE_LEFT: raw = data; break; case SCREEN_LANDSCAPE_RIGHT: landscapeData2Raw(data); break; } mNative.fireVideo(raw); } camera.addCallbackBuffer(buffer); }
private void landscapeData2Raw(byte[] data) { int width = mParam.getWidth(), height = mParam.getHeight(); int y_len = width * height; int k = 0; // y数据倒叙插入raw中 for (int i = y_len - 1; i > -1; i--) { raw[k] = data[i]; k++; } // System.arraycopy(data, y_len, raw, y_len, uv_len); // v1 u1 v2 u2 // v3 u3 v4 u4 // 需要转换为: // v4 u4 v3 u3 // v2 u2 v1 u1 int maxpos = data.length - 1; int uv_len = y_len >> 2; // 4:1:1 for (int i = 0; i < uv_len; i++) { int pos = i << 1; raw[y_len + i * 2] = data[maxpos - pos - 1]; raw[y_len + i * 2 + 1] = data[maxpos - pos]; } }
private void portraitData2Raw(byte[] data) { // if (mContext.getResources().getConfiguration().orientation != // Configuration.ORIENTATION_PORTRAIT) { // raw = data; // return; // } int width = mParam.getWidth(), height = mParam.getHeight(); int y_len = width * height; int uvHeight = height >> 1; // uv数据高为y数据高的一半 int k = 0; if (mParam.getCameraId() == CameraInfo.CAMERA_FACING_BACK) { for (int j = 0; j < width; j++) { for (int i = height - 1; i >= 0; i--) { raw[k++] = data[width * i + j]; } } for (int j = 0; j < width; j += 2) { for (int i = uvHeight - 1; i >= 0; i--) { raw[k++] = data[y_len + width * i + j]; raw[k++] = data[y_len + width * i + j + 1]; } } } else { for (int i = 0; i < width; i++) { int nPos = width - 1; for (int j = 0; j < height; j++) { raw[k] = data[nPos - i]; k++; nPos += width; } } for (int i = 0; i < width; i += 2) { int nPos = y_len + width - 1; for (int j = 0; j < uvHeight; j++) { raw[k] = data[nPos - i - 1]; raw[k + 1] = data[nPos - i]; k += 2; nPos += width; } } } }