4

我有一个图像数据库,我想将这些图像的 RGB 矩阵分别存储在 mysql db 中(例如:redMatrix_column、greenMatrix_column、blueMatrix_column)。在 matlab 中,我可以使用 imread() 函数分别获取 RBG 矩阵。如何在 java 中做到这一点?谢谢你的帮助。

4

1 回答 1

6

这是您获得颜色分量的方式:

public class GetImageColorComponents {
  public static void main(String... args) throws Exception {
    BufferedImage img = ImageIO.read(GetImageColorComponents.class
                                     .getResourceAsStream("/image.png"));
    int[] colors = new int[img.getWidth() * img.getHeight()];
    img.getRGB(0, 0, img.getWidth(), img.getHeight(), colors, 0, img.getWidth());

    int[] red = new int[colors.length];
    int[] green = new int[colors.length];
    int[] blue = new int[colors.length];

    for (int i = 0; i < colors.length; i++) {
      Color color = new Color(colors[i]);
      red[i] = color.getRed();
      green[i] = color.getGreen();
      blue[i] = color.getBlue();
    }
  }
}

有关在 MySQL 数据库中保存和检索字节的完整示例,请参阅此要点

于 2013-04-06T07:52:24.053 回答