* This file is part of JuniperBotJ.
 * JuniperBotJ 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.

 * JuniperBotJ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with JuniperBotJ. If not, see <http://www.gnu.org/licenses/>.
package ru.caramel.juniperbot.web.service;

import lombok.Getter;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class OpenCVService {

    private static final Logger LOGGER = LoggerFactory.getLogger(OpenCVService.class);

    private boolean initialized;

    public void init() {
        try {
            initialized = true;
        } catch (Exception e) {
            LOGGER.info("OpenCV initialization failed", e);

    public BufferedImage blur(BufferedImage source, int radius) throws IOException {
        if (!initialized) {
            throw new IOException("OpenCV unavailable");
        Mat sourceMat = getMat(source);
        Mat destination = new Mat(sourceMat.rows(), sourceMat.cols(), sourceMat.type());
        Imgproc.GaussianBlur(sourceMat, destination, new Size(radius, radius), 0);
        return getImage(destination);

    public static Mat getMat(BufferedImage image) throws IOException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ImageIO.write(image, "jpg", byteArrayOutputStream);
        return Imgcodecs.imdecode(new MatOfByte(byteArrayOutputStream.toByteArray()), Imgcodecs.CV_LOAD_IMAGE_UNCHANGED);

    public static BufferedImage getImage(Mat matrix) throws IOException {
        MatOfByte mob = new MatOfByte();
        Imgcodecs.imencode(".jpg", matrix, mob);
        return ImageIO.read(new ByteArrayInputStream(mob.toArray()));