ezvcard.types
Class PhotoType

java.lang.Object
  extended by ezvcard.types.VCardType
      extended by ezvcard.types.BinaryType<ImageTypeParameter>
          extended by ezvcard.types.PhotoType
All Implemented Interfaces:
Comparable<VCardType>

public class PhotoType
extends BinaryType<ImageTypeParameter>

A photo attached to the vCard (such as a portrait of the person).

Adding a photo

 VCard vcard = new VCard();
 
 //URL
 PhotoType photo = new PhotoType("http://www.mywebsite.com/mugshot.jpg", ImageTypeParameter.JPEG);
 vcard.addPhoto(photo);
 
 //binary data
 byte data[] = ...
 photo = new PhotoType(data, ImageTypeParameter.JPEG);
 vcard.addPhoto(photo);
 
 //if "ImageTypeParameter" does not have the pre-defined constant that you need, then create a new instance
 //arg 1: the value of the 2.1/3.0 TYPE parameter
 //arg 2: the value to use for the 4.0 MEDIATYPE parameter and for 4.0 data URIs
 //arg 3: the file extension of the data type (optional)
 ImageKeyTypeParameter param = new ImageTypeParameter("bmp", "image/x-ms-bmp", "bmp");
 photo = new PhotoType("http://www.mywebsite.com/mugshot.bmp", param);
 vcard.addPhoto(photo);
 

Getting the photos

 VCard vcard = ...
 
 int fileCount = 0;
 for (PhotoType photo : vcard.getPhotos()){
   //the photo will have either a URL or a binary data
   if (photo.getData() == null){
     System.out.println("Photo URL: " + photo.getUrl());
   } else {
     ImageTypeParameter type = photo.getContentType();
     
     if (type == null) {
       //the vCard may not have any content type data associated with the photo
       System.out.println("Saving a photo file...");
     } else {
       System.out.println("Saving a \"" + type.getMediaType() + "\" file...");
     }
     
     String folder;
     if (type == ImageTypeParameter.JPEG){ //it is safe to use "==" instead of "equals()"
       folder = "photos";
     } else {
       folder = "images";
     }
     
     byte data[] = photo.getData();
     String filename = "photo" + fileCount;
     if (type != null && type.getExtension() != null){
        filename += "." + type.getExtension();
     }
     OutputStream out = new FileOutputStream(new File(folder, filename));
     out.write(data);
     out.close();
     fileCount++;
   }
 }
 

vCard property name: PHOTO

vCard versions: 2.1, 3.0, 4.0

Author:
Michael Angstadt

Field Summary
static String NAME
           
 
Fields inherited from class ezvcard.types.BinaryType
contentType, data, url
 
Fields inherited from class ezvcard.types.VCardType
group, subTypes, typeName
 
Constructor Summary
PhotoType()
           
PhotoType(byte[] data, ImageTypeParameter type)
           
PhotoType(File file, ImageTypeParameter type)
           
PhotoType(InputStream in, ImageTypeParameter type)
           
PhotoType(String url, ImageTypeParameter type)
           
 
Method Summary
protected  ImageTypeParameter buildMediaTypeObj(String mediaType)
          Builds a MediaTypeParameter object based on the information in the MEDIATYPE parameter or data URI of 4.0 vCards.
protected  ImageTypeParameter buildTypeObj(String type)
          Builds a MediaTypeParameter object based on the value of the TYPE parameter in 2.1/3.0 vCards.
protected  void doUnmarshalHtml(HCardElement element, List<String> warnings)
          Unmarshals the type from an hCard (HTML document).
 
Methods inherited from class ezvcard.types.BinaryType
addPid, cannotUnmarshalValue, doMarshalSubTypes, doMarshalText, doMarshalXml, doUnmarshalText, doUnmarshalXml, getAltId, getContentType, getData, getPids, getPref, getType, getUrl, removePids, setAltId, setContentType, setData, setPref, setType, setUrl
 
Methods inherited from class ezvcard.types.VCardType
compareTo, getGroup, getQName, getSubTypes, getSupportedVersions, getTypeName, marshalSubTypes, marshalText, marshalXml, setGroup, unmarshalHtml, unmarshalText, unmarshalXml
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NAME

public static final String NAME
See Also:
Constant Field Values
Constructor Detail

PhotoType

public PhotoType()

PhotoType

public PhotoType(String url,
                 ImageTypeParameter type)
Parameters:
url - the URL to the photo
type - the content type (e.g. JPEG)

PhotoType

public PhotoType(byte[] data,
                 ImageTypeParameter type)
Parameters:
data - the binary data of the photo
type - the content type (e.g. JPEG)

PhotoType

public PhotoType(InputStream in,
                 ImageTypeParameter type)
          throws IOException
Parameters:
in - an input stream to the binary data (will be closed)
type - the content type (e.g. JPEG)
Throws:
IOException - if there's a problem reading from the input stream

PhotoType

public PhotoType(File file,
                 ImageTypeParameter type)
          throws IOException
Parameters:
file - the image file
type - the content type (e.g. JPEG)
Throws:
IOException - if there's a problem reading from the file
Method Detail

buildTypeObj

protected ImageTypeParameter buildTypeObj(String type)
Description copied from class: BinaryType
Builds a MediaTypeParameter object based on the value of the TYPE parameter in 2.1/3.0 vCards.

Specified by:
buildTypeObj in class BinaryType<ImageTypeParameter>
Parameters:
type - the TYPE value
Returns:
the parameter object

buildMediaTypeObj

protected ImageTypeParameter buildMediaTypeObj(String mediaType)
Description copied from class: BinaryType
Builds a MediaTypeParameter object based on the information in the MEDIATYPE parameter or data URI of 4.0 vCards.

Specified by:
buildMediaTypeObj in class BinaryType<ImageTypeParameter>
Parameters:
mediaType - the media type string (e.g. "image/jpeg")
Returns:
the parameter object

doUnmarshalHtml

protected void doUnmarshalHtml(HCardElement element,
                               List<String> warnings)
Description copied from class: VCardType
Unmarshals the type from an hCard (HTML document).

Overrides:
doUnmarshalHtml in class BinaryType<ImageTypeParameter>
Parameters:
element - the HTML element that contains the type data.
warnings - allows the programmer to alert the user to any note-worthy (but non-critical) issues that occurred during the unmarshalling process


Copyright © 2012-2013. All Rights Reserved.