Guest User

Untitled

a guest
Jan 19th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. public class YuvHelper {
  2.  
  3. public static byte[] rotateYUV420Degree90(byte[] data, int imageWidth,
  4. int imageHeight) {
  5. byte[] yuv = new byte[imageWidth * imageHeight * 3 / 2];
  6. // Rotate the Y luma
  7. int i = 0;
  8. for (int x = 0; x < imageWidth; x++) {
  9. for (int y = imageHeight - 1; y >= 0; y--) {
  10. yuv[i] = data[y * imageWidth + x];
  11. i++;
  12. }
  13. }
  14. // Rotate the U and V color components
  15. i = imageWidth * imageHeight * 3 / 2 - 1;
  16. for (int x = imageWidth - 1; x > 0; x = x - 2) {
  17. for (int y = 0; y < imageHeight / 2; y++) {
  18. yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + x];
  19. i--;
  20. yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth)
  21. + (x - 1)];
  22. i--;
  23. }
  24. }
  25. return yuv;
  26. }
  27.  
  28. public byte[] rotateYUV420Degree180(byte[] data, int imageWidth,
  29. int imageHeight) {
  30. byte[] yuv = new byte[imageWidth * imageHeight * 3 / 2];
  31. int i = 0;
  32. int count = 0;
  33. for (i = imageWidth * imageHeight - 1; i >= 0; i--) {
  34. yuv[count] = data[i];
  35. count++;
  36. }
  37. i = imageWidth * imageHeight * 3 / 2 - 1;
  38. for (i = imageWidth * imageHeight * 3 / 2 - 1; i >= imageWidth
  39. * imageHeight; i -= 2) {
  40. yuv[count++] = data[i - 1];
  41. yuv[count++] = data[i];
  42. }
  43. return yuv;
  44. }
  45.  
  46. public byte[] rotateYUV420Degree270(byte[] data, int imageWidth,
  47. int imageHeight) {
  48. byte[] yuv = new byte[imageWidth * imageHeight * 3 / 2];
  49. int nWidth = 0, nHeight = 0;
  50. int wh = 0;
  51. int uvHeight = 0;
  52. if (imageWidth != nWidth || imageHeight != nHeight) {
  53. nWidth = imageWidth;
  54. nHeight = imageHeight;
  55. wh = imageWidth * imageHeight;
  56. uvHeight = imageHeight >> 1;// uvHeight = height / 2
  57. }
  58.  
  59. int k = 0;
  60. for (int i = 0; i < imageWidth; i++) {
  61. int nPos = 0;
  62. for (int j = 0; j < imageHeight; j++) {
  63. yuv[k] = data[nPos + i];
  64. k++;
  65. nPos += imageWidth;
  66. }
  67. }
  68. for (int i = 0; i < imageWidth; i += 2) {
  69. int nPos = wh;
  70. for (int j = 0; j < uvHeight; j++) {
  71. yuv[k] = data[nPos + i];
  72. yuv[k + 1] = data[nPos + i + 1];
  73. k += 2;
  74. nPos += imageWidth;
  75. }
  76. }
  77. return rotateYUV420Degree180(yuv, imageWidth, imageHeight);
  78. }
  79. }
Add Comment
Please, Sign In to add comment