使用 Image.SCALE_SMOOTH算法进行图片压缩
来源:互联网 发布:淘宝免费货源怎么找 编辑:程序博客网 时间:2024/05/21 22:33
/** * */package cn.ffcs.test.img;import java.awt.Image;import java.awt.image.BufferedImage;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.util.Date;import javax.imageio.ImageIO;import com.sun.image.codec.jpeg.ImageFormatException;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;/** * 图片压缩 * @author pengyh * */public class ImageCompress {private final static String sourceImg = "E:/Myeclipse/icity/mytest/WebRoot/data/testpng.png";private final static String destImg = "E:/Myeclipse/icity/mytest/WebRoot/data/dest3.png";private Image img;private int width;private int height;/** * 构造函数。 * @param fileName * @throws IOException */public ImageCompress(String fileName) throws IOException {File file = new File(fileName);img = ImageIO.read(file); // 构造Image对象 width = img.getWidth(null);height = img.getHeight(null);}/** * 按照宽度还是高度进行压缩 * @param w 指定压缩宽度 * @param h 指定压缩高度 * @throws ImageFormatException * @throws IOException */public void compressFix(int w, int h) throws ImageFormatException, IOException{if(width / height > w / h){compressImg(w, (int)(height * w / width));} else {compressImg((int)(width * h / height), h);}}public void compressImg(int w, int h) throws ImageFormatException, IOException{/** * Image.SCALE_SMOOTH 的缩略算法 * 生成缩略图片的平滑度的 * 优先级比速度高 * 生成的图片质量比较好 * 但速度慢 * */BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);//绘制新图时,使用Image.SCALE_SMOOTH算法,压缩后的图片质量相对比较光滑,没有明显的锯齿形//image.getGraphics().drawImage(img, 0, 0, w, h, null); <span style="color:#ff6666;">//---------压缩图片如图1</span>image.getGraphics().drawImage(img.getScaledInstance(w, h, Image.SCALE_SMOOTH), 0, 0, null); <span style="color:#ff6666;">//-------压缩后图片如图2</span>File destFile = new File(destImg);FileOutputStream out = new FileOutputStream(destFile); // 输出到文件流// 可以正常实现bmp、png、gif转jpg JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);encoder.encode(image); // JPEG编码 out.close(); }@SuppressWarnings("deprecation")public static void main(String[] args) throws IOException {System.out.println("图片压缩开始:" + new Date().toLocaleString());ImageCompress compress = new ImageCompress(sourceImg);//compress.compressImg(150, 150); 不根据宽度或高度等比例压缩compress.compressFix(150, 150);//等比例以宽度或高度为基准进行压缩System.out.println("图片压缩结束:" + new Date().toLocaleString());}}
图片1:
图片2:
0 0
- 使用 Image.SCALE_SMOOTH算法进行图片压缩
- 使用 Image.SCALE_SMOOTH算法进行图片压缩
- 图片压缩 SCALE_SMOOTH算法
- Image图片的压缩使用
- Image图片压缩
- 图片(Image)压缩
- 【图片压缩】使用canvas,html5进行图片压缩
- 使用Tiny作为工具进行图片压缩
- 使用ImageMagick对图片进行全面压缩
- java压缩处理Image图片
- Image经典图片压缩方法
- 使用Deflate算法对文件进行压缩和解压缩
- 使用 Intervention/image 对 Laravel 项目中的图片进行处理
- 使用 Intervention/image 对 Laravel 项目中的图片进行处理
- K-Means聚类算法进行压缩图片
- 对图片进行压缩
- 对图片进行压缩
- 对图片进行压缩
- maven构建hibernate入门使用(1)
- tiny6410 开机qt 自启动
- log4j详解与实战
- 【Java】JFrame Helloworld
- 【学习ios之路:Objective-C】NSArray和NSMutableArray
- 使用 Image.SCALE_SMOOTH算法进行图片压缩
- C++ Primer 第四版读书笔记(五)之语句
- 报错:java.lang.OutOfMemoryError: Java heap space
- ubuntu中安装和配置apache2
- Binary Tree Preorder Traversal (Java)
- mfc SetWindowText不起作用
- Andoird 4.2.2的Account Manager原理
- apache commons fileupload 处理文件上传的两种方式(流式和非流式)
- iOS中如何修改系统自带的导航栏的颜色