Saving image back to sdcard after processing in Android+OpenCV [closed]
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();
}
}
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 ?
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)
you need to supply a valid file extension, i.e.
Highgui.imwrite("/path/to/sdcard/my.png", image);
Thank you so much :)