zoukankan      html  css  js  c++  java
  • 图片缩放 剪切

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

  • 相关阅读:
    HO引擎近况2020712
    大地形初探小结一
    大地形初探之信息收集
    真实大地形初探
    翻译22 Unity中的曲面
    翻译21 平面和线框着色
    翻译20 视差和法线、高度图回顾
    翻译19 GPU Instance
    翻译18 Realtime GI & LPPV & LOD
    翻译17 Mixed Lighting混合光照
  • 原文地址:https://www.cnblogs.com/justinsun/p/2151411.html
Copyright © 2011-2022 走看看