栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 图片处理工具类(图片简单处理 java原生)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 图片处理工具类(图片简单处理 java原生)

java 图片处理工具类 方法说明
方法描述
cutCenterImage从中间裁剪图片不进行缩放
zoomImage缩放图片根据最短边
zoomCutCenterImage缩放裁剪图片,先进行缩放,然后进行裁剪
实现代码
public class ImgUtils {
    
    public static String format = "PNG";
    public static int unit = 1024;

    
    public static BufferedImage cutCenterImage(BufferedImage image, int width, int height) throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(image, format, os);
        InputStream input = new ByteArrayInputStream(os.toByteArray());
        Iterator iterator = ImageIO.getImageReadersByFormatName(format);
        ImageReader reader = (ImageReader) iterator.next();
        ImageInputStream iis = ImageIO.createImageInputStream(input);
        reader.setInput(iis, true);
        ImageReadParam param = reader.getDefaultReadParam();
        int imageIndex = 0;
        Rectangle rect = new Rectangle((reader.getWidth(imageIndex) - width) / 2, (reader.getHeight(imageIndex) - height) / 2, width, height);
        param.setSourceRegion(rect);
        return reader.read(0, param);


    }

    
    public static BufferedImage zoomImage(BufferedImage image, int width, int height) throws Exception {
        double wr = 0, hr = 0;
        //获取缩放比例
        double wRatio = width * 1.0 / image.getWidth();
        double hRatio = height * 1.0 / image.getHeight();
        AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(wRatio, hRatio), null);
        return ato.filter(image, null);
    }

    

    public static BufferedImage zoomImage(InputStream is, Integer size) throws Exception {
        int fileSize = is.available();
        //文件大于size时,才进行缩放。注意:size以K为单位
        if (fileSize < size * unit) {
            return ImageIO.read(is);
        }
        // 获取长*宽(面积)缩放比例
        double sizeRate = (size * 1024 * 0.5) / fileSize;
        // 获取长和宽分别的缩放比例,即面积缩放比例的2次方根
        double sideRate = Math.sqrt(sizeRate);
        BufferedImage bufImg = ImageIO.read(is);
        AffineTransformOp ato = new AffineTransformOp(AffineTransform.getScaleInstance(sideRate, sideRate), null);
        return ato.filter(bufImg, null);

    }

    
    public static BufferedImage zoomCutCenterImage(BufferedImage image, int width, int height) throws Exception {
        // 读文件获取
        int w = image.getWidth();
        int h = image.getHeight();
        double wRatio = 1.0 * width / w;
        double hRatio = 1.0 * height / h;
        double ratio = Math.max(wRatio, hRatio);
        BufferedImage zoomImage = zoomImage(image, (int) (w * ratio), (int) (h * ratio));
        return cutCenterImage(zoomImage, width, height);
    }

    
    public static byte[] getByte(File file) {
        byte[] bytes = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            bytes = new byte[fis.available()];
            fis.read(bytes);
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bytes;
    }

    
    public static void writeFile(BufferedImage image, File file) throws IOException {
        ImageIO.write(image, format, file);
    }

    public static void writeFile(BufferedImage image, String filePath) throws IOException {
        ImageIO.write(image, format, new File(filePath));
    }

    public static BufferedImage getBufferedImage(InputStream is) throws IOException {
        return ImageIO.read(is);
    }

    private static BufferedImage getBufferedImage(File file) throws IOException {
        return ImageIO.read(file);
    }

    
    private static String getImageType(File file) {
        if (file != null && file.exists() && file.isFile()) {
            String fileName = file.getName();
            int index = fileName.lastIndexOf(".");
            if (index != -1 && index < fileName.length() - 1) {
                return fileName.substring(index + 1);
            }
        }
        return null;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/877562.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号