Permalink
Browse files

1. Added cast parameter to PluginResult + script callback via 'eval' …

…js function;

2. Camera Api work in progress.
  • Loading branch information...
unknown Jesse
unknown authored and Jesse committed Aug 24, 2011
1 parent 156aaee commit f3cb136a6f38074dde652b82256088b132b75c6d
View
@@ -155,9 +155,9 @@
<li onclick="exec(successCallback, failureCallback,'Device','name');">Device Platform</li>
<li onclick="exec(successCallback, failureCallback,'Dummy','somemethod');">Unknown Service</li>
<li onclick="exec(successCallback, failureCallback,'Device','somemethod');">Unknown Method</li>
- <li onclick="exec(successCallback, failureCallback,'Capture','captureImage',{limit: 2});">captureImage</li>
- <li onclick="exec(successCallback, failureCallback,'Camera','getPicture');">ChoosePictureFromAlbom</li>
- <li onclick="exec(successCallback, failureCallback,'Capture','getFormatData',{filePath: 'somePath', mimeType: 'image/jpeg'});">getMediaData</li>
+ <li onclick="exec(successCallback, failureCallback,'Capture','captureImage',{limit: 2});">Capture.captureImage</li>
+ <li onclick="exec(successCallback, failureCallback,'Camera','getPicture',{ PictureSourceType: 1, DestinationType: 1});">Camera.getPicture</li>
+ <li onclick="exec(successCallback, failureCallback,'Capture','getFormatData',{fullPath: 'CapturedImagesCache\\CameraCapture-f3b7730b-ff17-46b7-809a-59c179d08fd8.jpg'});">Capture.getMediaData</li>
</ul>
<div id="divProperties" style="color:Orange;">Placeholder for app messages</div>
View
@@ -254,13 +254,20 @@ private void InvokeJSSCallback(String callbackId, PluginResult result)
throw new ArgumentNullException("result");
}
+ string callBackScript = result.ToCallbackString(callbackId, "commandResult", "commandError");
+
+ // TODO: this is correct invokation method
+ //this.GapBrowser.InvokeScript("eval", new string[] {callBackScript });
+
+
+ /// But we temporary use this version because C#<->JS bridge is on fully ready
if (result.IsSuccess)
{
- this.GapBrowser.InvokeScript("commandResult", new string[] {callbackId, result.ToJSONString()});
+ this.GapBrowser.InvokeScript("commandResult", new string[] { callbackId, result.ToJSONString() });
}
else
{
- this.GapBrowser.InvokeScript("commandError", new string[] {callbackId, result.ToJSONString()});
+ this.GapBrowser.InvokeScript("commandError", new string[] { callbackId, result.ToJSONString() });
}
}
}
@@ -54,13 +54,13 @@ public object InvokeMethodNamed(string methodName, params object[] args)
// options.Add("limit",2);
// args[0] = options;
//}
- if (methodName.Equals("getFormatData"))
- {
- var options = new Dictionary<string, object>();
- options.Add("filePath", "CapturedImagesCache\\CameraCapture-3768793c-1c66-424a-9e55-a941cbc158610.jpg");
- options.Add("mimeType", "image/jpeg");
- args[0] = options;
- }
+ //if (methodName.Equals("getFormatData"))
+ //{
+ // var options = new Dictionary<string, object>();
+ // options.Add("filePath", "CapturedImagesCache\\CameraCapture-3768793c-1c66-424a-9e55-a941cbc158610.jpg");
+ // options.Add("mimeType", "image/jpeg");
+ // args[0] = options;
+ //}
return mInfo.Invoke(this, args);
// every function handles DispatchCommandResult by itself
@@ -7,24 +7,262 @@
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
-using System.Windows.Shapes;
using System.Collections.Generic;
using Microsoft.Phone.Tasks;
+using System.Runtime.Serialization;
+using System.IO;
+using System.IO.IsolatedStorage;
namespace WP7GapClassLib.PhoneGap.Commands
{
public class Camera : BaseCommand
{
+
+ /// <summary>
+ /// Return base64 encoded string
+ /// </summary>
+ private const int DATA_URL = 0;
+
+ /// <summary>
+ /// Return file uri
+ /// </summary>
+ private const int FILE_URI = 1;
+
+ /// <summary>
+ /// Choose image from picture library
+ /// </summary>
+ private const int PHOTOLIBRARY = 0;
+
+ /// <summary>
+ /// Take picture from camera
+ /// </summary>
+
+ private const int CAMERA = 1;
+
+ /// <summary>
+ /// Choose image from picture library
+ /// </summary>
+ private const int SAVEDPHOTOALBUM = 2;
+
+ /// <summary>
+ /// Take a picture of type JPEG
+ /// </summary>
+ private const int JPEG = 0;
+
+ /// <summary>
+ /// Take a picture of type PNG
+ /// </summary>
+ private const int PNG = 1;
+
+ /// <summary>
+ /// Desired width of the image
+ /// </summary>
+ private int targetWidth;
+
+ /// <summary>
+ /// desired height of the image
+ /// </summary>
+ private int targetHeight;
+
+
+ /// <summary>
+ /// Folder to store captured images
+ /// </summary>
+ private string isoFolder = "CapturedImagesCache";
+
+ /// <summary>
+ /// Represents captureImage action options.
+ /// </summary>
+ [DataContract]
+ public class CameraOptions
+ {
+ /// <summary>
+ /// Source to getPicture from.
+ /// </summary>
+ [DataMember]
+ public int PictureSourceType { get; set; }
+
+ /// <summary>
+ /// Format of image that returned from getPicture.
+ /// </summary>
+ [DataMember]
+ public int DestinationType { get; set; }
+
+ /// <summary>
+ /// Encoding of image returned from getPicture.
+ /// </summary>
+ [DataMember]
+ public int EncodingType { get; set; }
+
+ public static CameraOptions Default
+ {
+ get { return new CameraOptions() { PictureSourceType = CAMERA, DestinationType = DATA_URL, EncodingType = JPEG}; }
+ }
+
+ }
+
+ /// <summary>
+ /// Used to open photo library
+ /// </summary>
+ PhotoChooserTask photoChooserTask;
+
+ /// <summary>
+ /// Used to open camera application
+ /// </summary>
+ CameraCaptureTask cameraTask;
+
+ /// <summary>
+ /// Camera options
+ /// </summary>
+ CameraOptions cameraOptions;
+
public void getPicture(string options)
{
- var photoChooserTask = new PhotoChooserTask();
- photoChooserTask.Completed += onComplited;
- photoChooserTask.Show();
+ try
+ {
+ this.cameraOptions = String.IsNullOrEmpty(options) ?
+ CameraOptions.Default : JSON.JsonHelper.Deserialize<CameraOptions>(options);
+
+ }
+ catch (Exception ex)
+ {
+ this.DispatchCommandResult(new PluginResult(PluginResult.Status.JSON_EXCEPTION, ex.Message));
+ return;
+ }
+
+ //TODO Check if all the options are acceptable
+
+
+ if (cameraOptions.PictureSourceType == CAMERA)
+ {
+ cameraTask = new CameraCaptureTask();
+ cameraTask.Completed += onComplited;
+ cameraTask.Show();
+ }
+ else
+ {
+ if ((cameraOptions.PictureSourceType == PHOTOLIBRARY) || (cameraOptions.PictureSourceType == SAVEDPHOTOALBUM))
+ {
+ photoChooserTask = new PhotoChooserTask();
+ photoChooserTask.Completed += onComplited;
+ photoChooserTask.Show();
+ }
+ else
+ {
+ DispatchCommandResult(new PluginResult(PluginResult.Status.NO_RESULT));
+ }
+ }
+
}
public void onComplited(object sender,PhotoResult e) {
- string name = e.OriginalFileName;
- DispatchCommandResult();
+ if (e.Error != null)
+ {
+ DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR));
+ return;
+ }
+
+ switch (e.TaskResult)
+ {
+ case TaskResult.OK:
+ try
+ {
+ string imageData = string.Empty;
+
+ if(cameraOptions.PictureSourceType == CAMERA)
+ {
+ if (cameraOptions.DestinationType == DATA_URL)
+ {
+ imageData = getBase64(e.ChosenPhoto);
+ }
+ else
+ {
+ byte[] imageBytes = new byte[e.ChosenPhoto.Length];
+ e.ChosenPhoto.Read(imageBytes, 0, imageBytes.Length);
+ imageData = this.SaveImageToLocalStorage(Path.GetFileName(e.OriginalFileName), isoFolder, imageBytes);
+ }
+ }
+ else
+ {
+ if (cameraOptions.DestinationType == DATA_URL)
+ {
+ imageData = getBase64(e.ChosenPhoto);
+ }
+ else
+ {
+ //TODO Set default value or return base64 or something else
+ }
+ }
+
+ DispatchCommandResult(new PluginResult(PluginResult.Status.OK,imageData));
+
+ }
+ catch (Exception ex)
+ {
+ DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, "Error retrieving image."));
+ }
+ break;
+
+ case TaskResult.Cancel:
+ DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR,"Selection cancelled."));
+ break;
+
+ default:
+ DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR,"Selection did not complete!"));
+ break;
+ }
+ }
+
+
+ /// <summary>
+ /// Creates base64 string from binary file
+ /// </summary>
+ /// <param name="stream"></param>
+ /// <returns></returns>
+ private string getBase64(Stream stream) {
+ int streamLength = (int)stream.Length;
+ byte[] fileData = new byte[streamLength + 1];
+ stream.Read(fileData, 0, streamLength);
+ stream.Close();
+ return Convert.ToBase64String(fileData);
+ }
+
+
+ /// <summary>
+ /// Saves captured image in isolated storage
+ /// </summary>
+ /// <param name="imageFileName">image file name</param>
+ /// <param name="imageFolder">folder to store images</param>
+ /// <returns>Image path</returns>
+ private string SaveImageToLocalStorage(string imageFileName, string imageFolder, byte[] imageBytes)
+ {
+ if (imageBytes == null)
+ {
+ throw new ArgumentNullException("imageBytes");
+ }
+ try
+ {
+ var isoFile = IsolatedStorageFile.GetUserStoreForApplication();
+
+ if (!isoFile.DirectoryExists(imageFolder))
+ {
+ isoFile.CreateDirectory(imageFolder);
+ }
+ string filePath = System.IO.Path.Combine(imageFolder, imageFileName);
+
+ using (var stream = isoFile.CreateFile(filePath))
+ {
+ stream.Write(imageBytes, 0, imageBytes.Length);
+ }
+
+ return filePath;
+ }
+ catch (Exception e)
+ {
+ //TODO: log or do something else
+ throw;
+ }
}
+
}
}
Oops, something went wrong.

0 comments on commit f3cb136

Please sign in to comment.