import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
/**
* 图像压缩工具
*
* @author KevinKuang
*
*/
public class ImageSizer
{
public static final MediaTracker tracker = new MediaTracker(
new Component()
{
private static final long serialVersionUID = 1234162663955668507L;
});
/**
*
*
* @param imageData
* 原图像数据
* @param resizedFile
* 压缩后的图像文件
* @param format
* 图片格式 jpg, png, gif(非动画)
* @throws IOException
*/
public void resize(byte [] imageData, File resizedFile,
String format) throws Exception
{
if (format != null && "gif".equals(format.toLowerCase()))
{
return;
}
// ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
// int readLength = -1;
// int bufferSize = 1024;
// byte bytes[] = new byte[bufferSize];
// while ((readLength = fis.read(bytes, 0, bufferSize)) != -1)
// {
// byteStream.write(bytes, 0, readLength);
// }
// byte[] ins = byteStream.toByteArray();
// fis.close();
// byteStream.close();
Image inputImage = Toolkit.getDefaultToolkit().createImage(imageData);
waitForImage(inputImage);
int imageWidth = inputImage.getWidth(null);
/* 图片宽 最大值 */
if(imageWidth > 675)
{
imageWidth = 675;
}
if (imageWidth < 1)
throw new IllegalArgumentException("image width " + imageWidth
+ " is out of range");
int imageHeight = inputImage.getHeight(null);
if (imageHeight < 1)
throw new IllegalArgumentException("image height " + imageHeight
+ " is out of range");
// Create output image.
int height = -1;
double scaleW = (double) imageWidth / (double) imageWidth;
double scaleY = (double) imageHeight / (double) height;
if (scaleW >= 0 && scaleY >= 0) {
if (scaleW > scaleY) {
height = -1;
} else {
imageWidth = -1;
}
}
Image outputImage = inputImage.getScaledInstance(imageWidth, height,
java.awt.Image.SCALE_DEFAULT);
checkImage(outputImage);
encode(new FileOutputStream(resizedFile), outputImage, format);
}
/** Checks the given image for valid width and height. */
private static void checkImage(Image image) throws Exception
{
waitForImage(image);
int imageWidth = image.getWidth(null);
if (imageWidth < 1)
throw new IllegalArgumentException("image width " + imageWidth
+ " is out of range");
int imageHeight = image.getHeight(null);
if (imageHeight < 1)
throw new IllegalArgumentException("image height " + imageHeight
+ " is out of range");
}
/**
* Waits for given image to load. Use before querying image
* height/width/colors.
*/
private static void waitForImage(Image image) throws Exception
{
tracker.addImage(image, 0);
tracker.waitForID(0);
tracker.removeImage(image, 0);
}
/** Encodes the given image at the given quality to the output stream. */
private void encode(OutputStream outputStream, Image outputImage,
String format) throws java.io.IOException
{
int outputWidth = outputImage.getWidth(null);
if (outputWidth < 1)
throw new IllegalArgumentException("output image width "
+ outputWidth + " is out of range");
int outputHeight = outputImage.getHeight(null);
if (outputHeight < 1)
throw new IllegalArgumentException("output image height "
+ outputHeight + " is out of range");
BufferedImage bi = new BufferedImage(outputWidth, outputHeight,
BufferedImage.TYPE_INT_RGB);
Graphics2D biContext = bi.createGraphics();
biContext.drawImage(outputImage, 0, 0, null);
ImageIO.write(bi, format, outputStream);
// System.out.println("----the end ------");
outputStream.flush();
outputStream.close();
}
/**
* 压缩图片
*
* @param ipuntFile
*
* @param outFile
*
* @param postfix
*
* @throws Exception
*/
public void imageCompress(byte[] imageData,File outFile,String postfix) throws Exception
{
resize(imageData,outFile,postfix);
}
}
分享到:
相关推荐
鲁班压缩图片Android Studio开发版,需要java1.8
C# .Net压缩图片,大小仅为原图的1/10
H5 上传图片 拍照 压缩图片 上传 form file 表单提交,直接请求接口就可以了,下载了就能用,自己定义接口请求,实测绝对可用,不能用打我
压缩图片
java代码保存上传的文件_压缩图片
基于JNI压缩图片高效不失真,如果需要压到指定范围,亦可以使用该方法循环压缩。绝比高效,一句话代码基于JNI压缩图片高效不失真,如果需要压到指定范围,亦可以使用该方法循环压缩。绝比高效,一句话代码
图片压缩图片压缩图片压缩图片压缩图片压缩图片图片压缩压缩
图片压缩软件【压缩图片】 图片压缩软件 文件小,压缩能力强
java代码保存宽高不变压缩图片(失真不大),按照文件大小,进行压缩质量控制
Java压缩图片util,可等比例宽高不失真压缩,也可直接指定压缩后的宽高,只能用炫酷来形容,感兴趣就下载看看吧
java中根据设置的宽高等比例压缩图片文件 先保存原文件,再压缩、上传
前端js压缩图片插件,使用方法见博客文章
opencv压缩图片,设置压缩率,因无法设置0资源下载,故提供百度云链接: https://pan.baidu.com/s/1Qw30XwwCXNq1SBqXWmsIvg 密码: u3jq
无损压缩图片 CS脚本,可以直接使用。。。。。。。。。
H5 canvas原理,压缩图片,很硬核,易懂! 创作背景:客户上传的证件照,通常会3、4M,这样会让传输的过程十分缓慢,会给客户不好的体验。设想一点击上传,就要等个十几秒,如果是我,可能会在传输的过程中,就不耐烦...
java各种方式压缩上传图片,包括按指定宽度,高度压缩。指定比例压缩等
目前一般手机的相机都能达到800万像素,像我的Galaxy Nexus才500万像素,拍摄的照片也有1.5M左右。这么大的照片上传到服务器,不仅浪费流量,同时还... 此demo 解决了Android压缩图片到100K以下并保持不失真的高效方法
java 开发压缩图片文件大小,2m-->200k 不是压缩宽高的
用来压缩图片大小的工具
前端本地客户端压缩图片,兼容IOS,Android,PC、自动按需加载文件