at.entenbaer.utils.TPAUtils.java Source code

Java tutorial

Introduction

Here is the source code for at.entenbaer.utils.TPAUtils.java

Source

/**
 * This file is part of TexturePoemApp.
 * Created by Matthias Perkonigg, 2015.
 * TexturePoemApp is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *   
 *  TexturePoemApp is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TexturePoemApp.  If not, see <http://www.gnu.org/licenses/>.
 */

package at.entenbaer.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;

import org.opencv.android.Utils;
import org.opencv.core.Mat;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Class that hold utils function for the TexturePoemApp
 */
public class TPAUtils {

    /**
     * Saves a OpenCV Mat to a path inside the TexturePoemApp-Folder in the pictures directory
     * @param mat Image that should be saved
     * @param path path where the image should be saved inside the TexturePoemApp-Folder
     */
    public static void saveMatToBitmap(Mat mat, String path) {
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            String galleryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                    .toString();
            Log.d("galleryPath", galleryPath);

            Bitmap b = Bitmap.createBitmap(mat.cols(), mat.rows(), Bitmap.Config.ARGB_8888);

            Utils.matToBitmap(mat, b);

            File album = new File(galleryPath + "/TexturePoemApp");

            if (!album.isDirectory()) {
                album.mkdirs();
            }

            File f = new File(galleryPath + "/TexturePoemApp/" + path);
            try {
                FileOutputStream fo = new FileOutputStream(f);

                b.compress(Bitmap.CompressFormat.JPEG, 100, fo);

                fo.flush();
                fo.close();
            } catch (IOException e) {
                Log.e("IOException", "not saved");

                e.printStackTrace();
            }

        } else {
            Log.d("Env", "not mounted");
        }
    }

    /**
     * Gets a bitmap from a path inside the TexturePoemApp-Folder
     * @param path path of the bitmap inside the TexturePoemApp-Folder
     * @return Decoded bitmap 
     */
    public static Bitmap getBitmapFromPath(String path) {
        Bitmap b = null;
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            String galleryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                    .toString();

            File f = new File(galleryPath + "/TexturePoemApp/" + path);
            if (f.exists()) {
                b = BitmapFactory.decodeFile(f.getAbsolutePath());
            }
        }

        return b;
    }
}