java读取图片每个像素颜色值

直接赋代码
 

 public void getImagePixel(String image) throws Exception {

        System.out.println("---------start-----------");
        int[] rgb = new int[3];
        File file = new File(image);
        BufferedImage bi;
        //输出到指定文件
        String filename = "/Users/zou/Desktop/xxxx";
        try {
            bi = ImageIO.read(file);

            int width = bi.getWidth();
            int height = bi.getHeight();

            int minx = bi.getMinX();
            int miny = bi.getMinY();
            
            // +多少自己取值 值越小表示取的每一个像素越小
            for (int j = miny; j < height; j=j+4) {
                for (int i = minx; i < width; i=i+4) {
                    // 转16进制
                    int pixel = bi.getRGB(i, j);
                    rgb[0] = (pixel & 0xff0000) >> 16;
                    rgb[1] = (pixel & 0xff00) >> 8;
                    rgb[2] = (pixel & 0xff);
                    String str = "<font color='" + String.format("#%02X%02X%02X", rgb[0], rgb[1], rgb[2]) + "'>猪</font>";
                    Files.write(Paths.get(filename), str.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
                }
                Files.write(Paths.get(filename), "<br>".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
            }
            System.out.println("------------end----------");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws Exception {
        test rc = new test();
        rc.getImagePixel("/Users/zou/Desktop/WeChatdd730411a211ddae88a71623e38f7014.png");
    }

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值