Reputation:
I have problem with the writing image again to the database ! This what I do ! The user choose an image from the hard disk through a Jframe, I read the content of the image and then I send it to the database , here I don't have any problem ! The problem when the user open a Jframe again to edit something else ! I know it maybe vague for you if you don't see the whole code ! I know how can I read a file and then encode it into hex code to send it to the database - blob data type in mysql - this my code about reading an image and convert it to hex code: this works perfectly and I have no problems with it
private String makeBlob(String paths) {
char[] hexArray = "0123456789abcdef".toCharArray();
Path path = Paths.get(paths);
byte[] bytes = null;
try {
bytes = Files.readAllBytes(path);
} catch (IOException ex) {
Logger.getLogger(LuggageAdd.class.getName()).log(Level.SEVERE, null, ex);
}
char[] hexChars = new char[bytes.length * 2];
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
String test = new String(hexChars);
return "0x" + test;
}
Note : the image is displayed using a jLabel on my jFrame ! so it's imageIcon
Is there is way to do the same thing with an existed image object ! I mean, here above I generate a hex code from a file (PATH TO AN IMAGE ), but I want to generate a hex code from an existing imageIcon object ?? If I can do that it will be easy to resend the image again to the database if it has not been edited !
Here is how I get a image from the data base :
public ImageIcon getImage(int id) {
BufferedImage bufferedImage = null;
try {
String ss = "SELECT image FROM luggage WHERE ID = " + id + " ";
ResultSet result = this.databaseConnection.doQuery(ss);
while (result.next()) {
Blob blob = result.getBlob("image");
int blobLength = (int) blob.length();
byte[] blobAsBytes = blob.getBytes(1, blobLength);
bufferedImage = ImageIO.read(new ByteArrayInputStream(blobAsBytes));
}
} catch (Exception ex) {
ex.printStackTrace(); }
return new ImageIcon(bufferedImage.getScaledInstance(128, 128, Image.SCALE_SMOOTH));
}
PS: I have tried an another solution, I read the image and the write it temporarily to a hard disk and then rewrite it again to the data base, but the problem that the size of the image increased when I saved it to the disk! if the original image size is 1.9MB then it will be 2.40MB . That's not efficient because, I want to re-save my image with same quality and size, beside that I have a limit of size of an image.
So the only way to solve it is know how I can get the content of an image icon and resend it again to the data base !
Thanks :)
Upvotes: 0
Views: 307
Reputation: 57381
You can either store the image blob (of the blobAsBytes) reference as source of the image.
Or
use ImageIcon's method getImage()
and convert the image to bytes.
E.g.
try {
// retrieve image
BufferedImage bi = getMyImage();
File outputfile = new File("saved.png");
ImageIO.write(bi, "png", outputfile);
} catch (IOException e) {
...
}
or save not to file but to ByteArrayOutputStream.
But for me the first way is better because the ImageIcon keeps scled instance instead of original image.
Upvotes: 1