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

java 图片的处理

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

java 图片的处理

1.将透明图主体居中

    
    private void imageInit(File file2) {
        try {
            BufferedImage bufferedImage = ImageIO.read(file2);
            int xMin = bufferedImage.getWidth();
            int xMax = 0;
            int yMin = bufferedImage.getHeight();
            int yMax = 0;
            List coordList = new ArrayList<>();
            for (int i = 0; i < xMin; i++) {
                for (int j = 0; j < yMin; j++) {
                    //透明底判断
                    if (bufferedImage.getRGB(i, j) >> 24 != 0) {
                        Coord coord = new Coord(i, j);
                        coordList.add(coord);
                    }
                }
            }
            //取出最大和最小的xy(差值就是新图片的宽高)
            for (Coord coord : coordList) {
                int x = coord.x;
                int y = coord.y;
                if (x < xMin)
                    xMin = x;
                if (x > xMax)
                    xMax = x;
                if (y < yMin)
                    yMin = y;
                if (y > yMax)
                    yMax = y;
            }
            //设置透明底
            selectedImage = new BufferedImage(xMax - xMin, yMax - yMin, BufferedImage.TYPE_INT_RGB);
            Graphics2D graphics = selectedImage.createGraphics();
            selectedImage = graphics.getDeviceConfiguration().createCompatibleImage(xMax - xMin, yMax - yMin, Transparency.TRANSLUCENT);
            graphics.dispose();
            //从原图中将像素点拿出来
            for (int i = 0; i < xMax - xMin; i++) {
                for (int j = 0; j < yMax - yMin; j++) {
                    selectedImage.setRGB(i,j,bufferedImage.getRGB(i+xMin,j+yMin));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

处理前:

处理后:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/877683.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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