我只需使用Camera2 API(使用https://github.com/googlesamples/android-Camera2Basic的代码设计)来构建一个应用程序。早些时候,我使用相机API做了同样的事情,结果相当不错。现在,我可以捕获图像并将其保存在文件中。 我被困在这一点上: 我想将捕获的图像发送到另一个我想在 ImageView 中显示图像的 Activity 。与Camera API不同,Camera2 Api处理这个问题相当麻烦。 这是我试图传递 Intent 的代码示例,即在我获得输出变量集之后,我想启动 Activity,将其传递到下一个 Activity。 :
/**
* Saves a JPEG {@link Image} into the specified {@link File}.
*/
private static class ImageSaver implements Runnable {
/**
* The JPEG image
*/
private final Image mImage;
/**
* The file we save the image into.
*/
private final File mFile;
ImageSaver(Image image, File file) {
mImage = image;
mFile = file;
}
@Override
public void run() {
ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
FileOutputStream output = null;
try {
output = new FileOutputStream(mFile);
output.write(bytes);
Intent i = new Intent(current activity, next activity)
startActivity(i);
} catch (IOException e) {
e.printStackTrace();
} finally {
mImage.close();
if (null != output) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
这是我想将 Activity 调用传递到下一个 Activity 的位置(在camera2Basic代码中)。这里面临的问题是,此类中没有任何 Context 变量或可用的主 Activity 实例,并且由于封闭方法是静态的,我无法启动 Activity/传递 Intent 。
我已经用我正在使用的代码更新了它,但我正在寻找当我使用camera2 API时如何在 Activity 之间进行传输。因为在相机 API 中,这很容易。问题是 run() 函数在 UI 线程上运行,而该函数又在 ImageReader 初始化时被调用。整个代码只是 Camera2Basic 代码。我唯一想要添加的是在 Activity 之间传输并将捕获的图像发送到新 Activity 。
如果我能知道如何继续,那就太好了。 谢谢!
最佳答案
实例化 ImageSaver 类时添加上下文变量。
Context mContext;
ImageSaver(Image image, File file, Context context) {
mImage = image;
mFile = file;
mContext = context;
}
然后改变
startActivity(i);
至
Intent i = new Intent(mContext, next activity)
((Activity)mContext).startActivity(i);
关于java - 将捕获的图像从 Camera2 Api 传递到另一个 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48707665/