2014-09-18 46 views
0

我爲show image stream的相機創建了surfaceview,並且我有用於檢測特定區域的矩形。我只需要在矩形中獲得Y值,我該怎麼做。我發現代碼表單網站的代碼將YUV更改爲RGB,但我通過只返回Y值來適應它。 請告訴我如何在特定區域獲取僅在矩形中的Y值。如何通過特定區域獲取YUV值

我試着改變i,j,寬度,高度。它收集? 舉例說明。

我是新的android程序員。 謝謝

public void decodeYUV420(int[] rgb, byte[] yuv420, int width, int height) { 
    final int frameSize = width * height; 

for (int j = 0, yp = 0; j < height; j++) { 
    int uvp = frameSize + (j >> 1) * width, u = 0, v = 0; 
    for (int i = 0; i < width; i++, yp++) { 
     int y = (0xff & ((int) yuv420[yp])) - 16; 
     if (y < 0) y = 0; 
     if ((i & 1) == 0) { 
      v = (0xff & yuv420[uvp++]) - 128; 
      u = (0xff & yuv420[uvp++]) - 128; 
     } 

     int y1192 = 1192 * y; 
     int r = (y1192 + 1634 * v); 
     int g = (y1192 - 833 * v - 400 * u); 
     int b = (y1192 + 2066 * u); 

     if (r < 0) r = 0; else if (r > 262143) r = 262143; 
     if (g < 0) g = 0; else if (g > 262143) g = 262143; 
     if (b < 0) b = 0; else if (b > 262143) b = 262143; 

     //rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) 
      // & 0xff00) | ((b >> 10) & 0xff); 

      rgb[yp] = y;  // i need only y value 
    } 
} 
} 

回答

0

事情是這樣的:

public int[] extractY(byte[] yuv420, Size szYuv, Rect rcY) 
{ 
    rcY.intersect(0, 0, szYuv.width, szYuv.height); 
    final int[] res = int[rcY.width()*rcY.height()]; 

    int yCount = 0; 
    for (int row=rcY.left; row<rcY.right; row++) 
    { 
     for (int col=rcY.top; col<rcY.bottom; col++) 
     { 
      res[yCount++] = yuv420[col+rcY.left+(row+rcY.top)*szYuv.width]; 
     } 
    } 
    return res; 
}