/*
 * Decompiled with CFR 0.152.
 */
package com.hst.taxService.util;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.font.GlyphVector;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;

public class PhotoWater {
    public static void creatWatermarkImg(String srcPath, String markStr, float alpha) {
        OutputStream os = null;
        try {
            try {
                BufferedImage img = ImageIO.read(new File(srcPath));
                int srcImgWidth = ((Image)img).getWidth(null);
                int srcImgHeight = ((Image)img).getHeight(null);
                BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, 1);
                Graphics2D g = bufImg.createGraphics();
                g.drawImage(img, 0, 0, srcImgWidth, srcImgHeight, null);
                Font font = new Font("\u5b8b\u4f53", 1, 40);
                g.setColor(Color.GRAY);
                g.setFont(font);
                GlyphVector fontGV = font.createGlyphVector(g.getFontRenderContext(), markStr);
                Rectangle size = fontGV.getPixelBounds(g.getFontRenderContext(), 0.0f, 0.0f);
                double textWidth = size.getWidth();
                double textHeight = size.getHeight();
                g.setComposite(AlphaComposite.getInstance(10, alpha));
                g.rotate(Math.toRadians(-45.0), (double)srcImgWidth / 2.0, (double)srcImgHeight / 2.0);
                double rotateLength = Math.abs(Math.sqrt(2.0) * (2.0 * textHeight + textWidth) / 2.0) + 5.0;
                int tmpHeight = 0;
                while (tmpHeight < srcImgHeight) {
                    int tmpWidth = (int)(-rotateLength);
                    while (tmpWidth < srcImgWidth) {
                        PhotoWater.MyDrawString(markStr, tmpWidth, tmpHeight, 1.2, g);
                        tmpWidth = (int)((double)tmpWidth + rotateLength + 50.0);
                    }
                    tmpHeight = (int)((double)tmpHeight + rotateLength - 50.0);
                }
                g.dispose();
                os = new FileOutputStream(srcPath);
                ImageIO.write((RenderedImage)bufImg, "JPG", os);
            }
            catch (IOException e) {
                e.printStackTrace();
                try {
                    if (os != null) {
                        os.close();
                    }
                }
                catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
        }
        finally {
            try {
                if (os != null) {
                    os.close();
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static byte[] creatWatermarkImg(InputStream is, String markStr, float alpha) {
        BufferedImage img = null;
        try {
            img = ImageIO.read(is);
            if (img == null) {
                return null;
            }
            int srcImgWidth = ((Image)img).getWidth(null);
            int srcImgHeight = ((Image)img).getHeight(null);
            BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, 1);
            Graphics2D g = bufImg.createGraphics();
            g.drawImage(img, 0, 0, srcImgWidth, srcImgHeight, null);
            Font font = new Font("\u5b8b\u4f53", 1, 40);
            g.setColor(Color.GRAY);
            g.setFont(font);
            GlyphVector fontGV = font.createGlyphVector(g.getFontRenderContext(), markStr);
            Rectangle size = fontGV.getPixelBounds(g.getFontRenderContext(), 0.0f, 0.0f);
            double textWidth = size.getWidth();
            double textHeight = size.getHeight();
            g.setComposite(AlphaComposite.getInstance(10, alpha));
            g.rotate(Math.toRadians(-45.0), (double)srcImgWidth / 2.0, (double)srcImgHeight / 2.0);
            double rotateLength = Math.abs(Math.sqrt(2.0) * (2.0 * textHeight + textWidth) / 2.0) + 5.0;
            int tmpHeight = 0;
            while (tmpHeight < srcImgHeight) {
                int tmpWidth = (int)(-rotateLength);
                while (tmpWidth < srcImgWidth) {
                    PhotoWater.MyDrawString(markStr, tmpWidth, tmpHeight, 1.2, g);
                    tmpWidth = (int)((double)tmpWidth + rotateLength + 50.0);
                }
                tmpHeight = (int)((double)tmpHeight + rotateLength - 50.0);
            }
            g.dispose();
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write((RenderedImage)bufImg, "JPEG", out);
            out.close();
            img = null;
            byte[] byArray = out.toByteArray();
            return byArray;
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            img = null;
        }
        return null;
    }

    public int getWatermarkLength(String waterMarkContent, Graphics2D g) {
        return g.getFontMetrics(g.getFont()).charsWidth(waterMarkContent.toCharArray(), 0, waterMarkContent.length());
    }

    public static void MyDrawString(String str, int x, int y, double rate, Graphics g) {
        String tempStr = new String();
        int orgStringWight = g.getFontMetrics().stringWidth(str);
        int orgStringLength = str.length();
        int tempx = x;
        int tempy = y;
        while (str.length() > 0) {
            tempStr = str.substring(0, 1);
            str = str.substring(1, str.length());
            g.drawString(tempStr, tempx, tempy);
            tempx = (int)((double)tempx + (double)orgStringWight / (double)orgStringLength * rate);
        }
    }

    public static void main(String[] args) {
        try {
            PhotoWater.creatWatermarkImg("d://test.jpg", "\u676d\u5dde\u6b65\u957f\u79d1\u6280\u6709\u9650\u516c\u53f8", 0.5f);
        }
        catch (Exception exception) {}
    }
}
