1 | initial version |
Like others pointed FileStorage is not available on Android, so in order to avoid using the Android NDK I just save it to a Bitmap.
Here's the code I use:
/**
* Saves a Mat to the SD card application folder as a jpg.
*
* @param source The image to save.
* @param filename The name of the file to be saved.
* @param directoryName The directory where the
* @param ctx The activity context.
* @param colorConversion The openCV color conversion to apply to the image. -1 will use no color conversion.
*/
public void saveImageToDisk(Mat source, String filename, String directoryName, Context ctx, int colorConversion){
Mat mat = source.clone();
if(colorConversion != -1)
Imgproc.cvtColor(mat, mat, colorConversion, 4);
Bitmap bmpOut = Bitmap.createBitmap(mat.cols(), mat.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(mat, bmpOut);
if (bmpOut != null){
mat.release();
OutputStream fout = null;
String root = Environment.getExternalStorageDirectory().getAbsolutePath();
String dir = root + "/" + ctx.getResources().getString(R.string.app_name) + "/" + directoryName;
String fileName = filename + ".jpg";
File file = new File(dir);
file.mkdirs();
file = new File(dir, fileName);
try {
fout = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fout);
bmpOut.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
bmpOut.recycle();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
bmpOut.recycle();
}
Hope it helps someone.