日期:2014-05-20  浏览次数:20846 次

求一个比较好的jpg图片高宽调整代码
以下是我现在使用的一段调整高宽的代码,但是他的效率,实在是,太低了,虽然最后调整后的图片质量很高,但是还是受不了他的效率,请问有没有比较好的且效率很高的代码,或者有关的库?麻烦顺便贴出使用方法,谢谢,谢谢~

Java code
package cn.hlgc.util; 

import java.awt.image.BufferedImage;

import org.apache.log4j.Logger;

public class ResizePicUtil {
private int width;
private int height;
private int scaleWidth;
double support = (double) 3.0;
double PI = (double) 3.14159265358978;
double[] contrib;
double[] normContrib;
double[] tmpContrib;
int startContrib, stopContrib;
int nDots;
int nHalfDots;

private static Logger logger = Logger.getLogger(ResizePicUtil.class);

public BufferedImage imageZoomOut(BufferedImage srcBufferImage, int w, int h) {
width = srcBufferImage.getWidth();
height = srcBufferImage.getHeight();
scaleWidth = w;
logger.info("原图宽度为[" + width + "px]...");
logger.info("原图高度为[" + height + "px]...");
if (DetermineResultSize(w, h) == 1) {
logger.info("不需要将图片进行缩小...");
return srcBufferImage;
}
logger.info("计算缩小图片的宽度和高度...");
double wh = ((double) width) / ((double) height);
double hw = ((double) height) / ((double) width);
if (wh > hw) {
h = (int) (w * hw);
} else {
w = (int) (h * wh);
}
logger.info("图片宽度调整为[" + w + "px]...");
logger.info("图片高度调整为[" + h + "px]...");
long begin = System.currentTimeMillis();
CalContrib();
BufferedImage pbOut = HorizontalFiltering(srcBufferImage, w);
BufferedImage pbFinalOut = VerticalFiltering(pbOut, h);
long end = System.currentTimeMillis();
logger.info("图片高宽调整完毕,耗时[" + (end - begin) + "]毫秒...");
return pbFinalOut;
}

private int DetermineResultSize(int w, int h) {
double scaleH, scaleV;
scaleH = (double) w / (double) width;
scaleV = (double) h / (double) height;
if (scaleH >= 1.0 && scaleV >= 1.0) {
return 1;
}
return 0;
}

private double Lanczos(int i, int inWidth, int outWidth, double Support) {
double x;

x = (double) i * (double) outWidth / (double) inWidth;

return Math.sin(x * PI) / (x * PI) * Math.sin(x * PI / Support)
/ (x * PI / Support);

}

private void CalContrib() {
nHalfDots = (int) ((double) width * support / (double) scaleWidth);
nDots = nHalfDots * 2 + 1;
try {
contrib = new double[nDots];
normContrib = new double[nDots];
tmpContrib = new double[nDots];
} catch (Exception e) {
System.out.println("init  contrib,normContrib,tmpContrib" + e);
}

int center = nHalfDots;
contrib[center] = 1.0;

double weight = 0.0;
int i = 0;
for (i = 1; i <= center; i++) {
contrib[center + i] = Lanczos(i, width, scaleWidth, support);
weight += contrib[center + i];
}

for (i = center - 1; i >= 0; i--) {
contrib[i] = contrib[center * 2 - i];
}

weight = weight * 2 + 1.0;

for (i = 0; i <= center; i++) {
normContrib[i] = contrib[i] / weight;
}

for (i = center + 1; i < nDots; i++) {
normContrib[i] = normContrib[center * 2 - i];
}
}

// 处理边缘
private void CalTempContrib(int start, int stop) {
double weight = 0;

int i = 0;
for (i = start; i <= stop; i++) {
weight += contrib[i];
}

for (i = start; i <= stop; i++) {