Last active
December 15, 2015 21:28
-
-
Save RaffaeleSgarro/5325399 to your computer and use it in GitHub Desktop.
Save color components of an image separately in a MySQL database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package stackoverflow; | |
import java.awt.Color; | |
import java.awt.image.BufferedImage; | |
import java.sql.Blob; | |
import java.sql.Connection; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.util.Arrays; | |
import javax.imageio.ImageIO; | |
import javax.sql.rowset.serial.SerialBlob; | |
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; | |
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()); | |
byte[] red = new byte[colors.length]; | |
byte[] green = new byte[colors.length]; | |
byte[] blue = new byte[colors.length]; | |
for (int i = 0; i < colors.length; i++) { | |
Color color = new Color(colors[i]); | |
red[i] = (byte) color.getRed(); | |
green[i] = (byte) color.getGreen(); | |
blue[i] = (byte) color.getBlue(); | |
} | |
MysqlDataSource db = new MysqlDataSource(); | |
db.setDatabaseName("test"); | |
Connection conn = db.getConnection("test", "test"); | |
setupDb(conn); | |
PreparedStatement stmt = conn.prepareStatement("INSERT INTO image " + | |
"(name, red, green, blue) VALUES (?, ?, ?, ?)"); | |
stmt.setString(1, "foo"); | |
stmt.setBlob(2, new SerialBlob(red)); | |
stmt.setBlob(3, new SerialBlob(green)); | |
stmt.setBlob(4, new SerialBlob(blue)); | |
stmt.execute(); | |
stmt = conn.prepareStatement("SELECT * FROM image WHERE name = ? LIMIT 1"); | |
stmt.setString(1, "foo"); | |
ResultSet res = stmt.executeQuery(); | |
res.next(); | |
byte[] storedRed = getBytes(res.getBlob("red")); | |
byte[] storedGreen = getBytes(res.getBlob("green")); | |
byte[] storedBlue = getBytes(res.getBlob("blue")); | |
assert Arrays.equals(red, storedRed); | |
assert Arrays.equals(green, storedGreen); | |
assert Arrays.equals(blue, storedBlue); | |
for (int i = 0; i < colors.length; i++) { | |
Color color = new Color(storedRed[i] & 0xFF, storedGreen[i] & 0xFF, storedBlue[i] & 0xFF, 0xFF); | |
assert colors[i] == color.getRGB(); | |
} | |
conn.close(); | |
} | |
private static byte[] getBytes(Blob blob) throws Exception { | |
return blob.getBytes(1, (int) blob.length()); | |
} | |
public static void setupDb(Connection conn) throws Exception { | |
conn.prepareStatement("DROP TABLE IF EXISTS image").execute(); | |
conn.prepareStatement("CREATE TABLE image (" + | |
"name text," + | |
"red blob," + | |
"green blob," + | |
"blue blob)").execute(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Enable assertions when runnnig this program by passing
-ea
to the JVM