java 生成缩略图_java生成缩略图的方法示例

本文实例讲述了java生成缩略图的方法。分享给大家供大家参考,具体如下:

package com.util;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import javax.imageio.ImageIO;

/**

* 生成压缩图

*

*/

public class ImageScale {

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;

/**

* Start: Use Lanczos filter to replace the original algorithm for image

* scaling. Lanczos improves quality of the scaled image modify by :blade

*/

public BufferedImage imageZoomOut(BufferedImage srcBufferImage, int w, int h) {

width = srcBufferImage.getWidth();

height = srcBufferImage.getHeight();

scaleWidth = w;

if (DetermineResultSize(w, h) == 1) {

return srcBufferImage;

}

CalContrib();

BufferedImage pbOut = HorizontalFiltering(srcBufferImage, w);

BufferedImage pbFinalOut = VerticalFiltering(pbOut, h);

return pbFinalOut;

}

/**

* 决定图像尺寸

*/

private int DetermineResultSize(int w, int h) {

double scaleH, scaleV;

// update by libra

double wt = w > width ? width : w;

double ht = h > height ? height : h;

scaleH = (double) wt / (double) width;

scaleV = (double) ht / (double) height;

// 需要判断一下scaleH,scaleV,不做放大操作

if (scaleH >= 1.0 && scaleV >= 1.0) {

return 1;

}

return 0;

} // end of DetermineResultSize()

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);

} // end of Lanczos()

//

// Assumption: same horizontal and vertical scaling factor

//

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 cente

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值