GifUtil

来源:互联网 发布:门店经营数据 编辑:程序博客网 时间:2024/06/04 20:03
package com.tong.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Random;

import javax.imageio.ImageIO;

import com.madgag.gif.fmsware.AnimatedGifEncoder;

public class GifUtil {
    static Random r = new Random();

    static int delay = 250; // 延时
    static int angle = 20; // 角度
    static int repeat = 6; // 间隔

    public static void main(String[] args) throws IOException {
        FileInputStream in = new FileInputStream("d:\\灯笼.jpg");
        FileOutputStream out = new FileOutputStream("d:\\denglong.gif");
        write(in, out, "青春不解风情".toCharArray());
    }

    public static void write(InputStream img, OutputStream out, char[] cs) throws IOException {
        try {
            BufferedImage s = ImageIO.read(img);
            AnimatedGifEncoder encoder = new AnimatedGifEncoder();
            encoder.setRepeat(0);
            encoder.start(out);
            try {
                addFrames(encoder, s, angle, repeat, delay, cs);
            } catch (IOException e) {
                e.printStackTrace();
            }
            encoder.finish();
        } catch (IOException e) {
            throw e;
        } finally {
            close(img, out);
        }
    }

    private static void close(Closeable... stream) {
        for (Closeable cloneable : stream) {
            if (cloneable != null) {
                try {
                    cloneable.close();
                } catch (IOException e) {
                }
            }
        }
    }

    private static void addFrames(AnimatedGifEncoder e, BufferedImage s, int chushijiaodu, int ci, int delay, char[] cs)
            throws IOException {
        BufferedImage[][] x = new BufferedImage[cs.length][ci * 2 + 1];
        for (int i = 0; i < cs.length; i++) {
            x[i] = getFromCharachers(s, chushijiaodu, ci, cs[i]);
        }
        BufferedImage[] xx = new BufferedImage[ci * 2 + 1];
        for (int i = 0; i < ci * 2 + 1; i++) {
            BufferedImage[] arr = new BufferedImage[cs.length];
            for (int j = 0; j < arr.length; j++) {
                arr[j] = x[j][i];
            }
            xx[i] = hebing(arr);
        }
        e.setDelay(delay);

        for (int i = 0; i < xx.length; i++) {
            e.addFrame(xx[i]);
            e.setDelay(delay);
        }
        for (int i = xx.length - 2; i >= 1; i--) {
            e.addFrame(xx[i]);
            e.setDelay(delay);
        }

    }

    private static BufferedImage[] getFromCharachers(BufferedImage ori, int chushijiaodu, int ci, char c)
            throws IOException {
        BufferedImage s = new BufferedImage(ori.getWidth(), ori.getHeight(), ori.getType());
        Graphics g = s.getGraphics();
        g.drawImage(ori, 0, 0, null);
        g.setColor(new Color(r.nextInt(64), 64 + r.nextInt(128), 128 + r.nextInt(64)));
        g.setFont(new Font("微软雅黑", Font.ITALIC, 18)); // 字体
        g.drawString(Character.toString(c), s.getWidth() / 2 - 8, s.getHeight() / 2 ); // 字的高度
        BufferedImage[] x = new BufferedImage[ci * 2 + 1];
        x[ci] = s;
        for (int i = 0; i < ci; i++) {
            x[i] = rotateImg(s, chushijiaodu - chushijiaodu / ci * i, Color.white);
            x[x.length - 1 - i] = rotateImg(s, chushijiaodu / ci * i - chushijiaodu, Color.white);
        }

        return x;
    }

    private static BufferedImage hebing(BufferedImage[] x) {
        int width = 0;
        int height = 0;

        for (BufferedImage every : x) {
            width += every.getWidth();
            height = Math.max(height, every.getHeight());
        }
        BufferedImage i = new BufferedImage(width, height, x[0].getType());
        int w = 0;
        for (BufferedImage every : x) {
            i.createGraphics().drawImage(every, w, 0, null);
            w += every.getWidth();
        }

        return i;
    }

    public static BufferedImage rotateImg(BufferedImage image, int degree, Color bgcolor) throws IOException {
        int iw = image.getWidth();// 原始图象的宽度
        int ih = image.getHeight();// 原始图象的高度
        int w = 0;
        int h = 0;
        int x = 0;
        int y = 0;
        degree = degree % 360;
        if (degree < 0)
            degree = 360 + degree;// 将角度转换到0-360度之间
        double ang = Math.toRadians(degree);// 将角度转为弧度

        /**
         * 确定旋转后的图象的高度和宽度
         */

        if (degree == 180 || degree == 0 || degree == 360) {
            w = iw;
            h = ih;
        } else if (degree == 90 || degree == 270) {
            // w = ih;
            // h = iw;
            w = iw;
            h = ih;
        } else {
            // int d = iw + ih;
            // w = (int) (d * Math.abs(Math.cos(ang)));
            // h = (int) (d * Math.abs(Math.sin(ang)));
            w = iw;
            h = ih;
        }

        x = (w / 2) - (iw / 2);// 确定原点坐标
        y = (h / 2) - (ih / 2);
        BufferedImage rotatedImage = new BufferedImage(w, h, image.getType());
        Graphics2D gs = (Graphics2D) rotatedImage.getGraphics();
        if (bgcolor == null) {
            rotatedImage = gs.getDeviceConfiguration().createCompatibleImage(w, h, Transparency.TRANSLUCENT);
        } else {
            gs.setColor(bgcolor);
            gs.fillRect(0, 0, w, h);// 以给定颜色绘制旋转后图片的背景
        }

        AffineTransform at = new AffineTransform();
        at.rotate(ang, w / 2, h / 2);// 旋转图象
        at.translate(x, y);
        AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BICUBIC);
        op.filter(image, rotatedImage);
        return rotatedImage;
    }

}

0 0
原创粉丝点击