获取图片主要色素

获取图片主要色素

package com.bootdo.common.utils;
import net.coobird.thumbnailator.Thumbnails;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.*;
public class ClothingColorUtils {
    // 颜色标准数(固定差值)
    public static float colorNum = 51.0f;
    // 颜色标准(倍数关系)
    public static Map<Integer, String> colorStandard = new HashMap<Integer, String>();
    static {
        colorStandard.put(0, "00");
        colorStandard.put(1, "33");
        colorStandard.put(2, "66");
        colorStandard.put(3, "99");
        colorStandard.put(4, "cc");
        colorStandard.put(5, "ff");
    }

    // 十进制转十六进制
    @SuppressWarnings({ "static-access", "deprecation" })
    public static String tenToHex(int x) {
        Integer ten = new Integer(x);
        String hex = ten.toHexString(ten);
        return hex;
    }

    // 十六进制转十进制
    public static Integer hexToTen(String hex) {
        Integer x = Integer.parseInt(hex, 16);
        return x;
    }

    // 将十进制形式rgb转为符合标准颜色的十六进制字符串形式
    public static String changeRgb(int r, int g, int b) {
        // 通过颜色标准数计算颜色标准key
        int rk = Math.round(Float.parseFloat(r + "") / colorNum);
        int gk = Math.round(Float.parseFloat(g + "") / colorNum);
        int bk = Math.round(Float.parseFloat(b + "") / colorNum);
        // 通过颜色标准key取出标准颜色
        String rs = colorStandard.get(rk);
        String gs = colorStandard.get(gk);
        String bs = colorStandard.get(bk);
        return rs + gs + bs;
    }

    // 将BufferImage取出来的rgb int 值转换为三位数的rgb
    public static int[] changeToRgb(int pixel) {
        int[] rgb = new int[3];
        rgb[0] = (pixel & 0xff0000) >> 16;
        rgb[1] = (pixel & 0xff00) >> 8;
        rgb[2] = (pixel & 0xff);
        return rgb;
    }

    /**
     * 获取图片主要颜色
     * @param input
     */
    @SuppressWarnings("deprecation")
    public static List<String> recognition(String input) {
        String path = input.substring(input.indexOf("=") + 1, input.length());

        try {
            int[] rgb = new int[3];
            BufferedImage bi = Thumbnails.of(path).size(100, 100).asBufferedImage();
            Map<String, Integer> colorCount = new HashMap<String, Integer>();
            // 统计图中颜色标准的数量分布情况
            for (int w = 0; w < bi.getWidth(); w = w + 2) {
                for (int h = 0; h < bi.getHeight(); h = h + 2) {
                    // 取出每一个像素点的rgb值
                    rgb = changeToRgb(bi.getRGB(w, h));
                    // 将rbg转换为符合颜色标准的十六进制形式
                    String hexColor = changeRgb(rgb[0], rgb[1], rgb[2]);
                    // 统计下每一个颜色对应的像素数量
                    Integer count = colorCount.get(hexColor);
                    if (count == null) {
                        count = new Integer(0);
                    }
                    count++;
                    colorCount.put(hexColor, count);
                }
            }
            Map<Integer, String> colorCountRev = new HashMap<Integer, String>();
            List<Integer> countList = new ArrayList<Integer>();
            for (String key : colorCount.keySet()) {
                colorCountRev.put(colorCount.get(key), key);
                countList.add(colorCount.get(key));
            }
            // 排序
            Collections.sort(countList);
            int length = countList.size();
            if (length > 6) {
                length = 6;
            }

            // 取出数量最多的前五种颜色
            String colors = "";
            List<String> list = new ArrayList<>();
            for (int i = 1; i <= length; i++) {
                colors = String.valueOf(colorCountRev.get(countList.get(countList.size() - i)));
                //输出结果
//                System.out.println(colors);
                list.add(colors);
            }
            return list;
            
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


    public static void main(String[] args) {
       System.out.println(recognition("D:/logo/SPDB.png"));
    }
}


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值