Reputation: 512
I have an image database and I want to store these image's RGB matrix in mysql db separately (Forexample : redMatrix_column, greenMatrix_column, blueMatrix_column ). In matlab I can get RBG matrix separately using imread() function. How to do this in java ? thanks for your help.
Upvotes: 4
Views: 2115
Reputation: 20875
This is how you get the color components:
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();
}
}
}
See this gist for a complete example involving saving and retrieving the bytes in the MySQL database.
Upvotes: 6