根据图片获取图片中最多的颜色

2023-05-16

根据网络图片获取背景色,用Palette 获取出来的颜色总是不对。

Palette p = Palette.from(resource).generate();
                            int defaultColor =
                                    ContextCompat.getColor(ClipActivity.this, android.R.color.holo_blue_bright);
                            int[] colors = new int[7];
                            colors[0] = p.getMutedColor(defaultColor);
                            colors[1] = p.getDarkMutedColor(defaultColor);
                            colors[2] = p.getLightMutedColor(defaultColor);
                            colors[3] = p.getVibrantColor(defaultColor);
                            colors[4] = p.getDarkVibrantColor(defaultColor);
                            colors[5] = p.getLightVibrantColor(defaultColor);
                            colors[6] = p.getDominantColor(defaultColor);
//

                            ll_data.removeAllViews();
//                            List<Palette.Swatch> swatches = p.getSwatches();
//                            for (Palette.Swatch swatch : swatches) {
//                                int rgb1 = swatch.getRgb();
//                                setTextBgColor(rgb1);
//                            }
//                            for (int color : colors) {
//                                setTextBgColor(color);
//                            }
                            int color4 = getBigColor(resource);

最后找了终于找到了代码(参考的代码地址忘记了)

1.缩小图片,2获取图片每个像素颜色最多的。

	  private static Bitmap small(Bitmap bitmap) {
			Matrix matrix = new Matrix();
			matrix.postScale(0.25f, 0.25f);
			Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
			return resizeBmp;
	}
	
	 public static ArrayList<Integer> getPicturePixel(Bitmap bitmap) {

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

        // 保存所有的像素的数组,图片宽×高
        int[] pixels = new int[width * height];

        bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

        ArrayList<Integer> rgb = new ArrayList<>();
        for (int i = 0; i < pixels.length; i++) {
            int clr = pixels[i];
            int red = (clr & 0x00ff0000) >> 16; // 取高两位
            int green = (clr & 0x0000ff00) >> 8; // 取中两位
            int blue = clr & 0x000000ff; // 取低两位
//            Log.d("tag", "r=" + red + ",g=" + green + ",b=" + blue);
            int color = Color.rgb(red, green, blue);
            //除去白色和黑色
            if (color != Color.WHITE && color != Color.BLACK) {
                rgb.add(color);
            }
        }

        return rgb;
    }

    public static int getBigColor(Bitmap bitmap) {
        ArrayList<Integer> picturePixel = getPicturePixel(small(bitmap));
        //计数相同颜色数量并保存
        HashMap<Integer, Integer> color2 = new HashMap<>();
        for (Integer color : picturePixel) {
            if (color2.containsKey(color)) {
                Integer integer = color2.get(color);
                integer++;
                color2.remove(color);
                color2.put(color, integer);

            } else {
                color2.put(color, 1);
            }
        }
        //挑选数量最多的颜色
        Iterator iter = color2.entrySet().iterator();
        int count = 0;
        int color = 0;
        while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            int value = (int) entry.getValue();
            if (count < value) {
                count = value;
                color = (int) entry.getKey();
            }

        }
        return color;
    }

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

根据图片获取图片中最多的颜色 的相关文章

  • Docker安装mysql 8 忽略表名大小写,通过命令修改my.cnf配置文件,无需进容器重新初始化数据库

    看了很多博客都是需要先启动容器再进容器内部修改my cnf 重新初始化数据库 xff0c 然而DockerHub直接就对容器启动时设置了my cnf的修改方式 xff0c 具体步骤简单如下 xff1a 官方参考链接 xff1a https
  • 解决 Windows 10 自带虚拟机运行 Ubuntu 18.04 卡顿的问题

    来源 xff1a A guide how to run Ubuntu 18 04 in Enhanced Mode in Hyper V 系统要求 控制端 xff1a Windows 10 xff0c 1803以及之后 受控端 xff1a
  • linux常用命令command not found的解决方案(自己整理)

    1 ifconfig command not found 是因为没有安装此命令包 xff0c 安装方法 xff1a yum install net tools2 sz和rz文件上传命令command not found 执行 xff1a w
  • Window7升级 PowerShell

    一 查看当前PowerShell版本 1 命令行输入powershell 2 命令行输入get host 二 下载新版PowerShell xff08 下载说明 xff1a https docs microsoft com zh cn po
  • MSE(均方误差)函数和RMSE函数

    本文链接 xff1a https blog csdn net qq 36512295 article details 86526799 MSE xff08 均方误差 xff09 函数一般用来检测模型的预测值和真实值之间的偏差 训练集 xff
  • PSNR-峰值信噪比(原理及Python代码实现)

    本文链接 xff1a https blog csdn net leviopku article details 84586446 PSNR的全称为 Peak Signal to Noise Ratio xff0c 直译为中文就是峰值信噪比
  • matlab向量的模

    向量 v 中的元素 v1 v2 v3 vn xff0c 下式给出其幅度 xff1a v 61 v12 43 v22 43 v32 43 43 vn2 MATLAB中需要采按照下述步骤进行向量的模的计算 xff1a 采取的矢量及自身的积 xf
  • 图像阶梯效应

    图像阶梯效应现象产生原因 在利用二阶偏微分方程进行平滑图像过程中 xff0c 有时会出现 阶梯效应 或者是 块效应 即图像处理后某些区域内灰度相同 区域内灰度相同 xff0c 表示该区域任意一点其灰度值的一阶导数为0 这说明随着迭代次数增加
  • 邻接矩阵

    逻辑结构分为两部分 xff1a V和E集合 xff0c 其中 xff0c V是顶点 xff0c E是边 因此 xff0c 用一个一维数组存放图中所有顶点数据 xff1b 用一个二维数组存放顶点间关系 xff08 边或弧 xff09 的数据
  • JavaScript

    1 a 任何数值除以0都会导致错误而终止程序执行 但是在JavaScript中 xff0c 会返回特殊的值 xff0c 因此不会影响程序的执行 比0大的数除以0 xff0c 会得到无穷大 xff0c 所以js用infinity来x显示出来
  • -操作系统

    1进程同步的引入背景 xff1a 在多道程序环境下 xff0c 进程是并发执行的 xff0c 不同进程之间存在这不同的相互制约关系 为了协调进程之间的相互制约关系 xff0c 引入了进程同步的概念 2在有n个进程共享一个互斥段 xff0c
  • -网络基础

    1 物理层 xff1a RJ45 CLOCK IEEE802 3 xff08 中继器 集线器 网关 xff09 数据链路 xff1a PPP FR HDLC VLAN MAC xff08 网桥 xff0c 交换机 xff09 网络层 xff
  • 恒生面试准备

    目录 1 flex布局 2 vue的特性有哪些 xff1f 3 Vue中父子组件的传值的方式有几种 xff1f 它们有什么数据上的限制 4 项目中用了vue组件传值的方式有哪些 5 项目中用到的组件 6 浏览器输入url到渲染网页的过程 7
  • 同程艺龙一面

    目录 1 说说HTML5 2 link标签要放到head之后 xff0c script标签要放到body标签结束之前 xff0c 为什么 3 HTML语义化重要性 4 meta中的viewport干嘛的 5 P标签中的文字如何水平垂直居中
  • 杭州端点一面

    目录 1 vue双向绑定 2 Promise了解过吗 xff1f promise all解释一些 xff0c 为什么可以链式回调 3 let和var的区别 4 深拷贝和浅拷贝区别和实现方式 5 父子组件传值 6 了解webpack吗 xff
  • iOS 中 cell和 label 的自适应高度

    之前我们使 的 cell 一直都是同样的高度 或者某行固定 度 实际开发中经常要让 cell 根据 Model 中文本的长短动态的更改高度 1 获取文本高度 计算一段文本在限定宽高内所占矩形大 iOS7计算文本 度 法 CGRect bou
  • C++获取含有中文的字符串长度

    在Windows下 xff0c 中文字符在C 43 43 中的内存占用为2字节 xff0c 此时采用字符串长度获取函数得到的结果会将一个中文字符识别为两个长度 xff1a include lt stdio h gt include lt s
  • IOS中自定义cell大小的两种方法

    第一种 xff1a 自定义cell xff0c 不用xib的 xff0c 用已经封装好的类Category m类 第一步 xff1a label 的自定义高度 xff0c 用的是 xff08 封装好的方法 xff09 根据 label 中的
  • mysql的2003-Can‘t connect to MySQL server on ‘localhost‘(10061“Unknown error“)错误解决方法

    在我们对mysql的数据库进行连接时出现2003 Can 39 t connect to MySQL server on 39 localhost 39 10061 34 Unknown error 34 的错误 xff0c 截图如下 xf

随机推荐

  • python实现朗读内容

    项目描述 pyttsx3 是 Python 中的文本到语音转换库 与替代库不同 xff0c 它可以脱机工作 xff0c 并且与Python 2和3兼容 pyttsx3 的官网pyttsx3 皮皮 pypi org https pypi or
  • python计算圆的面积

    提示 xff1a 笔记希望对你有帮助 文章目录 前言1 事先准备2 代码书写3 代码运行总结 前言 提示 xff1a 本次我们的代码的是通过pycharm实现的 下面让我们使用python对我们的圆的面积以及周长进行一个简单的计算 1 事先
  • Windows环境下edge浏览器点击下载没有反应

    项目场景 xff1a 系统 xff1a windows 11 家庭中文版 浏览器 xff1a Microsoft Edge版本 104 0 1293 54 正式版本 64 位 问题描述 edge浏览器点击下载没有反应 原因分析 xff1a
  • python怎么查看安装了哪些库

    这里写目录标题 前言方法1方法2 前言 有时候我们在使用python的时候 xff0c 想知道自己安装哪些了第三方库以及第三方库的版本 xff0c 下面来介绍一下方法 方法1 我们可以通过命令提示符输入相关的命令进行查看 xff0c 命令如
  • python 安装dmPython

    文章目录 前言下载达梦数据库安装dmPython模块引用模块解决方法 代码演示 前言 使用python连接我们的连接达梦数据库 下载达梦数据库 达梦数据库的官网链接 达梦数据库的下载链接 安装dmPython模块 找到达梦数据库的安装目录
  • python requests cookie的获取和使用

    文章目录 前言一 cookie是什么 xff1f 二 使用步骤开始代码实现会话是什么然后写入我们的账号信息 使用session访问登陆账号的url获取账号的书架上的数据 完整的代码补充比较暴力的获取方式 前言 我们在使用爬虫中会用到很多账号
  • pyspark报错 org.apache.spark.SparkException: Python worker failed to connect back.

    项目场景 xff1a 使用pycharm工具将spark分析完的数据存储到MySQL数据库中 问题描述 在程序执行过程中发生以下报错 xff1a org span class token punctuation span apache sp
  • python的tkinter(图形用户界面)

    目录标题 什么是图形用户界面 xff08 GUI xff09 Tinter函数和参数说明 xff08 常用 xff09 Lable 标签 xff1a 效果Button 按钮 效果 Entry 文本框 效果 Text xff08 多行文本框
  • K8s配置文档

    xff01 xff01 xff01 xff01 xff01 xff01 如果看不懂文档点击进入视频k8s配置视频 xff01 xff01 xff01 xff01 xff01 xff01 xff01 xff01 xff01 xff01 xff
  • 安装spark

    安装spark 上传安装包文件 spark 2 1 1 bin hadoop2 7 解压安装包 span class token function tar span xvf spark 2 1 1 bin hadoop2 7 C opt m
  • 人大金仓数据库的简单巡查

    人大金仓的简单巡查 查询人大金仓的版本 sys ctl V sys ctl version span class token comment 在ksql命令行工具中查询数据库版本 span span class token keyword
  • 人大金仓数据库的单表查询

    人大金仓数据库的单表查询 查看表的内容 span class token comment 查询数据库的创建语句 span d exam span class token punctuation span course span class
  • JWT和Security 登录权限判断和token访问和让token失效

    文章目录 Spring SecurityJWT无状态的单点登录流程用到的方法configure HttpSecurity http 登录 authenticationSuccessHandler loadUserByUsername 通过t
  • Flutter Container设置 width 无效

    文章目录 布局组件和容器组件BoxContraints机制布局组件 Row Column解决办法参考 布局组件和容器组件 容器类Widget和布局类Widget都作用于其子Widget xff0c 不同的是 xff1a 布局类Widget一
  • Flutter 滚动距离来设置TabBar的位置,点击TabBar滚动的到指定的位置

    文章目录 效果gif定义属性每个组件设置Key获取控件的距离顶部的位置设置监听设置TabBar 点击getY源码 感觉有用的话 xff0c star一下如果想一起学习进步 QQ群766903559 效果gif 定义属性 ScrollCont
  • solidity create2 学习

    文章目录 saltcreatecreate2address this clone factoryshr shl使用 两个值生成唯一的值 xff0c 通过唯一值获取对应的两个值 salt nonce 也可以自己生成 create addres
  • Flutter和Native 通信 pigeon

    文章目录 1 pigeon2 定义接口3 定义sh文件 pigeon sh xff08 lib同级目录创建 xff09 4 运行sh文件 pigeon sh 会生成一下文件5 配置6 使用7 IOS xxx plugin h8 XxxPlu
  • flutter调用go

    文章目录 命令引入greeting aar和使用android中使用Flutter2gopluginPlugin kt参考文档 命令 mkdir demo cd demo go mod init demo 编写greeting go go
  • solidity 学习2.批量转账,存入eth。读取数据。

    pragma solidity 0 4 17 import 39 zeppelin solidity contracts token ERC20 StandardToken sol 39 contract BLEOS is Standard
  • 根据图片获取图片中最多的颜色

    根据网络图片获取背景色 xff0c 用Palette 获取出来的颜色总是不对 Palette p 61 Palette from resource generate int defaultColor 61 ContextCompat get