Android Open Source - PicPosterComplete Bitmap Json Converter






From Project

Back to project page PicPosterComplete.

License

The source code is released under:

Apache License

If you think the Android project PicPosterComplete listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package ca.ualberta.cs.picposter.network;
//from   w  w  w.j  a va 2  s  .com
import java.io.ByteArrayOutputStream;
import java.lang.reflect.Type;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

/**
 * Provides custom base64 serialization / deserialization for Bitmaps.
 * Algorithm taken from: http://stackoverflow.com/questions/9224056/android-bitmap-to-base64-string
 * @author zjullion
 */
public class BitmapJsonConverter implements JsonDeserializer<Bitmap>,
    JsonSerializer<Bitmap> {

  
  @Override
  public JsonElement serialize(Bitmap src, Type typeOfSrc, JsonSerializationContext context) {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    src.compress(Bitmap.CompressFormat.JPEG, 80, stream);
    String base64Encoded = Base64.encodeToString(stream.toByteArray(), Base64.NO_WRAP);
    return new JsonPrimitive(base64Encoded);
  }

  
  @Override
  public Bitmap deserialize(JsonElement src, Type typeOfSrc, JsonDeserializationContext context) 
      throws JsonParseException {
    String base64Encoded = src.getAsJsonPrimitive().getAsString();
    byte[] data = Base64.decode(base64Encoded, Base64.NO_WRAP);
    return BitmapFactory.decodeByteArray(data, 0, data.length);
  }
}




Java Source Code List

ca.ualberta.cs.picposter.PicPosterActivity.java
ca.ualberta.cs.picposter.controller.PicPosterController.java
ca.ualberta.cs.picposter.model.PicPostModel.java
ca.ualberta.cs.picposter.model.PicPosterModelList.java
ca.ualberta.cs.picposter.network.BitmapJsonConverter.java
ca.ualberta.cs.picposter.network.ElasticSearchOperations.java
ca.ualberta.cs.picposter.network.ElasticSearchResponse.java
ca.ualberta.cs.picposter.network.ElasticSearchSearchResponse.java
ca.ualberta.cs.picposter.network.Hits.java
ca.ualberta.cs.picposter.view.PicPostModelAdapter.java