Saving image back to sdcard after processing in Android+OpenCV [closed]

asked 2015-07-16 00:12:18 -0600

VPallavi gravatar image

I want to save the image after processing it, into the same location in sdcard. Initially I converted the image into 'Mat' for processing and now if I'm writing it directly into the same location but it's saving as a file containing some random values. I'm using the following code for writing.

private void write(String fpath, String fname, Mat image, boolean mode) {
// TODO Auto-generated method stub
try {
    File savefile = new File(fpath, "/"+fname);
    FileOutputStream fout = null;
    try {
        fout = new FileOutputStream(savefile,mode);
    } catch (FileNotFoundException e2) {
        // TODO Auto-generated catch block
        e2.printStackTrace();
    }
    PrintWriter pr = new PrintWriter(fout);
    for (int i = 0; i < image.width(); i++){
        for(int j = 0; j < image.height(); j++)
        {
        pr.print(image+"," );
        //pr.print("\n");
    }
    }

    pr.close();
} catch (Exception e) {
    e.printStackTrace();
}

}

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by berak
close date 2015-07-16 09:15:17.954678

Comments

you can't dump a Mat to disk using a FileStream, and hope, that an image appears magically on your sdcard.

please try to use imwrite() instead ?

berak gravatar imageberak ( 2015-07-16 00:22:29 -0600 )edit

I used imwrite also but got following error::

07-16 11:22:19.924: E/cv::error()(32492): OpenCV Error: Unspecified error (could not find a writer for the specified extension) in bool cv::imwrite_(const string&, const cv::Mat&, const std::vector<int>&, bool), file /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/highgui/src/loadsave.cpp, line 275 07-16 11:22:19.934: E/org.opencv.highgui(32492): highgui::imwrite_11() caught cv::Exception: /hdd2/buildbot/slaves/slave_ardbeg1/50-SDK/opencv/modules/highgui/src/loadsave.cpp:275: error: (-2) could not find a writer for the specified extension in function bool cv::imwrite_(const string&, const cv::Mat&, const std::vector<int>&, bool)

VPallavi gravatar imageVPallavi ( 2015-07-16 04:16:57 -0600 )edit

you need to supply a valid file extension, i.e. Highgui.imwrite("/path/to/sdcard/my.png", image);

berak gravatar imageberak ( 2015-07-16 04:23:39 -0600 )edit
1

Thank you so much :)

VPallavi gravatar imageVPallavi ( 2015-07-16 04:42:22 -0600 )edit