图片压缩

15 篇文章 0 订阅
package com.data.demo;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.CropImageFilter;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import com.data.common.utils.SmtpfException;

public class Imgtools{

/**
* 按照高宽比列压缩图片
* @param img
* @param width
* @param height
* @param os
*/
public static void thumbnail_w_h(File fileimg,int width,int height,
OutputStream os){

BufferedImage bi;
try {
bi = ImageIO.read(fileimg);
double srcWidth = bi.getWidth(); // 源图宽度
double srcHeight = bi.getHeight(); // 源图高度
double scale = -1;
if(width>0){
scale = width/srcWidth;
}
if(height>0){
scale = height / srcHeight;
}if(width>0&&height>0){//按照小比列压缩
scale = height/srcHeight < width/srcWidth ? height/srcHeight:width/srcWidth;
thumbnail(fileimg,(int)(srcWidth*scale),(int)(srcHeight * scale),os);
}
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 按照固定宽高原图压缩
*
* @param fileimg
* @param width
* @param height
* @param os
* @throws IOException
*/
public static void thumbnail(File fileimg, int width, int height, OutputStream os) throws IOException {
BufferedImage bi = ImageIO.read(fileimg);
Image image = bi.getScaledInstance(width, height, Image.SCALE_SMOOTH);
BufferedImage tag =new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.setColor(Color.RED);
g.drawImage(image, 0, 0, null);
g.dispose();//释放Graphics 对象。
ImageIO.write(tag, "JPEG", os);
}
/**
* 按照宽高剪接
* @param args
*/
public static void cut_w_h(File fileimg, int destwidth,int desthegiht,OutputStream os){
cut_w_h(fileimg, 0, 0, destwidth, desthegiht, os);
}
public static void cut_w_h(File fileimg, int width, int height, int destwidth,int desthegiht,OutputStream os){
Image img;
ImageFilter filter;
//读取源图片
try {
BufferedImage b1 =ImageIO.read(fileimg);
int srcWidth = b1.getWidth(); // 源图宽度
int srcHeight = b1.getHeight(); // 源图高度
if(srcWidth>=destwidth&&srcHeight>=desthegiht){
Image image = b1.getScaledInstance(srcWidth, srcHeight, Image.SCALE_DEFAULT);//创建此图像的缩放版本。
filter = new CropImageFilter(width, height,destwidth ,desthegiht);//裁剪图像
img = Toolkit.getDefaultToolkit().createImage( new FilteredImageSource(image.getSource(), filter));
BufferedImage tag = new BufferedImage(destwidth, desthegiht,
BufferedImage.TYPE_INT_RGB);
Graphics g = tag.getGraphics();
g.drawImage(img, 0, 0, null); // 绘制截取后的图
g.dispose();
// 输出为文件
ImageIO.write(tag, "JPEG", os);
}


} catch (IOException e) {
e.printStackTrace();
}


}
public static void main(String[] args) throws IOException {
File img = new File("e:\\a\\a.jpg");
FileOutputStream fos = new FileOutputStream("e:\\a\\b.jpg");
//Imgtools.thumbnail(img, 100, 100, fos);
// Imgtools.cut_w_h(img, 230, 200, fos);
Imgtools.thumbnail_w_h(img, 80,20, fos);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值