package test;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.awt.image.IndexColorModel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import org.imgscalr.Scalr;
import org.imgscalr.Scalr.Mode;
import com.madgag.gif.fmsware.AnimatedGifEncoder;
import com.madgag.gif.fmsware.GifDecoder;
/**
*
* @Description : gif处理工具类
* @author liuSir
* @date : 2015-3-24上午10:49:15
*/
public class GifUtils {
public static void main(String[] args) throws IOException {
boolean ok = ImageIO.write(createImage(), "gif", new File("C://test.gif"));
System.out.println("success=" + ok);
}
@SuppressWarnings("unused")
static BufferedImage createImage() {
IndexColorModel cm = createIndexColorModel();
BufferedImage im = new BufferedImage(100, 100, BufferedImage.TYPE_BYTE_INDEXED, cm);
Graphics2D g = im.createGraphics();
// g.setColor(new Color(0, 0, 0, 0)); // transparent
// g.fillRect(0, 0, 100, 100);
// g.setColor(Color.RED);
// g.fillRect(0, 0, 50, 50);
// g.setColor(Color.GREEN);
// g.fillRect(50, 50, 50, 50);
// g.dispose();
return im;
}
static IndexColorModel createIndexColorModel() {
BufferedImage ex = new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_INDEXED);
IndexColorModel icm = (IndexColorModel) ex.getColorModel();
int SIZE = 256;
byte[] r = new byte[SIZE];
byte[] g = new byte[SIZE];
byte[] b = new byte[SIZE];
byte[] a = new byte[SIZE];
icm.getReds(r);
icm.getGreens(g);
icm.getBlues(b);
java.util.Arrays.fill(a, (byte) 255);
r[0] = g[0] = b[0] = a[0] = 0; // transparent
return new IndexColorModel(8, SIZE, r, g, b, a);
}
/**
*
* @Description: 根据图片宽度进行压缩
* @throws
* @param srcImgPath 源文件路径
* @param outImgPath 文件输出路径
* @param width 想要压缩的宽度
*/
public static void resizeByWidth(File srcFile, String outImgPath,int width){
try
{
GifDecoder gd = new GifDecoder();
int status = gd.read(new FileInputStream(srcFile));
if (status != GifDecoder.STATUS_OK) {
return;
}
doGifByWidth(outImgPath, width, gd);
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
*
* @Description: 根据图片宽度进行压缩
* @throws
* @param srcImgPath 源文件路径
* @param outImgPath 文件输出路径
* @param width 想要压缩的宽度
*/
public static void resizeByWidth(String srcImgPath, String outImgPath,
int width){
try
{
GifDecoder gd = new GifDecoder();
int status = gd.read(new FileInputStream(new File(srcImgPath)));
if (status != GifDecoder.STATUS_OK) {
return;
}
doGifByWidth(outImgPath, width, gd);
}
catch (Exception e)
{
e.printStackTrace();
}
}
private static void doGifByWidth(String outImgPath, int width, GifDecoder gd)
throws FileNotFoundException
{
AnimatedGifEncoder ge = new AnimatedGifEncoder();
OutputStream outputStream = new FileOutputStream(new File(outImgPath));
ge.start(outputStream);
ge.setRepeat(0);
for (int i = 0; i < gd.getFrameCount(); i++) {
BufferedImage frame = gd.getFrame(i);
int w = frame.getWidth();
int height = frame.getHeight();
double scale = (double) w / (double) width;
height = (int) ((double) height / scale);
BufferedImage rescaled = Scalr.resize(frame, Mode.FIT_EXACT, width, height);
int delay = gd.getDelay(i);
ge.setDelay(delay);
ge.addFrame(rescaled);
}
ge.finish();
try
{
outputStream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
gif缩略图压缩处理maven项目
共22个文件
xml:4个
prefs:3个
java:2个
需积分: 48 11 下载量 76 浏览量
2015-03-24
16:12:05
上传
评论
收藏 12KB RAR 举报
温馨提示
项目有用到图片处理技术,因为有的图片很大,需要压缩处理。之前对gif图片不做处理,最近有个需求,需要做手机端,所以gif图片太大,显然是不合适的。所以要对gif进行压缩处理,用之前的代码压缩,会出现gif图片失帧。显示出来效果大打折扣,所以经过调研,写了点关于gif图片压缩处理的代码。只包含了按照宽度等比缩放的方法,其他都差不多了。供大家指点吐槽
资源推荐
资源详情
资源评论
收起资源包目录
giftest Maven Webapp.rar (22个子文件)
giftest Maven Webapp
pom.xml 816B
target
test-classes
m2e-jee
web-resources
META-INF
maven
com.caredear
giftest
pom.xml 816B
pom.properties 238B
MANIFEST.MF 114B
classes
test
GifUtils.class 4KB
GifTest.class 673B
.settings
com.genuitec.eclipse.j2eedt.core.prefs 56B
org.eclipse.m2e.core.prefs 90B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.project.facet.core.xml 252B
org.eclipse.wst.common.component 600B
org.eclipse.wst.jsdt.ui.superType.container 49B
.jsdtscope 508B
org.eclipse.jdt.core.prefs 430B
src
test
resources
java
main
webapp
META-INF
MANIFEST.MF 39B
WEB-INF
web.xml 215B
lib
index.jsp 52B
resources
java
test
GifUtils.java 4KB
GifTest.java 687B
.project 2KB
.mymetadata 328B
.classpath 564B
共 22 条
- 1
资源评论
夜华_
- 粉丝: 2
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 多机调度问题贪心算法:理论探索与实践应用.zip
- 探索tecreate:软件开发的未来之星.zip
- 打标机项目C#源码连接扫码
- 基于SSM的房屋租赁系统的设计与实现
- xyctf:从入门到精通的实用指南.zip
- mmqrcode1714153659780.png
- Screenshot_2024-04-27-06-08-58-486_com.baidu.xin.aiqicha.jpg
- 基于Javaweb+Tomcat+MySQL的大学生公寓管理系统+sql文件.zip
- 实训作业基于javaweb的订单管理系统源码+数据库+实训报告.zip
- 多机调度问题贪心算法基于最小堆和贪心算法求解多机调度问题.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功