缩小图片工具类

package com.juqi.group.common.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


public class ImageUtil {

/**
* 缩小图片
*
* @param srcFile
* 原图片
* @param destFile
* 目标图片
* @param multiple
* 缩略倍数
* @throws IOException
*/
public static void resizeFix(File srcFile, File destFile,float multiple) throws IOException {
BufferedImage srcImgBuff = ImageIO.read(srcFile);
int width = srcImgBuff.getWidth();
int height = srcImgBuff.getHeight();
int zoomWidth = (int)(width *multiple);
int zoomHeight = (int)(height *multiple);
BufferedImage imgBuff = scaleImage(srcImgBuff, width, height,
zoomWidth, zoomHeight);
writeFile(imgBuff, destFile);
}


public static void writeFile(BufferedImage imgBuf, File destFile)
throws IOException {
File parent = destFile.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}
ImageIO.write(imgBuf, "jpeg", destFile);
}

private static BufferedImage scaleImage(BufferedImage srcImgBuff,
int width, int height, int zoomWidth, int zoomHeight) {
int[] colorArray = srcImgBuff.getRGB(0, 0, width, height, null, 0,
width);
BufferedImage outBuff = new BufferedImage(zoomWidth, zoomHeight,
BufferedImage.TYPE_INT_RGB);
// 宽缩小的倍数
float wScale = (float) width / zoomWidth;
int wScaleInt = (int) (wScale + 0.5);
// 高缩小的倍数
float hScale = (float) height / zoomHeight;
int hScaleInt = (int) (hScale + 0.5);
int area = wScaleInt * hScaleInt;
int x0, x1, y0, y1;
int color;
long red, green, blue;
int x, y, i, j;
for (y = 0; y < zoomHeight; y++) {
// 得到原图高的Y坐标
y0 = (int) (y * hScale);
y1 = y0 + hScaleInt;
for (x = 0; x < zoomWidth; x++) {
x0 = (int) (x * wScale);
x1 = x0 + wScaleInt;
red = green = blue = 0;
for (i = x0; i < x1; i++) {
for (j = y0; j < y1; j++) {
color = colorArray[width * j + i];
red += getRedValue(color);
green += getGreenValue(color);
blue += getBlueValue(color);
}
}
outBuff.setRGB(x, y, comRGB((int) (red / area),
(int) (green / area), (int) (blue / area)));
}
}
return outBuff;
}

private static int getRedValue(int rgbValue) {
return (rgbValue & 0x00ff0000) >> 16;
}

private static int getGreenValue(int rgbValue) {
return (rgbValue & 0x0000ff00) >> 8;
}

private static int getBlueValue(int rgbValue) {
return rgbValue & 0x000000ff;
}

private static int comRGB(int redValue, int greenValue, int blueValue) {
return (redValue << 16) + (greenValue << 8) + blueValue;
}

public static void main(String[] args) throws Exception {
long time = System.currentTimeMillis();
ImageUtil.resizeFix(new File("E:\\pic\\mark.jpg"), new File("E:\\pic\\mark_2.jpg") , 1/2f);
ImageUtil.resizeFix(new File("E:\\pic\\mark.jpg"), new File("E:\\pic\\mark_4.jpg") , 1/4f);
time = System.currentTimeMillis() - time;
System.out.println("resize2 img in " + time + "ms");
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值