import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.ConvolveOp;
import java.awt.image.Kernel;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
* 图像压缩工具
* @author lihuoming@sohu.com
*
*/
public class ImageSizer {
public static final MediaTracker tracker = new MediaTracker(new Component() {
private static final long serialVersionUID = 1234162663955668507L;}
);
/**
* @param originalFile 原图像
* @param resizedFile 压缩后的图像
* @param width 图像宽
* @param format 图片格式 jpg, png, gif(非动画)
* @throws IOException
*/
public static void resize(File originalFile, File resizedFile, int width, String format) throws IOException {
if(format!=null && "gif".equals(format.toLowerCase())){
resize(originalFile, resizedFile, width, 1);
return;
}
FileInputStream fis = new FileInputStream(originalFile);
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[] in = byteStream.toByteArray();
fis.close();
byteStream.close();
Image inputImage = Toolkit.getDefaultToolkit().createImage( in );
waitForImage( inputImage );
int imageWidth = inputImage.getWidth( null );
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) width;
double scaleY = (double) imageHeight / (double) height;
if (scaleW >= 0 && scaleY >=0) {
if (scaleW > scaleY) {
height = -1;
} else {
width = -1;
}
}
Image outputImage = inputImage.getScaledInstance( width, 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 ) {
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 ) {
try {
tracker.addImage( image, 0 );
tracker.waitForID( 0 );
tracker.removeImage(image, 0);
} catch( InterruptedException e ) { e.printStackTrace(); }
}
/** Encodes the given image at the given quality to the output stream. */
private static 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" );
// Get a buffered image from the image.
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);
outputStream.flush();
}
/**
* 缩放gif图片
* @param originalFile 原图片
* @param resizedFile 缩放后的图片
* @param newWidth 宽度
* @param quality 缩放比例 (等比例)
* @throws IOException
*/
private static void resize(File originalFile, File resizedFile, int newWidth, float quality) throws IOException {
if (quality < 0 || quality > 1) {
throw new IllegalArgumentException("Quality has to be between 0 and 1");
}
ImageIcon ii = new ImageIcon(originalFile.getCanonicalPath());
Image i = ii.getImage();
Image resizedImage = null;
int iWidth = i.getWidth(null);
int iHeight = i.getHeight(null);
if (iWidth > iHeight) {
resizedImage = i.getScaledInstance(newWidth, (newWidth * iHeight) / iWidth, Image.SCALE_SMOOTH);
} else {
resizedImage = i.getScaledInstance((newWidth * iWidth) / iHeight, newWidth, Image.SCALE_SMOOTH);
}
// This code ensures that all the pixels in the image are loaded.
Image temp = new ImageIcon(resizedImage).getImage();
// Create the buffered image.
BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null),
BufferedImage.TYPE_INT_RGB);
// Copy image to buffered image.
Graphics g = bufferedImage.createGraphics();
// Clear background and paint the image.
g.setColor(Color.white);
g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
g.drawImage(temp, 0, 0, null);
g.dispose();
// Soften.
float softenFactor = 0.05f;
float[] softenArray = {0, softenFactor, 0, softenFactor, 1-(softenFactor*4), softenFactor, 0, softenFactor, 0};
Kernel kernel = new Kernel(3, 3, softenArray);
ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
bufferedImage = cOp.filter(bufferedImage, null);
// Write the jpeg to a file.
FileOutputStream out = new FileOutputStream(resizedFile);
// Encodes image as a JPEG data stream
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);
param.setQuality(quality, true);
encoder.setJPEGEncodeParam(param);
encoder.encode(bufferedImage);
}
}
测试类
import java.io.File;
import java.io.IOException;
import org.junit.Test;
public class ImageTest {
@Test
public void createImage(){
try {
ImageSizer.resize(new File("d:\\100_2844.gif"), new File("d:\\testabc.gif"), 200, "gif");
} catch (IOException e) {
e.printStackTrace();
}
}
}
ex
function getTopSell(typeid){
var salespromotion = document.getElementById('salespromotion');
if(salespromotion && typeid!=""){
salespromotion.innerHTML= "数据正在加载...";
send_request(function(value){salespromotion.innerHTML=value},
"<html:rewrite action="/product/switch"/>?method=topsell&typeid="+ typeid, true);
}
}
分享到:
相关推荐
主要介绍了Android中3种图片压缩处理方法,本文讲解了质量压缩方法、获得缩略图、图片缩放三种方法并分别给出示例代码,需要的朋友可以参考下
java 图片压缩 iphone拍照上传旋转问题处理,压缩工具类 首先导入jar 包,通过imgxz获取图片是否旋转属性,在调用旋转方法,旋转过来,然后在进行压缩
靠谱的图片压缩方法图片压缩.zip,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
在网上调查了图片压缩的方法并实装后,大致上可以认为有两类压缩:质量压缩(不改变图片的尺寸)和尺寸压缩(相当于是像素上的压缩);质量压缩一般可用于上传大图前的处理,这样就可以节省一定的流量,毕竟现在的...
图片处理、图片压缩工具,简单好用,非常方便。压缩使用方法,将需要处理的图片或者存储图片的文件夹直接拖到压缩.bat
Grearo图片压缩工具(修改版) 增加功能: 1,图象压缩平滑处理,插补优化; 2,去除原版本图象压缩后出现马赛克; 3,解决大量图片压缩会出现“程序假死”现象; 功能介绍【必读】: 1 图片...
C# ASPNET实现图片在线压缩和处理等方法 京华志&精华志出品 希望大家互相学习,互相进步 支持CSDN 支持微软 主要包括C# ASP.NET SQLDBA 源码 毕业设计 开题报告 答辩PPT等
项目有用到图片处理技术,因为有的图片很大,需要压缩处理。...显示出来效果大打折扣,所以经过调研,写了点关于gif图片压缩处理的代码。只包含了按照宽度等比缩放的方法,其他都差不多了。供大家指点吐槽
基于DSP的图像压缩与处理方法研究的论文
一个简单易用的JS图片压缩方法
这时就得必备一款优秀的批量图片压缩工具, Caesium 就是其中的一款。软件拥有智能图片压缩算法,压缩率达90%。不仅会把图片压缩得更小,还不会影响图片质量,如果你觉得图片不够小,可以自定义图片质量和像素,包你...
本篇文章主要介绍了Java中图片压缩处理的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
这款图片压缩工具是一款免费的图片处理软件,可以帮你批量压缩图片体积,并且图片的质损非常低,如果你在上传图片到一些论坛提示图片过大的时候可以利用这款工具压缩一下图片再上传就好了。 使用方法: 目前只支持...
网上找半天没有合适的方法处理,最后只能自己写了个方法,现在安卓手机拍照技术已经很高,好点的手机拍照的大小能达到10M左右。封装的工具设计思路,首先做长宽压缩,如果图片太大,先做长宽压缩,暂定大于2M(大小...
作为自媒体流行的年代,谁还不截几张图或者处理几张照片,可如果您面对的是成千上万张图像时如何快速批量调整压缩这才是真正提高工作效率的方法。今天给大家提供的图压中文版就是由国内新小科技开发的免费用于个人和...
Luban(鲁班)——Android图片压缩工具,仿微信朋友圈压缩策略。项目描述目前做app开发总绕不开图片这个元素。但是随着手机拍照分辨率的提升,图片的压缩成为一个很重要的问题。单纯对图片进行裁切,压缩已经有很多...
随着手机拍照分辨率的提升,图片的压缩成为一个很重要的问题。单纯对图片进行裁切,压缩已经... 此处通过python实现的鲁班压缩图片方法是最接近微信的一种压缩算法,方法实现简洁,不需要特殊库处理,可直接进行调用。
本文实例讲述了Android开发之图片压缩实现方法。分享给大家供大家参考,具体如下: 由于Android本身的机制限定 由于系统对每个应用内存分配规则的限制,如果加载过大图片很有可能会导致OOM 即闪退或者卡屏现象 但是...
作为自媒体流行的年代,谁还不截几张图或者处理几张照片,可如果您面对的是成千上万张图像时如何快速批量调整压缩这才是真正提高工作效率的方法。今天给大家提供的图压中文版就是由国内新小科技开发的免费用于个人和...