简体   繁体   English

将捕获的图像从Camera2 Api传递到另一个活动

[英]Passing captured Image from Camera2 Api to another Activity

I have just to use Camera2 API(using the code design of https://github.com/googlesamples/android-Camera2Basic ) to build an application. 我只需要使用Camera2 API(使用https://github.com/googlesamples/android-Camera2Basic的代码设计)来构建应用程序。 Earlier, i used camera API to do the same, and the results were quite good. 早些时候,我使用Camera API进行了同样的操作,并且效果相当不错。 Right now, I am able to capture the image and save it in File. 现在,我可以捕获图像并将其保存在File中。 I am stuck at this point : I want to send the captured image to another activity in which i want to display the image in an imageview . 我被困在这一点上: 我想将捕获的图像发送到另一个活动,在该活动中我想在imageview中显示该图像 Unlike Camera API, Camera2 Api is quite cumbersome dealing with this. 与Camera API不同,Camera2 Api处理起来很麻烦。 This is a sample of the code where i am trying to pass the inten, ie right after i get the output variable set, i would like to starActivity, to pass it on to the next Activity. 这是我尝试传递意图的代码示例,即在我获得输出变量集之后,我想将starActivity传递给下一个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();
                }
            }
        }
    }

}

This is where(in the camera2Basic code) i want to pass the activity call, to the next activity. 这是我想将活动调用传递到下一个活动的位置(在camera2基本代码中)。 This issue am facing here, is that there isnt any Context variable, or instance of the Main activity Available in this class, and as the enclosing method is static, i am not able to start the activity/pass the intent. 这里面临的问题是,此类中没有任何Context变量或Main活动的实例,并且由于封闭方法是静态的,因此我无法启动活动/传递意图。

i have updated it with the code which i am using, but i am looking for how i can transfer between activities when i use camera2 API. 我已经用我正在使用的代码更新了它,但是我正在寻找当我使用camera2 API时如何在活动之间进行转换。 cause in camera api, it is easy. 造成相机api中的问题,这很容易。 the problem is the run() function runs on a UI thread, which inturn is called when the ImageReader has been initialized. 问题是run()函数在UI线程上运行,而ImageReader初始化后又调用该线程。 The whole code is just the Camera2Basic code. 整个代码只是Camera2Basic代码。 the only thing am trying to add is to transfer between activities and send the image captured to the new activity. 唯一要添加的就是在活动之间进行转移,并将捕获的图像发送到新活动。

Would be great if i could get an idea how to proceed. 如果我能知道如何进行,那就太好了。 Thank you! 谢谢!

Add a context variable when you instantiate the ImageSaver class. 实例化ImageSaver类时,添加一个上下文变量。

Context mContext;

ImageSaver(Image image, File file, Context context) {
    mImage = image;
    mFile = file;
    mContext = context;
}

Then change 然后改变

 startActivity(i);

to

Intent i = new Intent(mContext, next activity)  
((Activity)mContext).startActivity(i);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM