Fix issue with DATA_URL and refactor code

This commit is contained in:
macdonst 2011-09-30 11:30:04 -04:00
parent 6098f46d08
commit 80695ec5e4
2 changed files with 44 additions and 67 deletions

View File

@ -105,56 +105,46 @@ Camera.prototype.getPicture = function(successCallback, errorCallback, options)
console.log("Camera Error: errorCallback is not a function"); console.log("Camera Error: errorCallback is not a function");
return; return;
} }
this.options = options;
var quality = 80;
if (options.quality) {
quality = this.options.quality;
}
var maxResolution = 0; if (!options) {
if (options.maxResolution) { options = {};
maxResolution = this.options.maxResolution;
} }
if (!options.quality) {
var destinationType = Camera.DestinationType.DATA_URL; options.quality = 80;
if (this.options.destinationType) {
destinationType = this.options.destinationType;
} }
var sourceType = Camera.PictureSourceType.CAMERA; if (!options.maxResolution) {
if (typeof this.options.sourceType === "number") { options.maxResolution = 0;
sourceType = this.options.sourceType;
} }
var encodingType = Camera.EncodingType.JPEG; if (!options.destinationType) {
if (typeof options.encodingType == "number") { options.destinationType = Camera.DestinationType.DATA_URL;
encodingType = this.options.encodingType;
} }
var mediaType = Camera.MediaType.PICTURE; if (!options.sourceType) {
if (typeof options.mediaType == "number") { options.sourceType = Camera.PictureSourceType.CAMERA;
mediaType = this.options.mediaType;
} }
if (!options.encodingType) {
var targetWidth = -1; options.encodingType = Camera.EncodingType.JPEG;
if (typeof options.targetWidth == "number") { }
targetWidth = options.targetWidth; if (!options.mediaType) {
options.mediaType = Camera.MediaType.PICTURE;
}
if (!options.targetWidth) {
options.targetWidth = -1;
} else if (typeof options.targetWidth == "string") { } else if (typeof options.targetWidth == "string") {
var width = new Number(options.targetWidth); var width = new Number(options.targetWidth);
if (isNaN(width) === false) { if (isNaN(width) === false) {
targetWidth = width.valueOf(); options.targetWidth = width.valueOf();
} }
} }
if (!options.targetHeight) {
var targetHeight = -1; options.targetHeight = -1;
if (typeof options.targetHeight == "number") {
targetHeight = options.targetHeight;
} else if (typeof options.targetHeight == "string") { } else if (typeof options.targetHeight == "string") {
var height = new Number(options.targetHeight); var height = new Number(options.targetHeight);
if (isNaN(height) === false) { if (isNaN(height) === false) {
targetHeight = height.valueOf(); options.targetHeight = height.valueOf();
} }
} }
PhoneGap.exec(successCallback, errorCallback, "Camera", "takePicture", [quality, destinationType, sourceType, targetWidth, targetHeight, encodingType, mediaType]); PhoneGap.exec(successCallback, errorCallback, "Camera", "takePicture", [options]);
}; };
PhoneGap.addConstructor(function() { PhoneGap.addConstructor(function() {

View File

@ -17,6 +17,7 @@ import java.io.OutputStream;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject;
import com.phonegap.api.Plugin; import com.phonegap.api.Plugin;
import com.phonegap.api.PluginResult; import com.phonegap.api.PluginResult;
@ -28,7 +29,6 @@ import android.database.Cursor;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat; import android.graphics.Bitmap.CompressFormat;
import android.net.Uri; import android.net.Uri;
import android.provider.ContactsContract;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.util.Log; import android.util.Log;
@ -89,36 +89,30 @@ public class CameraLauncher extends Plugin {
try { try {
if (action.equals("takePicture")) { if (action.equals("takePicture")) {
int srcType = CAMERA;
int destType = DATA_URL; int destType = DATA_URL;
this.targetHeight = 0; this.targetHeight = 0;
this.targetWidth = 0; this.targetWidth = 0;
if (args.length() > 1) {
destType = args.getInt(1);
}
int srcType = CAMERA;
if (args.length() > 2) {
srcType = args.getInt(2);
}
if (args.length() > 3) {
this.targetWidth = args.getInt(3);
}
if (args.length() > 4) {
this.targetHeight = args.getInt(4);
}
this.encodingType = JPEG; this.encodingType = JPEG;
if (args.length() > 5) {
this.encodingType = args.getInt(5);
}
this.mediaType = PICTURE; this.mediaType = PICTURE;
if (args.length() > 6) { this.mQuality = 80;
this.mediaType = args.getInt(6);
JSONObject options = args.optJSONObject(0);
if (options != null) {
srcType = options.getInt("sourceType");
destType = options.getInt("destinationType");
this.targetHeight = options.getInt("targetHeight");
this.targetWidth = options.getInt("targetWidth");
this.encodingType = options.getInt("encodingType");
this.mediaType = options.getInt("mediaType");
this.mQuality = options.getInt("quality");
} }
if (srcType == CAMERA) { if (srcType == CAMERA) {
this.takePicture(args.getInt(0), destType, encodingType); this.takePicture(destType, encodingType);
} }
else if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) { else if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) {
this.getImage(args.getInt(0), srcType, destType); this.getImage(srcType, destType);
} }
PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT); PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
r.setKeepCallback(true); r.setKeepCallback(true);
@ -149,9 +143,7 @@ public class CameraLauncher extends Plugin {
* @param quality Compression quality hint (0-100: 0=low quality & high compression, 100=compress of max quality) * @param quality Compression quality hint (0-100: 0=low quality & high compression, 100=compress of max quality)
* @param returnType Set the type of image to return. * @param returnType Set the type of image to return.
*/ */
public void takePicture(int quality, int returnType, int encodingType) { public void takePicture(int returnType, int encodingType) {
this.mQuality = quality;
// Save the number of images currently on disk for later // Save the number of images currently on disk for later
this.numPics = queryImgDB().getCount(); this.numPics = queryImgDB().getCount();
@ -191,9 +183,7 @@ public class CameraLauncher extends Plugin {
* @param returnType Set the type of image to return. * @param returnType Set the type of image to return.
*/ */
// TODO: Images selected from SDCARD don't display correctly, but from CAMERA ALBUM do! // TODO: Images selected from SDCARD don't display correctly, but from CAMERA ALBUM do!
public void getImage(int quality, int srcType, int returnType) { public void getImage(int srcType, int returnType) {
this.mQuality = quality;
Intent intent = new Intent(); Intent intent = new Intent();
String title = GET_PICTURE; String title = GET_PICTURE;
if (this.mediaType == PICTURE) { if (this.mediaType == PICTURE) {
@ -447,22 +437,19 @@ public class CameraLauncher extends Plugin {
* @param type FILE_URI or DATA_URL * @param type FILE_URI or DATA_URL
*/ */
private void checkForDuplicateImage(int type) { private void checkForDuplicateImage(int type) {
int diff = 0; int diff = 1;
Cursor cursor = queryImgDB(); Cursor cursor = queryImgDB();
int currentNumOfImages = cursor.getCount(); int currentNumOfImages = cursor.getCount();
if (type == FILE_URI) { if (type == FILE_URI) {
diff = 1; diff = 2;
} }
// delete the duplicate file if the difference is 2 for file URI or 1 for Data URL // delete the duplicate file if the difference is 2 for file URI or 1 for Data URL
if ((currentNumOfImages - numPics) > diff) { if ((currentNumOfImages - numPics) == diff) {
cursor.moveToLast(); cursor.moveToLast();
int id = Integer.valueOf(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media._ID))) - 1; int id = Integer.valueOf(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media._ID))) - 1;
Uri uri = Uri.parse(MediaStore.Images.Media.EXTERNAL_CONTENT_URI + "/" + id); Uri uri = Uri.parse(MediaStore.Images.Media.EXTERNAL_CONTENT_URI + "/" + id);
Log.d(LOG_TAG, "Deleting URI = " + uri.toString());
this.ctx.getContentResolver().delete(uri, null, null); this.ctx.getContentResolver().delete(uri, null, null);
} }
} }