- 这是一个好主意吗?有没有更好/更有效的方法?
是的。没有其他方法可以比较连续的帧来检测运动事件。
- 格式 YUV_420_88 中的哪个平面最适合这种运动检测?
我认为是Planes()[0]。
我的示例代码:
mOnImageAvailableListener = new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader imageReader) {
Image image = null;
try {
image = imageReader.acquireLatestImage();
if (null == image) {
return;
}
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.capacity()];
buffer.get(bytes);
detectMotion(bytes);
} finally {
if (image != null) {
image.close();
}
}
}
private void detectMotion(byte[] frame) {
if (null == frame) {
return;
}
int ret = NativeUtil.detectMotion(frame);
if (ret == 1) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mIvMotion.setVisibility(View.VISIBLE);
}
});
Log.d(TAG, "Detected motion event");
} else {
runOnUiThread(new Runnable() {
@Override
public void run() {
mIvMotion.setVisibility(View.INVISIBLE);
}
});
}
}
};