java中BufferedImage实现RGB色彩识别及剪切代码示例
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">近期偶然<span style="color: black;">发掘</span>一个API,<span style="color: black;">能够</span>获取RGB图像三原色,实现响应的<span style="color: black;">有些</span>图像计算功能;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在代码示例<span style="color: black;">起始</span>前,先简单简述<span style="color: black;">有些</span>RGB三原色的概念吧;在光的折射下<span style="color: black;">能够</span><span style="color: black;">显现</span>彩虹样式的彩带,在计算机<span style="color: black;">行业</span>中,<span style="color: black;">一般</span><span style="color: black;">运用</span>红绿蓝来<span style="color: black;">做为</span>基色,<span style="color: black;">亦</span><span style="color: black;">便是</span>常说的RGB;自然界中<span style="color: black;">咱们</span>所有的可见颜色,均可<span style="color: black;">运用</span>红绿蓝三种颜色<span style="color: black;">区别</span>比例组合而得,三原色<span style="color: black;">亦</span><span style="color: black;">叫作</span>之为三基色;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span>红绿蓝三种颜色适当组合,从而获取想要的颜色,<span style="color: black;">每一个</span>像素在每种颜色上<span style="color: black;">能够</span>负载的亮度级别为256(0-255)阶,<span style="color: black;">因此</span><span style="color: black;">咱们</span><span style="color: black;">一般</span><span style="color: black;">能够</span>看到在<span style="color: black;">暗示</span>某种颜色时,时常会<span style="color: black;">运用</span>(255, 0, 0) <span style="color: black;">这般</span>来<span style="color: black;">暗示</span>,三个坐标的值,分别对应着颜色 红绿蓝 三种颜色的亮度;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在颜色模式中有一张图,比较形象的<span style="color: black;">能够</span>描述出几种颜色的对照关系;</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://pic2.zhimg.com/80/v2-231cee3aefed82cec3c300680d35a9c1_720w.webp" style="width: 50%; margin-bottom: 20px;"></div>摘自李涛<span style="color: black;">公众</span>摄影2015课程素材<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">上面这种图是取自李涛的<span style="color: black;">公众</span>摄影2015课程素材,从上面这张图<span style="color: black;">咱们</span><span style="color: black;">能够</span>比较直观的看到三原色的叠色效果;而三基色及叠色的RGB值如下所示:</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">/**
* 在三原色RGB中,红(Red)、绿(Green)、蓝(Blue)
* 红色<span style="color: black;">暗示</span>为(255, 0, 0)
* 绿色<span style="color: black;">暗示</span>为(0, 255, 0)
* 蓝色<span style="color: black;">暗示</span>为(0, 0, 255)
*
* 纯黑<span style="color: black;">暗示</span>为(0, 0, 0)
* 纯白<span style="color: black;">暗示</span>为(255, 255, 255) 红 + 绿 + 蓝 = 白
*
* 青色<span style="color: black;">暗示</span>为(0, 255, 255) 绿 + 蓝 = 青
* 品红色<span style="color: black;">暗示</span>(255, 0, 255) 红 + 蓝 = 品红
* 黄色<span style="color: black;">暗示</span>为(255, 255, 0) 红 + 绿 = 黄
*/</div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">基于<span style="color: black;">以上</span><span style="color: black;">基本</span>,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">运用</span>java代码,从而获取<span style="color: black;">照片</span>颜色信息,从而<span style="color: black;">能够</span>处理<span style="color: black;">有些</span>对应的业务场景或计算等;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在本地调试过程中,分别<span style="color: black;">运用</span>了openCV和JDK自带的BufferedImage两种API来计算RGB颜色,经测试,<span style="color: black;">发掘</span>BufferedImage貌似计算的结果更加准确且不<span style="color: black;">必须</span>引入第三方依赖;<span style="color: black;">这儿</span>将采用BufferedImage进行代码演示;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">首要</span><span style="color: black;">咱们</span><span style="color: black;">必须</span>将<span style="color: black;">照片</span>读取到内存中,已方便对<span style="color: black;">照片</span>进行计算,JDK的ImageIO类为<span style="color: black;">咱们</span><span style="color: black;">供给</span>了4种读取<span style="color: black;">照片</span>文件的方式;</p>
<div style="color: black; text-align: left; margin-bottom: 10px;">-- 方式一:基于磁盘文件
String filePath="D:\\下载\\颜色模式.jpg";
final BufferedImage read = ImageIO.read(new File(filePath));
-- 方式二:基于InputStream字节流(有多种实现,分场景<span style="color: black;">选取</span>)
String filePath="D:\\下载\\颜色模式.jpg";
InputStream stream = new FileInputStream(filePath);
final BufferedImage read = ImageIO.read(stream);
-- 方式三:基于URL网络文件
final URL url = new URL("https://test-url.com/test-img.jpge");
final BufferedImage read = ImageIO.read(url);
-- 方式四:基于ImageInputStream<span style="color: black;">照片</span>输入流
-- ImageInputStream 是 InputStre</div>
外贸B2B平台有哪些? “BS”(鄙视的缩写) 你的话语如春风拂面,让我感到无比温暖。
页:
[1]