Package gotnames.dm

Source Code of gotnames.dm.ProfilePictureData

package gotnames.dm;

import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;
import com.google.appengine.api.images.OutputSettings;
import com.google.appengine.api.images.Transform;
import com.google.appengine.api.images.Image.Format;
import com.google.appengine.api.images.ImagesService.OutputEncoding;
import com.medallia.tiny.Encoding;

/**
* Class which stores information about a profile picture. It has the profile
* picture data as a byte[] as well as the width and height and md5 of the
* image.
*/
public class ProfilePictureData {
  public final byte[] image;
  public final String md5;
  public final int width, height;
 
  ProfilePictureData(byte[] image, String md5, int width, int height) {
    this.image = image;
    this.md5 = md5;
    this.width = width;
    this.height = height;
  }
 
  /**
   * Read the given picture bytes and create a canonical
   * {@link ProfilePictureData}. The picture can be in any format supported by
   * AppEngine, but it will be resized to a standard size and converted to
   * JPEG.
   */
  public static ProfilePictureData getProfilePicture(byte[] rawProfilePictureBytes) {
    if (rawProfilePictureBytes == null || rawProfilePictureBytes.length == 0)
      return null;
   
    Image uploadedImage = ImagesServiceFactory.makeImage(rawProfilePictureBytes);
   
    Image profilePicture = normalizeProfilePicture(uploadedImage);
    byte[] imageData = profilePicture.getImageData();
   
    return new ProfilePictureData(imageData, Encoding.md5(imageData), profilePicture.getWidth(), profilePicture.getHeight());
  }
 
  private static Image normalizeProfilePicture(Image uploadedImage) {
    ImagesService imagesService = ImagesServiceFactory.getImagesService();
    Transform resize = ImagesServiceFactory.makeResize(250, 500);
   
    OutputSettings jpeg = new OutputSettings(OutputEncoding.JPEG);
   
    Image profileImage = imagesService.applyTransform(resize, uploadedImage, jpeg);
    if (profileImage.getFormat() != Format.JPEG)
      throw new AssertionError("Wrong format: " + profileImage.getFormat());
   
    return profileImage;
  }
 
 
}
TOP

Related Classes of gotnames.dm.ProfilePictureData

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.