mirror of
https://github.com/apache/cordova-plugin-camera.git
synced 2025-04-21 10:16:24 +08:00
Refactor onActivityResult
This commit is contained in:
parent
ab5c957434
commit
61ba9cca4b
@ -265,25 +265,14 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Called when the camera view exits.
|
* Applies all needed transformation to the image received from the camera.
|
||||||
*
|
*
|
||||||
* @param requestCode The request code originally supplied to startActivityForResult(),
|
* @param destType In which form should we return the image
|
||||||
* allowing you to identify who this result came from.
|
|
||||||
* @param resultCode The integer result code returned by the child activity through its setResult().
|
|
||||||
* @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
|
* @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
|
||||||
*/
|
*/
|
||||||
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
private void processResultFromCamera(int destType, Intent intent) throws IOException {
|
||||||
|
|
||||||
// Get src and dest types from request code
|
|
||||||
int srcType = (requestCode / 16) - 1;
|
|
||||||
int destType = (requestCode % 16) - 1;
|
|
||||||
int rotate = 0;
|
int rotate = 0;
|
||||||
|
|
||||||
// If CAMERA
|
|
||||||
if (srcType == CAMERA) {
|
|
||||||
// If image available
|
|
||||||
if (resultCode == Activity.RESULT_OK) {
|
|
||||||
try {
|
|
||||||
// Create an ExifHelper to save the exif data that is lost during compression
|
// Create an ExifHelper to save the exif data that is lost during compression
|
||||||
ExifHelper exif = new ExifHelper();
|
ExifHelper exif = new ExifHelper();
|
||||||
try {
|
try {
|
||||||
@ -373,28 +362,45 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
|
|
||||||
this.cleanup(FILE_URI, this.imageUri, uri, bitmap);
|
this.cleanup(FILE_URI, this.imageUri, uri, bitmap);
|
||||||
bitmap = null;
|
bitmap = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String ouputResizedBitmap(Bitmap bitmap, Uri uri) throws IOException {
|
||||||
|
// Create an ExifHelper to save the exif data that is lost during compression
|
||||||
|
String resizePath = getTempDirectoryPath() + "/resize.jpg";
|
||||||
|
// Some content: URIs do not map to file paths (e.g. picasa).
|
||||||
|
String realPath = FileHelper.getRealPath(uri, this.cordova);
|
||||||
|
ExifHelper exif = new ExifHelper();
|
||||||
|
if (realPath != null && this.encodingType == JPEG) {
|
||||||
|
try {
|
||||||
|
exif.createInFile(realPath);
|
||||||
|
exif.readExifData();
|
||||||
|
rotate = exif.getOrientation();
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
this.failPicture("Error capturing image.");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If cancelled
|
OutputStream os = new FileOutputStream(resizePath);
|
||||||
else if (resultCode == Activity.RESULT_CANCELED) {
|
bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
|
||||||
this.failPicture("Camera cancelled.");
|
os.close();
|
||||||
|
|
||||||
|
// Restore exif data to file
|
||||||
|
if (realPath != null && this.encodingType == JPEG) {
|
||||||
|
exif.createOutFile(resizePath);
|
||||||
|
exif.writeExifData();
|
||||||
|
}
|
||||||
|
return resizePath;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If something else
|
/**
|
||||||
else {
|
* Applies all needed transformation to the image received from the gallery.
|
||||||
this.failPicture("Did not complete!");
|
*
|
||||||
}
|
* @param destType In which form should we return the image
|
||||||
}
|
* @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
|
||||||
|
*/
|
||||||
// If retrieving photo from library
|
private void processResultFromGallery(int destType, Intent intent) {
|
||||||
else if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) {
|
|
||||||
if (resultCode == Activity.RESULT_OK) {
|
|
||||||
Uri uri = intent.getData();
|
Uri uri = intent.getData();
|
||||||
|
int rotate = 0;
|
||||||
|
|
||||||
// If you ask for video or all media type you will automatically get back a file URI
|
// If you ask for video or all media type you will automatically get back a file URI
|
||||||
// and there will be no attempt to resize any returned data
|
// and there will be no attempt to resize any returned data
|
||||||
@ -448,31 +454,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
// Do we need to scale the returned file
|
// Do we need to scale the returned file
|
||||||
if (this.targetHeight > 0 && this.targetWidth > 0) {
|
if (this.targetHeight > 0 && this.targetWidth > 0) {
|
||||||
try {
|
try {
|
||||||
// Create an ExifHelper to save the exif data that is lost during compression
|
String resizePath = this.ouputResizedBitmap(bitmap, uri);
|
||||||
String resizePath = getTempDirectoryPath() + "/resize.jpg";
|
|
||||||
// Some content: URIs do not map to file paths (e.g. picasa).
|
|
||||||
String realPath = FileHelper.getRealPath(uri, this.cordova);
|
|
||||||
ExifHelper exif = new ExifHelper();
|
|
||||||
if (realPath != null && this.encodingType == JPEG) {
|
|
||||||
try {
|
|
||||||
exif.createInFile(realPath);
|
|
||||||
exif.readExifData();
|
|
||||||
rotate = exif.getOrientation();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
OutputStream os = new FileOutputStream(resizePath);
|
|
||||||
bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
|
|
||||||
os.close();
|
|
||||||
|
|
||||||
// Restore exif data to file
|
|
||||||
if (realPath != null && this.encodingType == JPEG) {
|
|
||||||
exif.createOutFile(resizePath);
|
|
||||||
exif.writeExifData();
|
|
||||||
}
|
|
||||||
|
|
||||||
// The resized image is cached by the app in order to get around this and not have to delete you
|
// The resized image is cached by the app in order to get around this and not have to delete you
|
||||||
// application cache I'm adding the current system time to the end of the file url.
|
// application cache I'm adding the current system time to the end of the file url.
|
||||||
this.callbackContext.success("file://" + resizePath + "?" + System.currentTimeMillis());
|
this.callbackContext.success("file://" + resizePath + "?" + System.currentTimeMillis());
|
||||||
@ -493,6 +475,49 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the camera view exits.
|
||||||
|
*
|
||||||
|
* @param requestCode The request code originally supplied to startActivityForResult(),
|
||||||
|
* allowing you to identify who this result came from.
|
||||||
|
* @param resultCode The integer result code returned by the child activity through its setResult().
|
||||||
|
* @param intent An Intent, which can return result data to the caller (various data can be attached to Intent "extras").
|
||||||
|
*/
|
||||||
|
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
|
||||||
|
|
||||||
|
// Get src and dest types from request code
|
||||||
|
int srcType = (requestCode / 16) - 1;
|
||||||
|
int destType = (requestCode % 16) - 1;
|
||||||
|
|
||||||
|
// If CAMERA
|
||||||
|
if (srcType == CAMERA) {
|
||||||
|
// If image available
|
||||||
|
if (resultCode == Activity.RESULT_OK) {
|
||||||
|
try {
|
||||||
|
this.processResultFromCamera(destType, intent);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
this.failPicture("Error capturing image.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If cancelled
|
||||||
|
else if (resultCode == Activity.RESULT_CANCELED) {
|
||||||
|
this.failPicture("Camera cancelled.");
|
||||||
|
}
|
||||||
|
|
||||||
|
// If something else
|
||||||
|
else {
|
||||||
|
this.failPicture("Did not complete!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If retrieving photo from library
|
||||||
|
else if ((srcType == PHOTOLIBRARY) || (srcType == SAVEDPHOTOALBUM)) {
|
||||||
|
if (resultCode == Activity.RESULT_OK) {
|
||||||
|
this.processResultFromGallery(destType, intent)
|
||||||
|
}
|
||||||
else if (resultCode == Activity.RESULT_CANCELED) {
|
else if (resultCode == Activity.RESULT_CANCELED) {
|
||||||
this.failPicture("Selection cancelled.");
|
this.failPicture("Selection cancelled.");
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user