读取色带图片里的rgb值
色带图片

读取色带图片的代码
1 /** 2 * 读取一张图片的RGB值 3 * 4 * @throws Exception 5 * @params image: 色带图片的路径 6 */ 7 public static int[][] getImagePixel(String image) throws Exception { 8 int[][] rgb = new int[256][3];//这里定义一个二维数组存放RGB的值,256是图片的高,有256种颜色 9 File file = new File(image); 10 BufferedImage bi = null; 11 try { 12 bi = ImageIO.read(file); 13 } catch (Exception e) { 14 e.printStackTrace(); 15 } 16 int width = bi.getWidth(); 17 int height = bi.getHeight(); 18 int minx = bi.getMinX(); 19 int miny = bi.getMinY(); 20 for (int i = minx; i < width; i++) { 21 for (int j = miny; j < height; j++) { 22 int pixel = bi.getRGB(i, j); // 下面三行代码将一个数字转换为RGB数字 23 rgb[j][0] = (pixel & 0xff0000) >> 16; 24 rgb[j][1] = (pixel & 0xff00) >> 8; 25 rgb[j][2] = (pixel & 0xff); 26 } 27 } 28 return rgb; 29 }
如果需要将RGB的值转为16进制颜色
String hex = String.format("#%02x%02x%02x", rgb[i][0], rgb[i][1], rgb[i][2]);
posted on 2020-08-11 12:44 jianglusheng 阅读(548) 评论(0) 收藏 举报
浙公网安备 33010602011771号