[JAVA] Bureau: OpenCV Add WaterMark

Goal
Test OpenCV add watermark.

OpenCV_Chinese_WaterMark.java


import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;

import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class OpenCV_Chinese_WaterMark {

    static {System.loadLibrary(Core.NATIVE_LIBRARY_NAME);}
    private JFrame frmjavaSwing;
    public BufferedImage globalBufferedImage;

    /**
     *  Launch the application.
     */
    public static void main(String[] args){
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try{
                    OpenCV_Chinese_WaterMark window = new OpenCV_Chinese_WaterMark();
                    window.frmjavaSwing.setVisible(true);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     * @throws  IOException
     */
    public OpenCV_Chinese_WaterMark() throws IOException{
        init();
    }


    /**
     *  Init the contents of the frame.
     * @throws  IOException
     */
    private void  init() throws IOException{
        Mat source =  Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\lena.jpg ");
        BufferedImage image=matToBufferedImage(source);

        frmjavaSwing = new JFrame();
        frmjavaSwing.setTitle("pratique de la marque flottante chinoise ouverte,Application Fumiya en anglais");
        frmjavaSwing.setBounds(100, 100, 639, 356);
        frmjavaSwing.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmjavaSwing.getContentPane().setLayout(null);

        final JLabel lblNewLabel = new JLabel("");
        lblNewLabel.setBounds(10, 64, 595, 255);
        lblNewLabel.setIcon(new ImageIcon(image));
        frmjavaSwing.getContentPane().add(lblNewLabel);

        JLabel lblNewLabel_1 = new JLabel("Transparence:");
        lblNewLabel_1.setBounds(10, 21, 46, 15);
        frmjavaSwing.getContentPane().add(lblNewLabel_1);

        final JLabel lblshowAlpha = new JLabel("1.0");
        lblshowAlpha.setBounds(235, 21, 57, 15);
        frmjavaSwing.getContentPane().add(lblshowAlpha);

        final JSlider slider = new JSlider();
        slider.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent arg0) {

                lblshowAlpha.setText((float)slider.getValue()/10+"");
                BufferedImage image=HandleWaterMark((float)slider.getValue()/10);
                lblNewLabel.setIcon(new ImageIcon(image));
                setGlobalBufferedImage(image);

            }
        });
        slider.setValue(10);
        slider.setMaximum(10);
        slider.setBounds(54, 12, 161, 24);
        frmjavaSwing.getContentPane().add(slider);

        JButton btnNewButton = new JButton("Save");
        btnNewButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent arg0) {
                BufferedImage image=getGlobalBufferedImage();
                if (image!=null){
                    try {
                        ImageIO.write(image, "png", new File(
                                "D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\lena_watermark.jpg "));
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

            }
        });
        btnNewButton.setBounds(277, 16, 89, 25);
        frmjavaSwing.getContentPane().add(btnNewButton);

    }

    //Application Fumiya en anglais
    public BufferedImage HandleWaterMark(float alpha){
        Mat source =  Imgcodecs.imread("D:\\projects\\Java\\OpenCV_Samples\\resource\\imgs\\lena.jpg ");
        BufferedImage image=matToBufferedImage(source);
        Graphics2D g2d = image.createGraphics();
        System.out.println(alpha);
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));//Définition de la transparence
        g2d.setColor(new Color(0,0,0));//Couleur des caractères
        g2d.setFont(new Font("Secte Wang Han", 36, 30));//Forme de caractère,Forme de caractèrestyle,Grand et petit
        //g2d.setFont(new Font("Wang Han Sokantei Ryu Shigeru", 36, 20));//Forme de caractère,Forme de caractèrestyle,Grand et petit
        g2d.drawString("Mindful Lake", 10,230);//Position du personnage
        g2d.dispose();
        return image;
    }

    public BufferedImage matToBufferedImage(Mat matrix) {
        int cols = matrix.cols();
        int rows = matrix.rows();
        int elemSize = (int)matrix.elemSize();
        byte[] data = new byte[cols * rows * elemSize];
        int type;
        matrix.get(0, 0, data);
        switch (matrix.channels()) {
            case 1:
                type = BufferedImage.TYPE_BYTE_GRAY;
                break;
            case 3:
                type = BufferedImage.TYPE_3BYTE_BGR;
                // bgr to rgb
                byte b;
                for(int i=0; i<data.length; i=i+3) {
                    b = data[i];
                    data[i] = data[i+2];
                    data[i+2] = b;
                }
                break;
            default:
                return null;
        }
        BufferedImage image2 = new BufferedImage(cols, rows, type);
        image2.getRaster().setDataElements(0, 0, cols, rows, data);
        return image2;
    }

    public BufferedImage getGlobalBufferedImage() {
        return globalBufferedImage;
    }

    public void setGlobalBufferedImage(BufferedImage globalBufferedImage) {
        this.globalBufferedImage = globalBufferedImage;
    }
}
Result
![lena_watermark.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/276243/8ec213ea-f463-0c79-7e57-3ced815a8b63.jpeg)

Recommended Posts

Bureau: OpenCV Add WaterMark
Bureau: OpenCV Add Broad
Bureau: seuil OpenCV
Bureau: OpenCV BilateralFilterBlur
Bureau: OpenCV Dilate
Bureau: OpenCV Expand
Bureau: OpenCV Affine
Bureau: OpenCV Emboss
Bureau: OpenCV CLAHE
Bureau: OpenCV Ellipse2Poly
Bureau: OpenCV HDR
Bureau: Polylignes OpenCV
Bureau: OpenCV Denoise
Bureau: netteté OpenCV
Bureau: OpenCV Concat
Bureau: OpenCV OpenCV_SalonUseBlurAddWeighted
Bureau: OpenCV Mosaic
Bureau: OpenCV Erode
Bureau: OpenCV Denoise
Bureau: Rectangle OpenCV
Bureau: OpenCV Watershed
Bureau: Texte OpenCV
Bureau: OpenCV Inpaint
Bureau: OpenCV NormalizeBlur
Bureau: OpenCV StereoSGBM
Bureau: OpenCV Spot
Bureau: OpenCV Canny
Bureau: OpenCV Denoise3
Bureau: histogramme OpenCV
Bureau: OpenCV Dft
Bureau: OpenCV Decolor
Bureau: OpenCV FaceDetector
Bureau: OpenCV Denoise2
Bureau: OpenCV StereoBM
Bureau: Filtre OpenCV Kirsch
Bureau: Filtre laplacien OpenCV 2
Bureau: Changement d'éclairage OpenCV
Bureau: OpenCV Fill ConvexPoly
Bureau: OpenCV Grab Cut
Bureau: OpenCV Sharpness Gui
Bureau: Changement de couleur OpenCV
Bureau: Filtre Freichennel OpenCV
Bureau: seuil adaptatif OpenCV
Bureau: OpenCV Draw Circle
Bureau: OpenCV Fill Poly
Bureau: OpenCV Mean Filter
Bureau: référentiel Java OpenCV
Bureau: OpenCV Sobel Filter2
Bureau: OpenCV pyrMeanShift Filter
Bureau: OpenCV OpticalFlow PyrLK
Bureau: Piano virtuel OpenCV
Bureau: image de fusion OpenCV
Bureau: Filtre OpenCV Scharr
Bureau: aperçu de la webcam Opencv
Bureau: Filtre laplacien OpenCV
Bureau: Flou médian OpenCV
Bureau: OpenCV Robinson Filter
Bureau: enregistrement vidéo OpenCV
Bureau: Aplatissement de la texture OpenCV
Bureau: OpenCV Flood Fill
Bureau: espace colorimétrique OpenCV