按照指定宽度等比缩放图片然后截取指定图片宽高

项目场景:

项目中有一个商品详情展示模块,图片比较大,每次进入页面时,页面渲染慢,宽带占比比较高,为了优化提出图片等比缩放到前端展示的宽度,然后按照指定的宽高进行截取图片进行展示

方法

 /**
 * 根据指定大小和指定精度压缩图片 (前端要求的指定宽高是240 *400)
 *
 * @param file      file 文件
 * @param desFileSize  指定图片大小,单位kb
 * @param accuracy     精度,递归压缩的比率,建议小于0.9
 * @param desMaxWidth  目标最大宽度
 * @param desMaxHeight 目标最大高度
 * @return 目标文件路径
 */
public static byte[] commpressPicForScale(String url, long desFileSize, double accuracy, int desMaxWidth, int desMaxHeight) throws Exception {
    int[] ints = returnImgWH(url);
    if(ints == null){
       return null;
    }
    BufferedImage img = ImageIO.read(new File("C:\\thetempimg.jpg"));
    String minFile = null;
    String maxFile;
   int width;
    int height;

// BufferedImage imgS = null;
String ext = null;
//获取图片的长和宽
width = img.getWidth();
height = img.getHeight();
int w = 240;
int h = 400;
if(!new File(“C:\thetempimg1.jpg”).exists()) {
int tempw = 0;
int temph = 0;
if(width>height){
tempw = w;
temph =height* w/width;
}else{
tempw = wwidth/height;
temph =height
w/width;;
}
//为了防止图片截取报错
if(width < w){
w = width;
}
if(temph < h){
h = temph;
}
//与按比例缩放的不同只在于,不需要获取新的长和宽,其余相同.
Image _img = img.getScaledInstance(w, temph, Image.SCALE_DEFAULT);
BufferedImage image = new BufferedImage(w, temph, BufferedImage.TYPE_INT_RGB);
//图片截取
BufferedImage pic2 = image.getSubimage(0, 0, w, h);
Graphics2D graphics = pic2.createGraphics();
graphics.drawImage(_img, 0, 0, null);
graphics.dispose();
OutputStream out = new FileOutputStream(new File(“C:\thetempim1.jpg”));
ImageIO.write(pic2, “jpg”, out);
ByteArrayOutputStream baos = new ByteArrayOutputStream(); //字节输出流(写入到内存)
Thumbnails.of(“C:\thetempim1.jpg”).scale(1).outputQuality(1).toOutputStream(baos);
return baos.toByteArray();
}

    //读取远程url图片,得到宽高
    public static int[] returnImgWH(String imgurl) {
        boolean b = false;
        try {
            //实例化url
            URL url = new URL(imgurl);
            //载入图片到输入流
            java.io.BufferedInputStream bis = new BufferedInputStream(url.openStream());
            //实例化存储字节数组
            byte[] bytes = new byte[100];
            //设置写入路径以及图片名称
            OutputStream bos = new FileOutputStream("C:\\thetempimg.jpg");
            int len;
            while ((len = bis.read(bytes)) > 0) {
                bos.write(bytes, 0, len);
            }
            bis.close();
            bos.flush();
            bos.close();
            //关闭输出流
            b = true;
        } catch (Exception e) {
            //如果图片未找到
            b = false;
        }
        int[] a = new int[2];
        if (b) {//图片存在
            //得到文件
            java.io.File file = new java.io.File("C:\\thetempimg.jpg");
            BufferedImage bi = null;
            boolean imgwrong = false;
            try {
                //读取图片
                bi = javax.imageio.ImageIO.read(file);
                try {
                    //判断文件图片是否能正常显示,有些图片编码不正确
                    int i = bi.getType();
                    imgwrong = true;
                } catch (Exception e) {
                    imgwrong = false;
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            if (imgwrong) {
                a[0] = bi.getWidth(); //获得 宽度
                a[1] = bi.getHeight(); //获得 高度
            } else {
                a = null;
            }
            //删除文件, 不能删, 因为上面还要处理图片
           // file.delete();
        } else {//图片不存在
            a = null;
        }
        return a;
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值