java图片处理工具之-ImageMagick+jmagick(二)
2015-08-13 15:07
609 查看
简单的图片处理测试类:
public class ImageUtil {
static{
System.setProperty("jmagick.systemclassloader","no"); //必须
}
/**
* 压缩图片
*/
public void createimage(String filepath,String topath, int width, int height) {
ImageInfo info = null; //加载图片
MagickImage image = null;
MagickImage scaled = null;
Dimension dim=null;
try {
info = new ImageInfo(filepath);
image = new MagickImage(info);
dim = image.getDimension();
double w = dim.getWidth();
double h = dim.getHeight();
int toHeight = (int)(h*width/w);//按宽度比例缩放
scaled = image.scaleImage(width, toHeight );// 小图片文件的大小.
scaled.setFileName(topath+width+height+".jpg"); //设置图片名称,必须得有保存图片路径
scaled.writeImage(info);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(image!=null)
{
image.destroyImages();
}
if (scaled != null) {
scaled.destroyImages();
}
}
}
public static void main(String[] args){
String filepath="d:/xxxx.jpg";
String topath="d:/xxxxx/";
int width=400;
int height=553;
ImageUtil image=new ImageUtil();
image.createimage(filepath, topath, width, height);
}
public class ImageUtil {
static{
System.setProperty("jmagick.systemclassloader","no"); //必须
}
/**
* 压缩图片
*/
public void createimage(String filepath,String topath, int width, int height) {
ImageInfo info = null; //加载图片
MagickImage image = null;
MagickImage scaled = null;
Dimension dim=null;
try {
info = new ImageInfo(filepath);
image = new MagickImage(info);
dim = image.getDimension();
double w = dim.getWidth();
double h = dim.getHeight();
int toHeight = (int)(h*width/w);//按宽度比例缩放
scaled = image.scaleImage(width, toHeight );// 小图片文件的大小.
scaled.setFileName(topath+width+height+".jpg"); //设置图片名称,必须得有保存图片路径
scaled.writeImage(info);
} catch (Exception e) {
e.printStackTrace();
} finally {
if(image!=null)
{
image.destroyImages();
}
if (scaled != null) {
scaled.destroyImages();
}
}
}
public static void main(String[] args){
String filepath="d:/xxxx.jpg";
String topath="d:/xxxxx/";
int width=400;
int height=553;
ImageUtil image=new ImageUtil();
image.createimage(filepath, topath, width, height);
}
相关文章推荐
- 垃圾收集趣史-Java垃圾收集算法
- 如何控制Java中的线程,总结了3种方法...
- Spring之LoadTimeWeaver——一个需求引发的思考
- Spring使用总结
- Java日志框架——Logback的运行过程
- Spring 学习笔记(二)——装配 bean
- Java 获得纯数字格式的时间
- Java - How Garbage Collection works in Java
- NJUPT JAVA语言 综合图形界面程序设计
- Java多线程编程之Runnable、Thread简介
- 了解 Spring Data JPA
- Java post 请求
- eclipse studio 使用
- struts2 中chain、redirect、redirectaction的区别
- java基础知识实例
- Spring_Transactional_Propagation
- java入门--4110:圣诞老人的礼物-Santa Clau’s Gifts
- java jaxb
- spring事务定义中,@Transactional(readOnly = true, propagation = Propagation.NOT_SUPPORTED) 这样定义有问题吗?
- Java设计模式之抽象工厂模式