zoukankan      html  css  js  c++  java
  • 【转】java缩放图片、java裁剪图片代码工具类

    • 一首先看下效果
    • 二工具类
    • 三测试类

    在系统的上传图片功能中,我们无法控制用户上传图片的大小,用户可能会上传大到几十M小到1k的的图片,一方面图片太大占据了太多的空间,另一方面,我们没办法在页面上显示统一大小的图片。所以我们需要对用户上传的图片进行缩放和裁剪,这里的缩放和平常的压缩不是一个意思,因为要实现小的图片会放大,大的图片会缩小,而且是等比例变的,图片不会显示挤压的效果。而这种操作Java完全可以实现。下面分享下java缩放、裁剪图片的工具类。

    一、首先看下效果:

    1.jpg为原图,yasuo.jpg是缩放后的,caijian.jpg是在yasuo.jpg基础上裁掉两边各10像素得到的 
    这里写图片描述 
    这里写图片描述 
    比较1.jpg,是不是既图片小了,又得到了想要的图片大小,而且图片没有被挤压? 下面来看代码:

    二、工具类:

    import java.awt.Color;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.geom.AffineTransform;
    import java.awt.image.AffineTransformOp;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    
    import javax.imageio.ImageIO;
    
    /**
     * 裁剪、缩放图片工具类
     * @author CSDN 没有梦想-何必远方 
     */
    public class ImgUtils {
        /**
         * 缩放图片方法
         * @param srcImageFile 要缩放的图片路径
         * @param result 缩放后的图片路径
         * @param height 目标高度像素
         * @param width  目标宽度像素  
         * @param bb     是否补白
         */
         public final static void scale(String srcImageFile, String result, int height, int width, boolean bb) {
                try {
                    double ratio = 0.0; // 缩放比例
                   File f = new File(srcImageFile);
                    BufferedImage bi = ImageIO.read(f);
                    Image itemp = bi.getScaledInstance(width, height, bi.SCALE_SMOOTH);//bi.SCALE_SMOOTH  选择图像平滑度比缩放速度具有更高优先级的图像缩放算法。
                    // 计算比例
                   if ((bi.getHeight() > height) || (bi.getWidth() > width)) {
                       double   ratioHeight = (new Integer(height)).doubleValue()/ bi.getHeight();
                       double   ratioWhidth = (new Integer(width)).doubleValue()/ bi.getWidth();
                       if(ratioHeight>ratioWhidth){
                           ratio= ratioHeight;
                       }else{
                           ratio= ratioWhidth;
                       }
                        AffineTransformOp op = new AffineTransformOp(AffineTransform//仿射转换
                                .getScaleInstance(ratio, ratio), null);//返回表示剪切变换的变换
                        itemp = op.filter(bi, null);//转换源 BufferedImage 并将结果存储在目标 BufferedImage 中。
                    }
                    if (bb) {//补白
                       BufferedImage image = new BufferedImage(width, height,
                                BufferedImage.TYPE_INT_RGB);//构造一个类型为预定义图像类型之一的 BufferedImage。
                        Graphics2D g = image.createGraphics();//创建一个 Graphics2D,可以将它绘制到此 BufferedImage 中。
                        g.setColor(Color.white);//控制颜色
                        g.fillRect(0, 0, width, height);// 使用 Graphics2D 上下文的设置,填充 Shape 的内部区域。
                        if (width == itemp.getWidth(null))
                            g.drawImage(itemp, 0, (height - itemp.getHeight(null)) / 2,
                                    itemp.getWidth(null), itemp.getHeight(null),
                                    Color.white, null);
                        else
                            g.drawImage(itemp, (width - itemp.getWidth(null)) / 2, 0,
                                    itemp.getWidth(null), itemp.getHeight(null),
                                    Color.white, null);
                        g.dispose();
                        itemp = image;
                    }
                    ImageIO.write((BufferedImage) itemp, "JPEG", new File(result));      //输出压缩图片
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
         /**
          * 裁剪图片方法
          * @param bufferedImage 图像源
          * @param startX 裁剪开始x坐标
          * @param startY 裁剪开始y坐标
          * @param endX 裁剪结束x坐标
          * @param endY 裁剪结束y坐标
          * @return
          */
         public static BufferedImage cropImage(BufferedImage bufferedImage, int startX, int startY, int endX, int endY) {
                int width = bufferedImage.getWidth();
                int height = bufferedImage.getHeight();
                if (startX == -1) {
                    startX = 0;
                }
                if (startY == -1) {
                    startY = 0;
                }
                if (endX == -1) {
                    endX = width - 1;
                }
                if (endY == -1) {
                    endY = height - 1;
                }
                BufferedImage result = new BufferedImage(endX - startX, endY - startY, 4);
                for (int x = startX; x < endX; ++x) {
                    for (int y = startY; y < endY; ++y) {
                        int rgb = bufferedImage.getRGB(x, y);
                        result.setRGB(x - startX, y - startY, rgb);
                    }
                }
                return result;
            }
    }

    三、测试类:

    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import com.etoak.util.ImgUtils;
    
    public class Test {
        public static void main(String[] args) throws IOException {
            String path="C:/1.jpg";    //输入图片  测试要在C盘放一张图片1.jpg
            ImgUtils.scale("C:/1.jpg","C:/yasuo.jpg", 180, 240, true);//等比例缩放  输出缩放图片
    
            File newfile=new File("C:/yasuo.jpg");   
            BufferedImage bufferedimage=ImageIO.read(newfile);
            int width = bufferedimage.getWidth();
            int height = bufferedimage.getHeight();
            //目标将图片裁剪成 宽240,高160
            if (width > 240) {
                                                                /*开始x坐标              开始y坐标             结束x坐标                     结束y坐标*/
                bufferedimage=ImgUtils.cropImage(bufferedimage,(int) ((width - 240) / 2),0,(int) (width - (width-240) / 2),(int) (height)
                        );
                if (height > 160) {
                    bufferedimage=ImgUtils.cropImage(bufferedimage,0,(int) ((height - 160) / 2),240,(int) (height - (height - 160) / 2)
                            );
                }
            }else{
                if (height > 160) {
                    bufferedimage=ImgUtils.cropImage(bufferedimage,0,(int) ((height - 160) / 2),(int) (width),(int) (height - (height - 160) / 2)
                            );
                }
            }
            ImageIO.write(bufferedimage, "jpg", new File("C:/caijian.jpg"));    //输出裁剪图片
        }
    }

    原文链接:http://blog.csdn.net/frightingforambition/article/details/49929201

  • 相关阅读:
    3. 尾缀
    Cocos工程命名规则整理(node部分)
    3.1-3.3 HBase Shell创建表
    2.11-2.12 HBase的数据迁移常见方式
    2.8-2.10 HBase集成MapReduce
    2.7 HBase架构深入剖析
    2.3-2.6 HBase java API
    2.1-2.2 HBase数据存储
    1.6-1.8 HBase表的物理模型
    1.4-1.5 HBase部署及基本使用
  • 原文地址:https://www.cnblogs.com/w821759016/p/6213933.html
Copyright © 2011-2022 走看看