PImage base64 encode and decode

Hi everyone,

I would like to send a PImage over the network using JSON. I'm trying to make it work using base64 but i'm keep getting ClassCastException in decode stage. I noticed that if I save the BufferedImage to the hard drive and then load it using PApplet.loadImage(path), it works fine. Because I'm using Applets, I can't write to client's local file system, so I can't use this solution. Can anyone help? thanks

java.lang.ClassCastException: [I cannot be cast to [B
at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:364)
at processing.core.PImage.<init>(PImage.java:287)
at UserInterface.Applets.RecpieRenderingPApplet.DecodePImageFromBase64(RecpieRenderingPApplet.java:577)
at UserInterface.Applets.RecpieRenderingPApplet.setup(RecpieRenderingPApplet.java:75)
at processing.core.PApplet.handleDraw(PApplet.java:2281)
at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:237)
at processing.core.PApplet.run(PApplet.java:2177)
at java.lang.Thread.run(Thread.java:745)




import org.apache.commons.codec.binary.Base64;



public String EncodePImageToBase64(PImage i_Image) throws UnsupportedEncodingException, IOException
 {
    String result = null;
    BufferedImage buffImage = (BufferedImage)i_Image.getNative();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ImageIO.write(buffImage, "PNG", out);
    byte[] bytes = out.toByteArray();
    result = Base64.encodeBase64URLSafeString(bytes);

    return result;
 }

 public PImage DecodePImageFromBase64(String i_Image64) throws IOException
 {
    PImage result = null;
    byte[] decodedBytes = Base64.decodeBase64(i_Image64);
    InputStream in = new ByteArrayInputStream(decodedBytes);
    BufferedImage bImageFromConvert = ImageIO.read(in);
    result = new PImage(bImageFromConvert);

    return result;
 }

Answers

  • "[I cannot be cast to [B" means the methods expected to find an array of bytes [B and found an array of integers [I.
    Check ImageIO.read() if you can add some more parameters (like you did in write()), to help it interpreting the data.

  • Hi PhiLho,

    ImageIO.read() get only 1 parameter and I had no luck finding alternative.

  • Well, after a couple of sleepless nights, I managed to solve the problem. Here is the solution:

        public PImage DecodePImageFromBase64(String i_Image64) throws IOException
        {
           PImage result = null;
           byte[] decodedBytes = Base64.decodeBase64(i_Image64);
    
           ByteArrayInputStream in = new ByteArrayInputStream(decodedBytes);
           BufferedImage bImageFromConvert = ImageIO.read(in);
           BufferedImage convertedImg = new BufferedImage(bImageFromConvert.getWidth(),     bImageFromConvert.getHeight(), BufferedImage.TYPE_INT_ARGB);
           convertedImg.getGraphics().drawImage(bImageFromConvert, 0, 0, null);
           result = new PImage(convertedImg);
    
           return result;
        }
    
  • Ah, I thought the problem was in the ImageIO.read() call. The problem of reporting errors without pointing out where they happened...
    Glad you sorted out the problem by yourself.

Sign In or Register to comment.