We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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;
}
Answers
Here's a working implementation :D
Neat! Thanks for sharing \m/ :bz
Kf
Excellent. Thanks for sharing this, @CharlesDesign.
Note that an equally valid option to apache's Base64 is to use Java 8
import java.util.Base64
. Then instead ofBase64.encodeBase64()
you useBase64.getEncoder().encode()
.So either:
or:
For discussion see: http://stackoverflow.com/a/13109632/7207622