我想在视频流运行时获取每个像素的 RGB 值。我目前正在使用 textureView 来显示相机流。每次我得到一个新帧,我都会扫描帧,看看我的 RG 和 B 值是否都等于 255。但问题是:扫描一帧需要 20 多秒,所以它会跳过 30-40 帧。我想读取像素而不跳过任何像素。因此,如果有人可以让我知道是否有有效的方法来做到这一点,我将非常感谢他们的帮助。
这就是我正在做的。
TextureView.SurfaceTextureListener textureListener = new TextureView.SurfaceTextureListener() {
@Override
public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i1) {
openCamera();
}
@Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i1) {
}
@Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
return false;
}
@Override
public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
Bitmap bitmap = textureView.getBitmap();
// Initializing all color values
int redmax = 0, greenmax = 0, bluemax = 0, redx = 0, bluex = 0, greenx = 0, redy = 0, bluey = 0, greeny = 0;
int Pixel = bitmap.getHeight() * bitmap.getWidth();
ByteBuffer Pixels = ByteBuffer.allocateDirect(4 * Pixel);
for (int y = 0; y < bitmap.getHeight(); y++) { // Height
for (int x = 0; x < bitmap.getWidth(); x++) { // Width
GLES20.glReadPixels(x, y, bitmap.getWidth(), bitmap.getHeight(), GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, Pixels);
byte rValue = Pixels.get(0);
byte gValue = Pixels.get(1);
byte bValue = Pixels.get(2);
byte A = Pixels.get(3);
if (rValue > redmax) { // This is just to get max red Value in the frame
redmax = rValue;
redx = x;
redy = y;
}
if (bValue > bluemax) { // This is just to get max blue Value in the frame
bluemax = bValue;
bluex = x;
bluey = y;
}
if (gValue > greenmax) { // This is just to get max green Value in the frame
greenmax = gValue;
greenx = x;
greeny = y;
}
}
}
Log.i("Picture max red value", "R: " + redmax + " x: " + redx + " y: " + redy);
Log.i("Picture max green value", "G: " + greenmax + " x: " + greenx + " y: " + greeny);
Log.i("Picture max blue value", "B: " + bluemax + " x: " + bluex + " y: " + bluey);
Log.i("Picture x and y value", "XY: " + " x: " + bitmap.getWidth() + " y: " + bitmap.getHeight());
if ((redmax + bluemax + greenmax) > 760) {
BitmapUtil.saveBitmap(bitmap, String.format("/sdcard/read_1" + UUID.randomUUID().toString() + ".jpg"));
} else {
bitmap.recycle();
}