gpt4 book ai didi

java - Android Things 上 ImageReader.OnImageAvailableListener 始终失败并显示 "buffer has been freed"的原因可能是什么

转载 作者:行者123 更新时间:2023-12-02 10:45:55 28 4
gpt4 key购买 nike

我想使用官方 Raspberry Pi 相机拍照并将其保存到临时文件夹中,以将其用作嵌入式 http (nanoHttp) 服务器的资源,以便在当前版本的 Android 上运行的网页上显示它事情。

问题是,我距离目标还很远。我知道这通常是一个 Android 问题,但对我来说,如果没有屏幕来测试其他场景,调试起来相对困难。

我的 ImageReader.OnImageAvailableListener onImageAvailableListener 中总是出现“缓冲区已被释放”异常(请参阅代码 fragment )。

我不知道为什么。也许相机有问题?我的代码是不是有一堆可怕的错误?

代码

private ImageReader.OnImageAvailableListener onImageAvailableListener = new ImageReader.OnImageAvailableListener()
{
@Override
public void onImageAvailable(ImageReader imageReader)
{
Log.d(TAG, "Camera image listener triggered");

try
{
Image image = imageReader.acquireLatestImage();
ByteBuffer byteBuffer = image.getPlanes()[0].getBuffer();
byteBuffer.rewind();
image.close();

File file = File.createTempFile("test.jpg", null, getApplicationContext().getCacheDir());

if (!file.exists())
{
file.createNewFile();
}

BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, false));
bufferedWriter.write(byteBuffer.asCharBuffer().array());
bufferedWriter.close();

Log.d(TAG, "Camera image listener wrote to file: " + file.getAbsolutePath());
}
catch (IOException e)
{
Log.e(TAG, "Camera image saving failed with error; " + e.getLocalizedMessage());
}
}
};

错误

E/AndroidRuntime: FATAL EXCEPTION: CameraThread
Process: com.example.app, PID: 2634
java.lang.IllegalStateException: buffer has been freed
at java.nio.DirectByteBuffer.asCharBuffer(DirectByteBuffer.java:428)
at com.example.org.MainActivity$1.onImageAvailable(MainActivity.java:115)
at android.media.ImageReader$ListenerHandler.handleMessage(ImageReader.java:812)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)

更新

@nick-felker 感谢您帮助我!我也尝试从office学习Android Things image classifier example ,但没有成功。

如果正确识别相机,我会得到一堆控制台输出,因为我仍然收到错误:

D/TobbotActivity: Camera - onImageAvailable triggered
W/ImageReader_JNI: Unable to acquire a buffer item, very likely client tried to acquire more than maxImages buffers
W/AndroidMediaUtils: Image_getJpegSize: No JPEG header detected, defaulting to size=width=480252
E/AndroidRuntime: FATAL EXCEPTION: BackgroundThread
Process: io.github.tscholze.tobbot, PID: 2492
java.lang.UnsupportedOperationException
at java.nio.CharBuffer.array(CharBuffer.java:713)
at io.github.tscholze.tobbot.TobbotActivity.onImageAvailable(TobbotActivity.java:238)
at android.media.ImageReader$ListenerHandler.handleMessage(ImageReader.java:812)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.os.HandlerThread.run(HandlerThread.java:65)

也许你可以看看 class on Github .

对于如何解决我的问题的每一个想法,我都会非常感激。 :)

最佳答案

我会等待您的操作结束后关闭所有内容。事实上,最好预先声明可关闭变量,然后在 finally block 中将它们全部关闭,这样您就可以确保它们关闭,无论您的中间出现任何异常。代码。它还将有助于确保您的代码流仅在您确定不再使用资源时才关闭资源。

Image image = null;
File file = null;
BufferedWriter bufferedWriter = null;

try {
image = imageReader.acquireLatestImage();
ByteBuffer byteBuffer = image.getPlanes()[0].getBuffer();
byteBuffer.rewind();

file = File.createTempFile("test.jpg", null, getApplicationContext().getCacheDir());

if (!file.exists()) {
file.createNewFile();
}

bufferedWriter = new BufferedWriter(new FileWriter(file, false));
bufferedWriter.write(byteBuffer.asCharBuffer().array());

Log.d(TAG, "Camera image listener wrote to file: " + file.getAbsolutePath());
} catch (IOException e) {
Log.e(TAG, "Camera image saving failed with error; " + e.getLocalizedMessage());
} finally {
if (image != null) {
image.close();
}
if (file != null) {
file.close();
}
if (bufferedWriter != null) {
bufferedWriter.close();
}
}

关于java - Android Things 上 ImageReader.OnImageAvailableListener 始终失败并显示 "buffer has been freed"的原因可能是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52578593/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com