Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java converting byte[] to image

I know this seems like a common question but i have looked all over the internets, and tried many different tutorials and methods for doing this. I think i am close, but not sure. Also i am using Play Framework but it should be the same for java. here is my error

javax.image.IIOException: I/O error reading PNG header!
   at com.sun.plugins.png.PNGImageReader.readHeader(Unknown Source)
   ...
   ...
Caused by: java.io.EOFException
at javax.imageio.stream.ImageInputStreamImpl.readFully(Unknown Source)
  ...

Here is my code where i get the picture among other things from a form and convert the image to a byte[] and store in MS SQL db.

@Transactional
public static Result submitTrailer(){
     filledForm = newTrailerForm.bindFromRequest();
     MultipartFormData body = request().body().asMultipartFormData();
     FilePart picture = body.getFile("file");
     String fileName = picture.getFilename();
     System.out.println(fileName);
     String contentType = picture.getContentType(); 
     System.out.println(contentType);
     final File file = picture.getFile();
     filledForm.get().setContentType(contentType);

     try{
     BufferedImage originalImage = ImageIO.read(file);
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     ImageIO.write(originalImage, contentType, baos);

     filledForm.get().setImage(baos.toByteArray());
     baos.flush();
     baos.close();
     filledForm.get().save();
     }catch(IOException e){
         e.printStackTrace();
     }

    return ok(views.html.index.index.render());
}

here is where i am trying to covert the byte[] back to an image so i can display it in html

public File getConvertedPicture(){
    File imageFile;
    System.out.println("byteToImage() called");
    if(getImage()==null){
        System.out.println("getByteImage()==null");
        return null;

    }else{
        try{
        ByteArrayInputStream bis = new ByteArrayInputStream(getImage());
        imageFile=File.createTempFile("pattern", ".suffix");

        Iterator<?> readers = ImageIO.getImageReadersByFormatName("PNG");


        ImageReader reader = (ImageReader) readers.next();
         Object source = bis;
         ImageInputStream iis = ImageIO.createImageInputStream(source);
         reader.setInput(iis, true);
            ImageReadParam param = reader.getDefaultReadParam();

            Image image = reader.read(0, param);
            BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null), BufferedImage.TYPE_INT_RGB);
            Graphics2D g2 = bufferedImage.createGraphics();
            g2.drawImage(image, null, null);

            ImageIO.write(bufferedImage,"PNG", imageFile);
            return imageFile;

        }
        catch(IOException e){
            e.printStackTrace();
            return null;
        }
    }

I am a beginner, this is my first time using play, and first time using databases. Any advice to get this to work would be greatly appreciated.

also, in my method getConvertedPicture() i have to specify the format type, is there anyway to get around this so the user can upload any type of picture they want.

like image 825
mingle Avatar asked Aug 31 '26 14:08

mingle


1 Answers

To convert bytes to an image without knowing the file type I usually do:

ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
BufferedImage image = ImageIO.read(bais);

That will return a BufferedImage that you can save into any picture format such as jpg.

To write the image back out to a byte array:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", baos);
byte [] bytes = baos.toByteArray();
like image 74
David Avatar answered Sep 02 '26 05:09

David



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!