我的任务是从相机中获取预览帧,处理它们并在我的布局中更新TextView。我参考google的camera2示例代码,并设法使用OnImageAvailableListener' s {获取帧{1}}方法,但我无法在OnImageAvailable()
定义(应用程序崩溃)中更新我的TextView内容。我是Android编程和java的新手。获取每个帧后更新我的TextView的任何方法。
OnImageAvailable的定义(片段的一部分,而非CameraActivity,如谷歌的样本):
OnImageAvailable()
来自Android Monitor的崩溃日志:
public final ImageReader.OnImageAvailableListener mOnImageAvailableListener
= new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader reader) {
Image image = null;
try {
image = reader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] imageBytes = new byte[buffer.remaining()];
buffer.get(imageBytes);
final Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
textView.append("a"); // crashes here
} finally {
if (image != null) {
image.close();
}
}
}
};
答案 0 :(得分:0)
你需要把它放在一个runnable / thread中。我并不是真的精通它,但也许这可能会有所帮助,但如果它没有,我更确定它指向正确的解决方案
public final ImageReader.OnImageAvailableListener mOnImageAvailableListener
= new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(final ImageReader reader) {
Image image = null;
try {
image = reader.acquireLatestImage();
ByteBuffer buffer = image.getPlanes()[0].getBuffer();
byte[] imageBytes = new byte[buffer.remaining()];
buffer.get(imageBytes);
final Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
YourClassNameHere.this.runOnUIThread(new Runnable() {
@Override
public void run() {
textView.append("a"); // crashes here
});
} finally {
if (image != null) {
image.close();
}
}
}
}
};