以目标图片尺寸按原比例缩放切割图片

以目标图片尺寸按原比例缩放切割图片

来源:http://my.oschina.net/u/873047/blog/106551

 

需求:原始图片缩放为目标尺寸后,要求缩放后的图片保持原图片的样式不改变,但是缩放后图片的四边可以裁减。
原理:将待裁剪图片宽高相除值和目标尺寸宽高相除值进行比较,如果前者较大,说明待裁剪图片相对于目标尺寸来说要宽出一块,现在已高为基准进行裁剪,即将原始图片的高度缩放到目标尺寸的高度。计算出原始图片高度和目标尺寸高度的比例,和原始图片宽度相乘,得到缩放后的宽度。此时的缩缩放后的高度就是目标尺寸的高度,但是宽度*肯定*会比目标尺寸宽度宽。将缩放后的多余的宽度分为两份右边和左边各裁去一份。
以宽为基准原理类似。

代码:

 

public class ImgUtil {
/**
 *
 * @param srcImgPath 待切割图片路径
 * @param destImgPath 切割后图片路径
 * @param destImgW 所需宽度
 * @param destImgH 所需高度
 */
public static void createThumb (String srcImgPath , String destImgPath, int destImgW , int destImgH){
    //原图片等比例缩小或放大之后的图片
    int narrowImgW ;
    int narrowImgH ;
    //原图片大小
    int srcImgW ;
    int srcImgH ;
    try {
        BufferedImage bi = ImageIO. read(new File(srcImgPath));
        srcImgW = bi.getWidth();
        srcImgH = bi.getHeight();
        // 转换图片尺寸与目标尺寸比较 , 如果转换图片较小,说明转换图片相对于目标图片来说高较小,需要以高为基准进行缩放。
        if((float )srcImgW /srcImgH > (float)destImgW / destImgH){
            narrowImgW = ( int)(((float )destImgH / (float)srcImgH)*srcImgW);
            narrowImgH = destImgH;
            //按照原图以高为基准等比例缩放、或放大。这一步高为所需图片的高度,宽度肯定会比目标宽度宽。
            int cutNarrowImgSize = (narrowImgW - destImgW)/2;
            BufferedImage narrowImg = new BufferedImage(narrowImgW, narrowImgH,BufferedImage.TYPE_INT_RGB);
            narrowImg.getGraphics().drawImage(bi.getScaledInstance(narrowImgW, narrowImgH, Image.SCALE_SMOOTH ), 0, 0, null);
            //等比例缩放完成后宽度与目标尺寸宽度相比较 , 将多余宽的部分分为两份 ,左边删除一部分
            Image image = narrowImg.getScaledInstance(narrowImgW, narrowImgH, Image.SCALE_DEFAULT );
            CropImageFilter cropFilter = new CropImageFilter(cutNarrowImgSize, 0, narrowImgW-cutNarrowImgSize, narrowImgH);
            Image img = Toolkit.getDefaultToolkit().createImage( new FilteredImageSource(image.getSource(), cropFilter));
            BufferedImage cutLiftNarrowImg = new BufferedImage( narrowImgW-cutNarrowImgSize, narrowImgH,BufferedImage.TYPE_INT_RGB );
            cutLiftNarrowImg.getGraphics().drawImage(img, 0, 0, null);
            //右边删除一部分
            image = cutLiftNarrowImg.getScaledInstance(narrowImgW-cutNarrowImgSize, narrowImgH, Image.SCALE_DEFAULT );
            cropFilter = new CropImageFilter(0, 0, narrowImgW-cutNarrowImgSize*2, narrowImgH);
            img = Toolkit.getDefaultToolkit().createImage( new FilteredImageSource(image.getSource(), cropFilter));
            BufferedImage cutRightNarrowImg = new BufferedImage( narrowImgW-cutNarrowImgSize*2, narrowImgH,BufferedImage.TYPE_INT_RGB );
            Graphics g = cutRightNarrowImg.getGraphics();
            g.drawImage(img, 0, 0, null); // 绘制截取后的图
            g.dispose();
            //输出为文件 最终为所需要的格式
            ImageIO. write(cutRightNarrowImg, "JPEG", new File(destImgPath));
        }
        else{ //以宽度为基准
            narrowImgW = destImgW;
            narrowImgH = ( int) (((float )destImgW / (float)srcImgW)*srcImgH);
            int cutNarrowImgSize = (narrowImgH - destImgH)/2;

            BufferedImage narrowImg = new BufferedImage(narrowImgW, narrowImgH,BufferedImage.TYPE_INT_RGB);
            narrowImg.getGraphics().drawImage(bi.getScaledInstance(narrowImgW, narrowImgH, Image.SCALE_SMOOTH ), 0, 0, null);

            Image image = narrowImg.getScaledInstance(narrowImgW, narrowImgH, Image.SCALE_DEFAULT );
            CropImageFilter cropFilter = new CropImageFilter(0, cutNarrowImgSize, narrowImgW, narrowImgH-cutNarrowImgSize);
            Image img = Toolkit.getDefaultToolkit().createImage( new FilteredImageSource(image.getSource(), cropFilter));
            BufferedImage cutTopNarrowImg = new BufferedImage( narrowImgW, narrowImgH-cutNarrowImgSize,BufferedImage. TYPE_INT_RGB);
            cutTopNarrowImg.getGraphics().drawImage(img, 0, 0, null);

            image = cutTopNarrowImg.getScaledInstance(narrowImgW, narrowImgH-cutNarrowImgSize, Image. SCALE_DEFAULT);
            cropFilter = new CropImageFilter(0, 0, narrowImgW, narrowImgH-cutNarrowImgSize*2);
            img = Toolkit.getDefaultToolkit().createImage( new FilteredImageSource(image.getSource(), cropFilter));
            BufferedImage cutBottomNarrowImg = new BufferedImage( narrowImgW, narrowImgH-cutNarrowImgSize*2,BufferedImage. TYPE_INT_RGB);
            Graphics g = cutBottomNarrowImg.getGraphics();
            g.drawImage(img, 0, 0, null);
            g.dispose();
            ImageIO. write(cutBottomNarrowImg, "JPEG", new File(destImgPath));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}
public static void main(String []args){
    createThumb("G://3.png", "G://223.jpg", 320, 212);
}

 

你可能感兴趣的:(图片)