图片缩放 剪切

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

import javax.imageio.ImageIO;

public class ImageUtils {
/**
* 根据指定宽高缩小图片,多余部分切掉
*
* @param src
* 原图片
* @param destWidth
* 目标宽
* @param destHeight
* 目标高
* @return 缩小后图片
*/
public static BufferedImage scaleImg(BufferedImage src, int destWidth,
int destHeight) {
if (destWidth <= 0 || destHeight <= 0 || src == null)
return null;

// 原图的哪个范围被画入新图
int cropWidth = 0;
int cropHeight = 0;

// 原图的大小
int sw = src.getWidth();
int sh = src.getHeight();
// 原图比目标图小,直接返回
if (sw < destWidth && sh < destHeight)
return src;

if (sw < destWidth)
destWidth = sw;
if (sh < destHeight)
destHeight = sh;

double widthScale = (double) destWidth / sw;
double heightScale = (double) destHeight / sh;
double scale = Math.max(widthScale, heightScale);

cropWidth = (int) (destWidth / scale);
cropHeight = (int) (destHeight / scale);

// System.out.println("sw = " + sw +" sh = " + sh +" dest = " +
// destWidth+" destHeight = " + destHeight +" cropW = " + cropWidth
// +" cropHeight = " + cropHeight);

int type = src.getType();
BufferedImage res = new BufferedImage(destWidth, destHeight, type);
res.getGraphics().drawImage(src, 0, 0, destWidth, destHeight,
(sw - cropWidth) / 2, (sh - cropHeight) / 2,
(sw - cropWidth) / 2 + cropWidth,
(sh - cropHeight) / 2 + cropHeight, null);

return res;
}

/**
* 按照指定宽缩放图片
*
* @param src 原图片
* @param destWidth 目标宽
* @return
*/
public static BufferedImage resizeImgByWidth(BufferedImage src, int destWidth) {
if (destWidth <= 0
|| src == null)
return null;
int srcHeight = src.getHeight();
int srcWidth = src.getWidth();
int destHeight = 0;// 缩略图高

double scale = (double)destWidth / srcWidth;
destHeight = (int) (srcHeight * scale);

BufferedImage res = new BufferedImage(destWidth, destHeight,
src.getType());
res.getGraphics().drawImage(src, 0, 0, destWidth, destHeight, null);
return res;
}


/**
* 按照指定高缩放图片
*
* @param src 原图片
* @param destHeight 目标高
* @return
*/
public static BufferedImage resizeImgByHeight(BufferedImage src, int destHeight) {
if (destHeight <= 0
|| src == null)
return null;
int srcHeight = src.getHeight();
int srcWidth = src.getWidth();
int destWidth = 0;

double scale = (double)destHeight / srcHeight;
destWidth = (int) (srcWidth * scale);

BufferedImage res = new BufferedImage(destWidth, destHeight,
src.getType());
res.getGraphics().drawImage(src, 0, 0, destWidth, destHeight, null);
return res;
}

public static void main(String[] args) {
try {
BufferedImage src = ImageIO.read(new File("c:/1.jpg"));
BufferedImage res = ImageUtils.scaleImg(src, 300, 192);
ImageIO.write(res, "JPG", new File("c:/rest1.jpg"));
res = ImageUtils.scaleImg(src, 344, 192);
ImageIO.write(res, "JPG", new File("c:/rest2.jpg"));
res = ImageUtils.scaleImg(src, 344, 160);
ImageIO.write(res, "JPG", new File("c:/rest3.jpg"));
res = ImageUtils.scaleImg(src, 344, 500);
ImageIO.write(res, "JPG", new File("c:/rest4.jpg"));
res = ImageUtils.scaleImg(src, 800, 500);
ImageIO.write(res, "JPG", new File("c:/rest5.jpg"));

res = ImageUtils.resizeImgByWidth(src, 1200);
ImageIO.write(res, "JPG", new File("c:/rest6.jpg"));

res = ImageUtils.resizeImgByWidth(src, 200);
ImageIO.write(res, "JPG", new File("c:/rest7.jpg"));
} catch (Exception e) {
e.printStackTrace();
}
}
}

原文地址:https://www.cnblogs.com/justinsun/p/2151411.html