byte[] to jpg image? - java

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.

Related

Java BLOB to image file

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) {
...
}

Resize a byte array image in pixels

I have an image stored as a byte array. Regardless how large the size, I want to always resize it down to 100x100 pixels.
I am trying to convert the byte array into a bufferedimage, resize and save it back as a bytearray. But with the following code, the images no longer appear on my page.
byte[] pict; // this array contains the image
BufferedImage bufferedImage;
ByteArrayInputStream bais = new ByteArrayInputStream(pict);
try {
bufferedImage = ImageIO.read(bais);
bufferedImage = Scalr.resize(bufferedImage, 100);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write( bufferedImage, "jpg", baos );
baos.flush();
pict = baos.toByteArray();
baos.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
OutputStream o = response.getOutputStream();
o.write(pict);
Do it this way instead:
Image tmpImage = ImageIO.read(bais);
Image scaled = tmpImage.getScaledInstance(100, 100, Image.SCALE_SMOOTH);
Then, to convert to a byte array, convert the image into a bufferedimage and then that into the byte array:
BufferedImage buffered = ((ToolkitImage) scaled).getBufferedImage();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(buffered, "jpg", baos);
baos.flush();
pict = baos.toByteArray();
baos.close();

how to get a image for a label from FileOutPutStream in java?

i created a QR generator in java.I tested my application using FileOutPutStream.So it worked properly,now i need to get the generated QR image to a jLabel before save,so how to do this.please help me??
this is my code:
ByteArrayOutputStream out = QRCode.from("Hello World").to(ImageType.PNG).stream();
ByteArrayOutputStream webout = QRCode.from("http://viralpatel.net").to(ImageType.PNG).stream();
try {
FileOutputStream fout = new FileOutputStream(new File("D:\\QR_Code.JPG"));
BufferedImage image= ImageIO.read(new FileOutputStream(fout)));
fout.write(out.toByteArray());
fout.flush();
fout.close();
} catch (Exception e){
}
A ByteArrayOutputStream is used to write to a final byte array.
A ByteArrayInputStream allows to read from an initial byte array.
So:
ByteArrayOutputStream out = ...
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
BufferedImage image= ImageIO.read(in);

Get pixel data from a BufferedImage

I need to obtain the pixel data from a BufferedImage so I can recreate the image from the data. I looked into Raster, but that did not seem to contain the information I need. How can I obtain data from a BufferedImage so I can recreate the image without needing the original file?
You should check out the answers to this question
Java - get pixel array from image
One way to do it is to use
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, "jpg", baos);
baos.flush();
byte[] imageBytes = baos.toByteArray();
baos.close();
Now when you want to create a new BufferedImage from the data you use
ByteArrayInputStream bais = new ByteArrayInputStream(imageBytes);
BufferedImage newImage = null;
try {
newImage = ImageIO.read(bais);
} catch (IOException e) {
// handle exception
}

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