压缩图片工具

服务器同时为Android端和浏览器端进行图片传输,有时图片尺寸大,为节省流量,压缩尺寸发送。

package util;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/**
* 本段代码用于转换图片大小的工具类
*/
public class GetImageWidth_Height {
/*
*
*/

public static void main(String[] args) {
try {
BufferedImage img1 = ImageIO.read(new File("e:/love2.jpg"));
System.out.println(img1.getWidth() + "*" + img1.getHeight());
ImageIcon icon1 = new ImageIcon(img1);
System.out.println(icon1.getIconWidth() + "*" + icon1.getIconHeight());
Image img2 = img1.getScaledInstance(img1.getWidth() / 2, img1.getHeight() / 2,Image.SCALE_AREA_AVERAGING);
ImageIcon icon2 = new ImageIcon(img2);
System.out.println(icon2.getIconWidth() + "*" + icon2.getIconHeight());

BufferedImage bImage = new BufferedImage(img2.getWidth(null), img2.getHeight(null), BufferedImage.TYPE_INT_BGR);
Graphics bg = bImage.getGraphics();
bg.drawImage(img2, 0, 0, null);
bg.dispose();

ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(bImage, "jpg", out);

byte[] abc = out.toByteArray();

File file=new File("e:/love3.jpg");
FileOutputStream fos=new FileOutputStream(file);
fos.write(abc);

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

/**
* 这是根据一串二进制数据读取图片并按照指定输入的大小进行缩放的方法
* @param width 希望生成图片的长
* @param height 希望生成图片的宽
* @param img 图片的二进制数据
* @return 缩放后图片的二进制数据
* @throws Exception
*/
public static byte[] getImage(int width,int height, byte[] img) throws Exception{
BufferedImage imgTemp=ImageIO.read(new BufferedInputStream(new ByteArrayInputStream(img)));
Image imgTemp2=imgTemp.getScaledInstance(width,height,BufferedImage.TYPE_INT_BGR);
BufferedImage bImage = new BufferedImage(imgTemp2.getWidth(null), imgTemp2.getHeight(null), BufferedImage.TYPE_INT_BGR);
Graphics bg = bImage.getGraphics();
bg.drawImage(imgTemp2, 0, 0, null);
bg.dispose();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(bImage, "jpg", out);

return out.toByteArray();
}

/**
* 这是根据一串二进制数据读取图片并按照指定输入的缩小倍数进行缩放的方法
* @param num
* @param img
* @return 缩放后图片的二进制数据
* @throws Exception
*/
public static byte[] getImage(int num,byte[] img) throws Exception{
BufferedImage imgTemp=ImageIO.read(new BufferedInputStream(new ByteArrayInputStream(img)));
Image imgTemp2=imgTemp.getScaledInstance(imgTemp.getWidth()/num,imgTemp.getHeight()/num,BufferedImage.TYPE_INT_BGR);
BufferedImage bImage = new BufferedImage(imgTemp2.getWidth(null), imgTemp2.getHeight(null), BufferedImage.TYPE_INT_BGR);
Graphics bg = bImage.getGraphics();
bg.drawImage(imgTemp2, 0, 0, null);
bg.dispose();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(bImage, "jpg", out);

return out.toByteArray();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值