我正尝试使用Camera2API Google示例https://github.com/googlesamples/android-Camera2Video将yuv帧转换为rgb。我通过删除录制功能和添加https://developer.android.com/reference/android/media/ImageReader.OnImageAvailableListener的实现来更改源,同时将分辨率设置为1280x720。
这是图像监听器的实现:
private static final String TAG = "FrameImageListener";
private static final int SKIP_FRAMES = 30;
private int mImageCounter = 0;
@Override
public void onImageAvailable(ImageReader reader) {
Image img;
img = reader.acquireNextImage();
if (img == null) return;
try {
if (mImageCounter % SKIP_FRAMES == 0) {
rgb(img);
}
} finally {
mImageCounter++;
img.close();
}
}
private Bitmap rgb(Image image) {
if (image == null) return null;
int W = image.getWidth();
int H = image.getHeight();
byte[] frameData = new byte[W * H * 2 - 2];
Image.Plane Y = image.getPlanes()[0];
int YRemaining = Y.getBuffer().remaining();
ByteBuffer YBuffer = Y.getBuffer();
long start = System.nanoTime();
YBuffer.get(frameData, 0, YRemaining);
Log.d(TAG, "image W H " + W + " " + H);
Log.d(TAG, "remaining " + YRemaining);
Log.d(TAG, "copy time " + (System.nanoTime() - start) / 1000_000);
return null;
}
我们在不同的Samsumg设备上测试了相同的代码,并注意到S8 +缺乏性能
使用带有Android 8.0的Samsung Galaxy s8 +调试输出,如下所示:
image W H 1024 768
remaining 786432
copy time 18
三星Galaxy s7的调试输出如下:
image W H 1024 768
remaining 786432
copy time 5
所以问题是复制时间有很大差异。较新的设备比旧的设备工作慢。