App42 Cloud API

Windows Phone Cloud SDK

Image Processor

The ImageProcessor service is a Image utility service on the Cloud. Developers can upload files on the cloud and perform various Image Manipulation operations on the Uploaded Images e.g. resize, scale, thumbnail, crop etc. It is especially useful for Mobile Apps when they dont want to store Images locally and dont want to perform processor intensive operations. It is also useful for web applications who want to perform complex Image Operations

In order to use the various functions available under a particular API, a developer will have to create an instance of ServiceAPI by passing the apiKey and the secretKey which has been received while creating an app.

ServiceAPI api = new ServiceAPI("<API_KEY>","<SECRET_KEY>");       

After initialization, developer needs to call the BuildXXXService method on ServiceAPI instance to get the instance of the particular API that you wish to build. For example, To build an instance of UserService, BuildImageProcessorService() method needs to be called.

	ImageProcessorService imageProcesorService = api.BuildImageProcessorService();

Once the API instance has been retrieved, You are ready to use the functions available for that particular API.

The Windows Phone API's work in an asynchronous mode that means the response will not be available immediately and all the API methods would not return any value and have return type as void. The response will be returned in callback methods which the caller will provide to the calling method.
As mentioned above that the response will be returned in callback methods, every API method is taking instance of App42Callback (as the last parameter) which will have the implementation of 2 methods: OnSuccess & OnException.(as shown below)

public class CallBack : App42Callback
{
public override void OnSuccess(Object obj)
{
ImageProcessor imageProcessor = (ImageProcessor) obj;
//Do whatever you want with the user object
}
public override void OnException(App42Exception e)
{
//Do whatever you want with the exception
}
}

The callback methods will return the response in the form of Object which will have to be type cast to the object domain object (e.g. App42Response / ImageProcessor in this case) The Domain object have the accessor / mutator to access the available properties for that object. You can get the response in the form of JSON as well. We have provided the JSON response with every function detail which can be retrieved by calling the ToString() on the returned object.

public override void OnSuccess(Object obj)
   {
       ImageProcessor imageProcessor = (ImageProcessor) obj;
       String imageName =  imageProcessor.GetName(); 
       String originalImage =  imageProcessor.GetOriginalImage(); 
       String convertedImage =  imageProcessor.GetConvertedImage();
       String width =  imageProcessor.GetWidth(); 
       String height =  imageProcessor.GetHeight(); 			
       String jsonResponse = imageProcessor.ToString(); /* returns the response in JSON format. */ 
    }
                            

The functions available under Image Processor API can throw some exceptions in abnormal conditions. Example of the same has been given below.

E.g. If App developer is resizing the image file and storing it with the name which already exist in the database, the function will throw the App42Exception (as shown below) with message as "Bad Request" and the appErrorCode as "3200" and the details as "The request parameters are invalid. Image with the name '<name>' already exists".

		public override void OnException(App42Exception ex)
		{
			int appErrorCode = ex.GetAppErrorCode();
			int httpErrorCode = ex.GetHttpErrorCode();
			
			String jsonText = ex.GetMessage(); /* returns the Exception text in JSON format. (as shown below)*/	
		}
                            
	{
		"app42Fault": {
			"httpErrorCode": 400,
			"appErrorCode": 3200,
			"message": "Bad Request",
			"details": "The Request parameters are invalid. Image with the name 'Resize01' already Exists"
		}
	}							

Below are the HTTP Error Codes and their description, the function under the Image Processor API can throw.

/*
1400 - BAD REQUEST - The Request parameters are invalid
1401 - UNAUTHORIZED - Client is not authorized
1500 - INTERNAL SERVER ERROR - Internal Server Error. Please try again	
*/						

Below are the Application Error Codes and their description, the function under the Image Processor API can throw.

/*
3200 - BAD REQUEST - The request parameters are invalid. Image with the name '<name>' already exists.
*/			

Various functions available under ImageProcessor API has been explained below.

public void Resize(String name, Stream stream, Int32 width, Int32 height, App42Callback requestCallback)

Resize image. Returns the original image url and converted image url. Images are stored in the cloud and can be accessed through the urls Resizing is done based on the width and height provided.

Parameters:

name - Name of the image to resize
stream - Sequence of bytes of file to resize.
width - Width of the image to resize
height - Height of the image to resize
requestCallback - Callback instance that will have the implementation of OnSuccess & OnException of App42Callback.

Returns:

Image object containing urls for the original and converted images

Response: Image Object

Exception:

This method throws following exception.
/*
3200 - BAD REQUEST - The request parameters are invalid. Image with the name '<name>' already exists.
*/ 	        
For more details click Exception
String name = "Resize01';
Stream stream = "file stream";
Integer width = 234;
Integer height = 1000;

imageProcessorService.Resize(name,stream,width,height,requestCallback);  /* returns the Image object. */ 
OnSuccess(Object obj){
Image image = (Image) obj;
Console.WriteLine("imageName is " + image.GetName());
Console.WriteLine("originalImage is " + image.GetOriginalImage());
Console.WriteLine("convertedImage is " + image.GetConvertedImage());
Console.WriteLine("width is " + image.GetWidth());
Console.WriteLine("height is " + image.GetHeight());		 			
String jsonResponse = image.ToString(); /* returns the response in JSON format. (as shown below)*/
}
                            
{
    "app42": {
        "response": {
            "success": true,
            "image": {
                "name": "Resize01",
                "action": "RESIZE",
                "originalImage": "http://XXXX.jpg",
                "convertedImage": "XXXX-resize.jpg",
                "originalImageTinyUrl": "http://tinyurl.com/d2tj2vy",
                "convertedImageTinyUrl": "http://tinyurl.com/c3vknau",
                "width": 234,
                "height": 1000
            }
        }
    }
}
public void Thumbnail(String name, Stream stream, Int32 width, Int32 height, App42Callback requestCallback)

Creates a thumbnail of the image. There is a difference between thumbnail and resize The thumbnail operation is optimized for speed, it removes information of the image which is not necessary for a thumbnail e.g hearder information. Returns the original image url and converted image url. Images are stored on the cloud and can be accessed through the urls Resizing is done based on the width and height provided.

Parameters:

name - Name of the image file for which thumbnail has to be created
stream - Sequence of bytes of file whose thumbnail has to be created
width - Width of the image for thumbnail
height - Height of the image for thumbnail
requestCallback - Callback instance that will have the implementation of OnSuccess & OnException of App42Callback.

Returns:

Image object containing urls for the original and converted images

Response: Image Object

Exception:

This method throws following exception.
/*
3200 - BAD REQUEST - The request parameters are invalid. Image with the name '<name>' already exists.
*/ 		       
For more details click Exception
String name = "Thumbnail1";
Stream stream = "file stream";
Integer width = 234;
Integer height = 1000;		

imageProcessorService.Thumbnail(name,strem,width,height,requestCallback);  /* returns the Image object. */ 
OnSuccess(Object obj){
Image image = (Image) obj;
Console.WriteLine("imageName is " + image.GetName());
Console.WriteLine("originalImage is " + image.GetOriginalImage());
Console.WriteLine("convertedImage is " + image.GetConvertedImage());
Console.WriteLine("width is " + image.GetWidth());
Console.WriteLine("height is " + image.GetHeight()); 			
String jsonResponse = image.ToString(); /* returns the response in JSON format. (as shown below)*/
}
                            
{
    "app42": {
        "response": {
            "success": true,
            "image": {
                "name": "Thumbnail",
                "action": "THUMBNAIL",
                "originalImage": "http://XXXX.jpg",
                "convertedImage": "http://XXXX-thumbnail.jpg",
                "originalImageTinyUrl": "http://tinyurl.com/clyf8mz",
                "convertedImageTinyUrl": "http://tinyurl.com/buthakc",
                "width": 234,
                "height": 1000
            }
        }
    }
}
public void Scale(String name, Stream stream, Int32 width, Int32 height, App42Callback requestCallback)

Scales the image based on width and height. Returns the original image url and converted image url. Images are stored in the cloud and can be accessed through the urls Resizing is done based on the width and height provided

Parameters:

name - Name of the image to scale
stream - Sequence of bytes of file to scale
width - Width of the image to scale
height - Height of the image to scale
requestCallback - Callback instance that will have the implementation of OnSuccess & OnException of App42Callback.

Returns:

Image object containing urls for the original and converted images

Response: Image Object

Exception:

This method throws following exception.
/*
3200 - BAD REQUEST - The request parameters are invalid. Image with the name '<name>' already exists.
*/ 	       
For more details click Exception
String name = "Scale01";
Stream stream = "file stream";
Integer width = 234;
Integer height = 1000;

imageProcessorService.Scale(name,stream,width,height,requestCallback);  /* returns the Image object. */ 
OnSuccess(Object obj){
Image image = (Image) obj;
Console.WriteLine("imageName is " + image.GetName());
Console.WriteLine("originalImage is " + image.GetOriginalImage());
Console.WriteLine("convertedImage is " + image.GetConvertedImage());
Console.WriteLine("width is " + image.GetWidth());
Console.WriteLine("height is " + image.GetHeight());			
String jsonResponse = image.ToString(); /* returns the response in JSON format. (as shown below)*/
}
{
    "app42": {
        "response": {
            "success": true,
            "image": {
                "name": "Scale01",
                "action": "SCALE",
                "originalImage": "http://XXXX.jpg",
                "convertedImage": "http://XXXX-scale.jpg",
                "originalImageTinyUrl": "http://tinyurl.com/c35yhp2",
                "convertedImageTinyUrl": "http://tinyurl.com/blbw65b",
                "width": 234,
                "height": 1000
            }
        }
    }
}
public void Crop(String name, Stream stream, Int32 width, Int32 height, Int32 x, Int32 y, App42Callback requestCallback)

Crops image based on width, height and x, y coordinates. Returns the original image url and converted image url. Images are stored in the cloud and can be accessed through the urls Resizing is done based on the width and height provided

Parameters:

name - Name of the image to crop
stream - sequence of bytes of file to crop
width - Width of the image to crop
height - Height of the image to crop
x - Coordinate X
y - Coordinate Y
requestCallback - Callback instance that will have the implementation of OnSuccess & OnException of App42Callback.

Returns:

Image object containing urls for the original and converted images

Response: Image Object

Exception:

This method throws following exception.
/*
3200 - BAD REQUEST - The request parameters are invalid. Image with the name '<name>' already exists.
*/ 		    
For more details click Exception
String name = "Crop";
Stream stream = "file stream";
int width = 234;
int height = 1000;
int x = 34 ;
int y = 32 ;

imageProcessorService.Crop(name,stream,width,height,x,y,requestCallback);  /* returns the Image object. */ 
OnSuccess(Object obj){
Image image = (Image) obj;
Console.WriteLine("imageName is " + image.GetName());
Console.WriteLine("originalImage is " + image.GetOriginalImage());
Console.WriteLine("convertedImage is " + image.GetConvertedImage());
Console.WriteLine("width is " + image.GetWidth());
Console.WriteLine("height is " + image.GetHeight());			
String jsonResponse = image.ToString(); /* returns the response in JSON format. (as shown below)*/
}
{
    "app42": {
        "response": {
            "success": true,
            "image": {
                "name": "Crop",
                "action": "CROP",
                "originalImage": "http://XXXX.jpg",
                "convertedImage": "http://XXXX-crop.jpg",
                "originalImageTinyUrl": "http://tinyurl.com/cy9ydr2",
                "convertedImageTinyUrl": "http://tinyurl.com/cs9axzk",
                "width": 21,
                "height": 23,
                "x": 34,
                "y": 32
            }
        }
    }
}
public void ConvertFormat(String name, Stream stream, String formatToConvert,App42Callback requestCallback)

Converts the format of the image. Returns the original image url and converted image url. Images are stored on the cloud and can be accessed through the urls Conversion is done based on the formatToConvert provided

Parameters:

name - Name of the image to convert
stream - sequence of bytes of file to convert
formatToConvert - To which file needs to be converted
requestCallback - Callback instance that will have the implementation of OnSuccess & OnException of App42Callback.

Returns:

Image object containing urls for the original and converted images

Response: Image Object

Exception:

This method throws following exception.
/*
3200 - BAD REQUEST - The request parameters are invalid. Image with the name '<name>' already exists.
*/ 		    
For more details click Exception
String name = "Convert";
Stream stream = "file stream";
String formatToConvert = "new type in which file has to be converted";

imageProcessorService.ConvertFormat(name,stream,width,formatToConvert,requestCallback);  /* returns the Image object. */ 
OnSuccess(Object obj){
Image image = (Image) obj;
Console.WriteLine("imageName is " + image.GetName());
Console.WriteLine("originalImage is " + image.GetOriginalImage());
Console.WriteLine("convertedImage is " + image.GetConvertedImage());
String jsonResponse = image.ToString(); /* returns the response in JSON format. (as shown below)*/
}
{
    "app42": {
        "response": {
            "success": true,
            "image": {
                "name": "ResizeTue Oct 09 14:23:54 IST 2012",
                "action": "ConvertFormat",
                "originalImage": "http://XXXXX.jpg",
                "convertedImage": "http://XXXXX-convert.png",
                "originalImageTinyUrl": "http://tinyurl.com/XXXX",
                "convertedImageTinyUrl": "http://tinyurl.com/XXXX"
            }
        }
    }
}
public void ResizeByPercentage(String name, Stream stream, Int32 percentage, App42Callback requestCallback)

Resize image by Percentage. Returns the original image url and converted image url. Images are stored in the cloud and can be accessed through the urls Resizing is done based on the width and height provided

Parameters:

name - Name of the image to resize
stream - Sequence of bytes of file to resize
percentage - Percentage to which image has to be resized
requestCallback - Callback instance that will have the implementation of OnSuccess & OnException of App42Callback.

Returns:

Image object containing urls for the original and converted images

Response: Image Object

Exception:

This method throws following exception.
/*
3200 - BAD REQUEST - The request parameters are invalid. Image with the name '<name>' already exists.
*/ 		    
For more details click Exception
String name = "ResizeByPercentage02";
Stream stream = "file stream";
double percentage = 100;

imageProcessorService.ResizeByPercentage(name,stream,percentage,requestCallback);  /* returns the Image object. */ 
OnSuccess(Object obj){
Image image = (Image) obj;
Console.WriteLine("imageName is " + image.GetName());
Console.WriteLine("originalImage is " + image.GetOriginalImage());
Console.WriteLine("convertedImage is " + image.GetConvertedImage());
Console.WriteLine("percentage is " + image.GetPercentage());

String jsonResponse = image.ToString(); /* returns the response in JSON format. (as shown below)*/
}
{
    "app42": {
        "response": {
            "success": true,
            "image": {
                "name": "ResizeByPercentageTue Jul 24 13:20:55 IST 2012.jpg",
                "action": "RESIZE",
                "originalImage": "http://XXXX.jpg",
                "convertedImage": "http://XXXX-resize.jpg",
                "originalImageTinyUrl": "http://tinyurl.com/c5zo79m",
                "convertedImageTinyUrl": "http://tinyurl.com/ck4o4s5",
                "percentage": "10.9"
            }
        }
    }
}
public void ThumbnailByPercentage(String name, Stream stream, Int32 percentage, App42Callback requestCallback)

Creates a thumbnail of the image by Percentage. There is a difference between thumbnail and resize. The thumbnail operation is optimized for speed removes information of the image which is not necessary for a thumbnail to reduce size e.g hearder information. Returns the original image url and converted image url. Images are stored in the cloud and can be accessed through the urls. Resizing is done based on the width and height provided.

Parameters:

name - Name of the image file for which thumbnail has to be created
stream - Sequence of bytes of file whose thumbnail has to be created
percentage - Percentage for thumbnail
requestCallback - Callback instance that will have the implementation of OnSuccess & OnException of App42Callback.

Returns:

Image object containing urls for the original and converted images

Response: Image Object

Exception:

This method throws following exception.
/*
3200 - BAD REQUEST - The request parameters are invalid. Image with the name '<name>' already exists.
*/	        
For more details click Exception
String name = "ThumbnailByPercentage";
Stream stream = "file stream";
double percentage = 100;

imageProcessorService.ThumbnailByPercentage(name,stream,percentage,requestCallback);  /* returns the Image object. */ 
OnSuccess(Object obj){
Image image = (Image) obj;
Console.WriteLine("imageName is " + image.GetName());
Console.WriteLine("originalImage is " + image.GetOriginalImage());
Console.WriteLine("convertedImage is " + image.GetConvertedImage());
Console.WriteLine("percentage is " + image.GetPercentage());
String jsonResponse = image.ToString(); /* returns the response in JSON format. (as shown below)*/
}
                            
{
    "app42": {
        "response": {
            "success": true,
            "image": {
                "name": "ThumbnailByPercentage",
                "action": "THUMBNAIL",
                "originalImage": "http://XXXX.jpg",
                "convertedImage": "http://XXXX-thumbnail.jpg",
                "originalImageTinyUrl": "http://tinyurl.com/cgpqlja",
                "convertedImageTinyUrl": "http://tinyurl.com/cspufmt",
                "percentage": "10.7"
            }
        }
    }
}
public void ScaleByPercentage(String name, Stream stream, Int32 percentage, App42Callback requestCallback)

Scales the image by Percentage. Returns the original image url and converted image url. Images are stored in the cloud and can be accessed through the urls Resizing is done based on the width and height provided.

Parameters:

name - Name of the image file to scale
stream - Sequence of bytes of file to scale.
percentage - Percentage to which image has to be scaled
requestCallback - Callback instance that will have the implementation of OnSuccess & OnException of App42Callback.

Returns:

Image object containing urls for the original and converted images

Response: Image Object

Exception:

This method throws following exception.
/*
3200 - BAD REQUEST - The request parameters are invalid. Image with the name '<name>' already exists.
*/		       
For more details click Exception
String name = "ScaleByPercentage";
Stream stream = "file stream";
double percentage = 100;

imageProcessorService.ScaleByPercentage(name,stream ,percentage,requestCallback);  /* returns the Image object. */ 
OnSuccess(Object obj){
Image image = (Image) obj;
Console.WriteLine("imageName is " + image.GetName());
Console.WriteLine("originalImage is " + image.GetOriginalImage());
Console.WriteLine("convertedImage is " + image.GetConvertedImage());
Console.WriteLine("percentage is " + image.GetPercentage());
String jsonResponse = image.ToString(); /* returns the response in JSON format. (as shown below)*/
}
{
    "app42": {
        "response": {
            "success": true,
            "image": {
                "name": "ScaleByPercentage",
                "action": "SCALE",
                "originalImage": "http://XXXX.jpg",
                "convertedImage": "http://XXXX-scale.jpg",
                "originalImageTinyUrl": "http://tinyurl.com/csyv2f8",
                "convertedImageTinyUrl": "http://tinyurl.com/cwbdj73",
                "percentage": "10.9"
            }
        }
    }
}