mirror of
https://github.com/moparisthebest/davmail
synced 2024-12-13 19:22:22 -05:00
Carddav: resize contact picture
git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1212 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
parent
12013cd1ee
commit
fbfa09cba9
@ -26,6 +26,7 @@ import davmail.exception.HttpNotFoundException;
|
||||
import davmail.exchange.ExchangeSession;
|
||||
import davmail.http.DavGatewayHttpClientFacade;
|
||||
import davmail.ui.tray.DavGatewayTray;
|
||||
import davmail.util.IOUtil;
|
||||
import davmail.util.StringUtil;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.httpclient.*;
|
||||
@ -44,9 +45,7 @@ import org.apache.jackrabbit.webdav.property.DavPropertyNameSet;
|
||||
import org.apache.jackrabbit.webdav.property.DavPropertySet;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.mail.MessagingException;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.*;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.NoRouteToHostException;
|
||||
@ -651,16 +650,13 @@ public class DavExchangeSession extends ExchangeSession {
|
||||
String contactPictureUrl = getHref() + "/ContactPicture.jpg";
|
||||
String photo = get("photo");
|
||||
if (photo != null) {
|
||||
// photo url
|
||||
// need to update photo
|
||||
BufferedImage image = ImageIO.read(new ByteArrayInputStream(Base64.decodeBase64(photo.getBytes())));
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ImageIO.write(image, "jpg", baos);
|
||||
byte[] resizedImageBytes = IOUtil.resizeImage(Base64.decodeBase64(photo.getBytes()), 90);
|
||||
|
||||
final PutMethod putmethod = new PutMethod(URIUtil.encodePath(contactPictureUrl));
|
||||
putmethod.setRequestHeader("Overwrite", "t");
|
||||
putmethod.setRequestHeader("Content-Type", "image/jpeg");
|
||||
putmethod.setRequestEntity(new ByteArrayRequestEntity(baos.toByteArray(), "image/jpeg"));
|
||||
putmethod.setRequestEntity(new ByteArrayRequestEntity(resizedImageBytes, "image/jpeg"));
|
||||
try {
|
||||
status = httpClient.executeMethod(putmethod);
|
||||
if (status != HttpStatus.SC_OK && status != HttpStatus.SC_CREATED) {
|
||||
|
@ -22,6 +22,7 @@ import davmail.exception.DavMailAuthenticationException;
|
||||
import davmail.exception.DavMailException;
|
||||
import davmail.exchange.ExchangeSession;
|
||||
import davmail.http.DavGatewayHttpClientFacade;
|
||||
import davmail.util.IOUtil;
|
||||
import davmail.util.StringUtil;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.httpclient.HttpException;
|
||||
@ -29,9 +30,10 @@ import org.apache.commons.httpclient.HttpMethod;
|
||||
import org.apache.commons.httpclient.HttpStatus;
|
||||
import org.apache.commons.httpclient.methods.HeadMethod;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.*;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.NoRouteToHostException;
|
||||
import java.net.UnknownHostException;
|
||||
@ -768,12 +770,10 @@ public class EwsExchangeSession extends ExchangeSession {
|
||||
|
||||
if (photo != null) {
|
||||
// convert image to jpeg
|
||||
BufferedImage image = ImageIO.read(new ByteArrayInputStream(Base64.decodeBase64(photo.getBytes())));
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ImageIO.write(image, "jpg", baos);
|
||||
byte[] resizedImageBytes = IOUtil.resizeImage(Base64.decodeBase64(photo.getBytes()), 90);
|
||||
|
||||
// TODO: handle photo update, fix attachment mapi properties (available only with Exchange 2010)
|
||||
FileAttachment attachment = new FileAttachment("ContactPicture.jpg", "image/jpeg", new String(Base64.encodeBase64(baos.toByteArray())));
|
||||
FileAttachment attachment = new FileAttachment("ContactPicture.jpg", "image/jpeg", new String(Base64.encodeBase64(resizedImageBytes)));
|
||||
// update photo attachment
|
||||
CreateAttachmentMethod createAttachmentMethod = new CreateAttachmentMethod(newItemId, attachment);
|
||||
executeMethod(createAttachmentMethod);
|
||||
|
@ -18,9 +18,10 @@
|
||||
*/
|
||||
package davmail.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import javax.imageio.ImageIO;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* Input output functions.
|
||||
@ -43,4 +44,48 @@ public class IOUtil {
|
||||
outputStream.write(bytes, 0, length);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Resize image bytes to a max width or height image size.
|
||||
*
|
||||
* @param inputBytes input image bytes
|
||||
* @param max max size
|
||||
* @return scaled image bytes
|
||||
* @throws IOException on error
|
||||
*/
|
||||
public static byte[] resizeImage(byte[] inputBytes, int max) throws IOException {
|
||||
BufferedImage inputImage = ImageIO.read(new ByteArrayInputStream(inputBytes));
|
||||
BufferedImage outputImage = resizeImage(inputImage, max);
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ImageIO.write(outputImage, "jpg", baos);
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Resize image to a max width or height image size.
|
||||
*
|
||||
* @param inputImage input image
|
||||
* @param max max size
|
||||
* @return scaled image
|
||||
*/
|
||||
public static BufferedImage resizeImage(BufferedImage inputImage, int max) {
|
||||
int width = inputImage.getWidth();
|
||||
int height = inputImage.getHeight();
|
||||
int targetWidth;
|
||||
int targetHeight;
|
||||
if (width <= max && height <= max) {
|
||||
return inputImage;
|
||||
} else if (width > height) {
|
||||
targetWidth = max;
|
||||
targetHeight = targetWidth * height / width;
|
||||
} else {
|
||||
targetHeight = max;
|
||||
targetWidth = targetHeight * width / height;
|
||||
}
|
||||
Image scaledImage = inputImage.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH);
|
||||
BufferedImage targetImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
|
||||
targetImage.getGraphics().drawImage(scaledImage, 0, 0, null);
|
||||
return targetImage;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user