gpt4 book ai didi

java - Android PictureCallback需要先执行然后继续其他代码

转载 作者:行者123 更新时间:2023-12-01 12:46:50 27 4
gpt4 key购买 nike

我有可以拍摄照片的代码,但是在它到达 PictureCallback() 后,它会继续按代码方式运行,最终在获得字节数组之前执行其他代码。有什么办法可以阻止这种情况吗?它只需要几毫秒,而且我不介意停止 UI 线程。

private static byte[] patronImage;
.
.
.
takePatronPicture();
//I want to do something with patronImage here, but the next line
//gets executed almost immediately while getJpegCallback is still
//doing its thing.
code that does something with patronImage;
.
.
.
public void takePatronPicture(){
if (camera != null) {
try {
SurfaceView surfaceView = (SurfaceView)getView().findViewById(R.id.surfaceView1);
camera.setPreviewDisplay(surfaceView.getHolder());
camera.startPreview();
camera.takePicture(null, null, getJpegCallback);
}
catch(Exception e) {
e.printStackTrace();
}
}
}

final PictureCallback getJpegCallback = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
try {
patronImage = data;

camera.release();
camera = null;
} catch (final Exception e) {
e.printStackTrace();
}
}
};

最佳答案

回调的目的是在完成后它会调用您。如果您要运行需要操作数据的其他代码,请将其放入回调中。您不会卡住当前线程来等待它。事实上,如果操作将事件发送到主线程的循环程序,这样做可能会导致死锁。

关于java - Android PictureCallback需要先执行然后继续其他代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24620342/

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