java 实现BufferedImage和ImageReader两种方式获取图片宽高、判断图片类型、获取图片大小工具类代码以及测试响应结果

本文提供了一种使用Java的BufferedImage和ImageReader来获取图片宽度、高度、类型及大小的方法。通过示例代码详细解释了如何实现这些功能,并附有测试结果。

源码:

import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class ImageUtil {


    public static void checkImageIsNormal(final MultipartFile imageFile)
            throws Exception {
        if (imageFile == null) {
            throw new IllegalArgumentException("imageFile == null");
        }
        final long imageSize = imageFile.getSize();
        System.out.println("length:" + imageSize);
        if (imageSize > Config.IMG_MAX_SIZE) {
            throw new IllegalArgumentException("图片大小不能超过2M");
        }
        final String fileName = imageFile.getOriginalFilename();
        final String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        System.out.println("name:" + fileName);
        System.out.println("type:" + suffix);
        final String[] imageTypes = { "jpeg", "jpg", "png", "bmp" };
        final List<String> imageTypeList = Arrays.asList(imageTypes);
        if (!imageTypeList.contains(suffix.toLowerCase())) {
            throw new IllegalArgumentException("图片格式不支持");
        }
        final BufferedImage bufferedImage = ImageIO.read(imageFile
                .getInputStream());
        final int width = bufferedImage.getWidth();
        final int height = bufferedImage.getHeight();
        System.out.println("width:" + width + " height:" + height);
        if (width < Config.IMG_MIN_PIXEL || width > Config.IMG_MAX_PIXEL
                || height < Config.IMG_MIN_PIXEL
                || height > Config.IMG_MAX_PIXEL) {


            throw new IllegalArgumentException("图片尺寸不符合");
        }
    }


    public static void checkImageIsNormal(final String imageUrl)
            throws Exception {
        if (imageUrl == null) {
            throw new IllegalArgumentException("imageUrl == null");
        }
        final URL url = new URL(imageUrl);
        final URLConnection uc = url.openConnection();


        if (uc == null) {
            throw new NullPointerException("网络图片不存在");
        }
        final long imageSize = uc.getContentLength();
        System.out.println("netImage length:" + imageSize);
        if (imageSize > Config.IMG_MAX_SIZE) {
            throw new IllegalArgumentException("图片大小不能超过2M");
        }


        final ImageInputStream iis = ImageIO.createImageInputStream(uc
                .getInputStream());


        final Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
        if (!iter.hasNext()) {
            throw new RuntimeException("No readers found!");
        }
        final ImageReader reader = iter.next();
        System.out.println("Net imageType: " + reader.getFormatName());
        final String suffix = reader.getFormatName();
        final String[] imageTypes = { "jpeg", "jpg", "png", "bmp" };
        final List<String> imageTypeList = Arrays.asList(imageTypes);
        if (!imageTypeList.contains(suffix.toLowerCase())) {
            throw new IllegalArgumentException("图片格式不支持");
        }


        reader.setInput(iis, true);
        final int width = reader.getWidth(0);
        final int height = reader.getHeight(0);
        System.out.println("width:" + width + " height:" + height);
        if (width < Config.IMG_MIN_PIXEL || width > Config.IMG_MAX_PIXEL
                || height < Config.IMG_MIN_PIXEL
                || height > Config.IMG_MAX_PIXEL) {


            throw new IllegalArgumentException("图片尺寸不符合");
        }
    }


    public static void imageReader() throws Exception, IOException {
        final long start = System.currentTimeMillis();
        final File file = new File("wwj6.jpg");
        final ImageInputStream iis = ImageIO
                .createImageInputStream(new FileInputStream(file));


        final Iterator<ImageReader> iter = ImageIO.getImageReaders(iis);
        if (!iter.hasNext()) {
            throw new RuntimeException("No readers found!");
        }
        final ImageReader reader = iter.next();
        reader.setInput(iis, true);
        final int width = reader.getWidth(0);
        final int height = reader.getHeight(0);
        final long end = System.currentTimeMillis();
        System.out.println("userd:" + (end - start) + "ms");
        System.out.println("width:" + width + " height:" + height);
    }


    public static void bufferedImage() throws Exception, IOException {
        final long start = System.currentTimeMillis();
        final File file = new File("wwj6.jpg");
        final BufferedImage bufferedImage = ImageIO.read(new FileInputStream(
                file));
        final int width = bufferedImage.getWidth();
        final int height = bufferedImage.getHeight();
        final long end = System.currentTimeMillis();
        System.out.println("userd:" + (end - start) + "ms");
        System.out.println("width:" + width + " height:" + height);
    }


    public static void main(final String[] args)  {


        System.out.println("imageReader:");
        try {
            imageReader();
        } catch (final Exception e) {
            e.printStackTrace();
        }
        System.out.println("bufferedImage:");
        try {
            bufferedImage();
        } catch (final Exception e) {
            e.printStackTrace();
        }
    }

}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值