Java BLOB to image file - java

I have a BLOB that is retrieved from a MySQL database as follows:
Blob imageBlob;
while (rs.next()) {
imageBlob= rs.getBlob("face");
}
after that my imageBlob is something like this: data:image/png;base64,iVBORw0KGgoAAAANSUhE................
I've been googling around but I haven't found any solution to my problem: how do I create an image file and save it on the disk from this BLOB?

imageBlob is storing the base64 representation of your image data. For storing that onto your disk you need to decode that base64 representation into the original binary format representation.
// Imports required
import java.util.Base64
import java.io.ByteArrayInputStream;
import java.io.File;
import javax.imageio.ImageIO;
String imageData = "data:image/png;base64,iVBORw0KGgoAAAANSUhE....";
String base64Data = imageData.split(",")[1]
byte[] decodedBytes = Base64.getDecoder().decode(base64Data);
ByteArrayInputStream bis = new ByteArrayInputStream(decodedBytes);
BufferedImage image = ImageIO.read(bis);
File outputFile = new File("output.png");
ImageIO.write(image, "png", outputFile);

First convert the Blob to BuffededImage:
Blob aBlob = rs.getBlob("Photo");
InputStream is = aBlob.getBinaryStream(1, aBlob.length());
BufferedImage image=ImageIO.read(is);
Then BufferedImage to Image:
try {
// Retrieve Image
File outputfile = new File("saved.png");
ImageIO.write(image, "png", outputfile); // Write the Buffered Image into an output file
Image image = ImageIO.read(new File("saved.png")); // Opening again as an Image
} catch (IOException e) {
...
}

Related

byte[] to jpg image?

I have the following code, that transforms an Image into a byte[]:
BufferedImage image = ImageIO.read(new File("Path/To/Custom/image.jpg"));
ByteArrayOutputStream baos=new ByteArrayOutputStream(1000);
ImageIO.write(image, "jpg", baos);
byte[] imageBytes = baos.toByteArray();
This code works totally fine, at least I get a byte array containing different values. But now comes the difficult part: The byte[] has to be reconstructed into am image again. The following code does not work, ImageIO.read(...) returns null. I read the documentation, but still I can not find out what to change so that the code functions in the way I want it to.
ByteArrayInputStream ba = new ByteArrayInputStream(imageBytes);
BufferedImage image = ImageIO.read(ba);
//image is always null, no matter what the stream or the byte values are.
import java.io.ByteArrayOutputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class ByteArrayToImage {
public static void main(String args[]) throws Exception {
BufferedImage bImage = ImageIO.read(new File("sample.jpg"));
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(bImage, "jpg", bos );
byte [] data = bos.toByteArray();
ByteArrayInputStream bis = new ByteArrayInputStream(data);
BufferedImage bImage2 = ImageIO.read(bis);
ImageIO.write(bImage2, "jpg", new File("output.jpg") );
System.out.println("image created");
}
}
Modify this to your needs.
Try reading an ByteArrayInputStream on ImageIO.read() not an ByteArrayOutputstream.

Images getting corrupted when JavaFX Image converted to BufferedImage and back again?

I'm designing a program that stores geography data on the JavaFX platform, and whenever I convert an image from a JavaFX Image into a BufferedImage then a ByteArray (for the purpose of serialization) before converting to a Buffered Image and then JavaFX Image again, it gets slightly corrupt. Here's the code I'm using to convert back and forth:
private byte [] loadImageData (Image image){
try{
//creating a byte array output stream from the Image
BufferedImage bi = SwingFXUtils.fromFXImage(image, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
ImageIO.write(bi, "png", baos );
baos.flush();
byte[] imageData = baos.toByteArray();
baos.close();
return imageData;
}catch (Exception e){
e.printStackTrace();
}
}
public Image restoreMapData (byte[] data){
try{
//converting back to an image
InputStream in = new ByteArrayInputStream(data);
BufferedImage bi = ImageIO.read(in);
return SwingFXUtils.toFXImage(bi, null);
}catch(Exception e){
e.printStackTrace();
return null;
}
}
Could there be an error elsewhere? I've attached a corrupted and un-corrupt picture of the data.
I also noticed that if I convert to BufferedImage with a type TYPE_INT_ARGB it greatly diminishes the effect.

How to convert Base64(imageurl) to an image

I have to convert image URL into an image. For that I tried following coding for convert base64 to an image. While on debugging the code "Bufferedimage image" is always null after ByteArrayInputStream bis=new ByteArrayInputStream(imagebyte). What can I do?
String imageStr = request.getParameter("imgURL");
BufferedImage image = null;
try {
BASE64Decoder decoder = new BASE64Decoder();
byte[] imageByte = decoder.decodeBuffer(imageStr);
ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
image = ImageIO.read(bis);
File outputfile = new File("E:\\saved.png");
ImageIO.write(image, "png", outputfile);
bis.close();
} catch (Exception e) {
e.printStackTrace();
}
I have to convert image URL into an image.
If that is your only requirement, then will this do?
URL url = new URL("www.example.com/image.png");
BufferedImage image = ImageIO.read(url);
File outputfile = new File("E:\\saved.png");
ImageIO.write(image, "png", outputfile);

How to convert PNG to jpeg in grails/java

I have a png file and tried to convert it to jpeg. But the resulting image has wrong colors with very big areas of pink. This is my code:
BufferedImage image = null
BufferedImage imageRGB = null
image = ImageIO.read(new ByteArrayInputStream(imageBytesPng))
imageRGB = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB)
imageRGB.setData(image.getData())
ByteArrayOutputStream baos=new ByteArrayOutputStream()
ImageIO.write(imageRGB, "jpeg", baos)
baos.flush()
def outImage = baos.toByteArray()
baos.close()
return outImage
What can I change to make the image colors apear as in the png file?
InputStream pngInputStream = ...
OutputStream jpgOutputStream = ...
BufferedImage image = ImageIO.read(pngInputStream));
ImageIO.write(image, "jpeg", jpgOutputStream);
Try this all the best..
import javax.media.jai.*;
public class jai_png_jpg
{
public static void main(String[] args)throws Exception
{
String filename="input_png.png";
//Read input PNG as a PlanarImage file
PlanarImage inputfile = JAI.create("fileload", filename);
//write output in JPG Format
JAI.create("filestore",inputfile,"jai_jpg_output.jpg","JPEG"); } }

Image transcoding (JPEG to PNG) with Java

In my Java application I would like to download a JPEG, transfer it to a PNG and do something with the resulting bytes.
I am almost certain I remember a library to do this exists, I cannot remember its name.
This is what I ended up doing, I was thinking toooo far outside of the box when I asked the question..
// these are the imports needed
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import java.io.ByteArrayOutputStream;
// read a jpeg from a inputFile
BufferedImage bufferedImage = ImageIO.read(new File(inputFile));
// write the bufferedImage back to outputFile
ImageIO.write(bufferedImage, "png", new File(outputFile));
// this writes the bufferedImage into a byte array called resultingBytes
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
ImageIO.write(bufferedImage, "png", byteArrayOut);
byte[] resultingBytes = byteArrayOut.toByteArray();
ImageIO can be used to load JPEG files and save PNG files (also into a ByteArrayOutputStream if you don't want to write to a file).
javax.imageio should be enough.
Put your JPEG to BufferedImage, then save it with:
File file = new File("newimage.png");
ImageIO.write(myJpegImage, "png", file);
BufferedImage bufferGambar;
try {
bufferGambar = ImageIO.read(new File("ImagePNG.png"));
// pkai type INT karna bertipe integer RGB bufferimage
BufferedImage newBufferGambar = new BufferedImage(bufferGambar.getWidth(), bufferGambar.getHeight(), BufferedImage.TYPE_INT_RGB);
newBufferGambar.createGraphics().drawImage(bufferGambar, 0, 0, Color.white, null);
ImageIO.write(newBufferGambar, "jpg", new File("Create file JPEG.jpg"));
JOptionPane.showMessageDialog(null, "Convert to JPG succes YES");
} catch(Exception e) {
JOptionPane.showMessageDialog(null, e);
}

Categories