Building Mobile Enterprise Applications : webMethods Mobile Development Help : Code Snippets : Encoding and Decoding Images with Base64
Encoding and Decoding Images with Base64
When using RESTful services or web services, it is often required to send binary data (such as images) encoded as String. This can be achieved by using several API classes, as described in the following snippet:
import org.apache.axis.j2me.rpc.Base64;
import javax.microedition.lcdui.Image;
import com.softwareag.mobile.runtime.media.ImageHandler;

public String encodeImage(final Image image) {
String encodedImage = null;
if (image != null) {
final byte[] bytes = ImageHandler.pngCreateByteArray(image);
final byte[] encodedBytes = Base64.encode(bytes);
encodedImage = new String(encodedBytes);
}
return encodedImage;
}

public Image decodeImage(final String encodedImage) {
Image image = null;
if (!AppUtility.instance.isEmpty(encodedImage)) {
final byte[] decodedBytes = Base64.decode(encodedImage.getBytes());
if ((decodedBytes != null) & (decodedBytes.length > 0)) {
image = Image.createImage(decodedBytes, 0, decodedBytes.length);
}
}
return image;
}
Copyright © 2007-2017 Software AG, Darmstadt, Germany.

Product LogoContact Support   |   Community   |   Feedback