Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

PImage to Base64 for API upload

$
0
0

Hi All,

Based on this thread and some code from @snir102002 I have put a sketch together which encodes an image to Base64, decodes it and then displays it. This works as expected.

However, when I send the Base64 string to my API I get a response telling me that my string isn't valid. For troubleshooting I've saved the string in a txt file, copied it and pasted in a an Base64 online decoder but none of them seem to be able to interpret it.

Where am I going wrong?

Here's an executable code:

import org.apache.commons.codec.binary.Base64;
import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;

void setup() {
  size(1200, 600);
  PImage img = loadImage("http" + "://i.stack.imgur.com/WCveg.jpg");
  image(img, 0, 0);

  String encoded = "";
  PImage decoded = createImage(img.width, img.height, RGB);

  try {
    encoded = EncodePImageToBase64(img);
    //println(encoded);
  }
  catch (IOException e) {
    println(e);
  }
  String [] hell = {encoded};

  saveStrings("encoded.txt", hell);

  try {
    decoded = DecodePImageFromBase64(encoded);
    println(decoded);
  }
  catch (IOException e) {
    println(e);
  }

  image(decoded, img.width, 0);
}



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);

  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;
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles