简体   繁体   中英

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. Earlier, i used camera API to do the same, and the results were quite good. Right now, I am able to capture the image and save it in 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 . Unlike Camera API, Camera2 Api is quite cumbersome dealing with this. 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. :

 /**
 * 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. 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.

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. cause in camera api, it is easy. the problem is the run() function runs on a UI thread, which inturn is called when the ImageReader has been initialized. The whole code is just the Camera2Basic code. 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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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