mirror of
https://github.com/apache/cordova-plugin-camera.git
synced 2025-01-18 19:22:51 +08:00
Updating Plugin from curent source
This commit is contained in:
parent
a0bff7b154
commit
aaef443dc6
@ -27,6 +27,7 @@ import java.io.IOException;
|
|||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
import org.apache.commons.codec.binary.Base64;
|
import org.apache.commons.codec.binary.Base64;
|
||||||
|
import org.apache.cordova.DirectoryManager;
|
||||||
import org.apache.cordova.FileHelper;
|
import org.apache.cordova.FileHelper;
|
||||||
import org.apache.cordova.api.CallbackContext;
|
import org.apache.cordova.api.CallbackContext;
|
||||||
import org.apache.cordova.api.CordovaPlugin;
|
import org.apache.cordova.api.CordovaPlugin;
|
||||||
@ -43,6 +44,7 @@ import android.graphics.Bitmap;
|
|||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
import android.graphics.Matrix;
|
import android.graphics.Matrix;
|
||||||
import android.graphics.Bitmap.CompressFormat;
|
import android.graphics.Bitmap.CompressFormat;
|
||||||
|
import android.graphics.Rect;
|
||||||
import android.media.MediaScannerConnection;
|
import android.media.MediaScannerConnection;
|
||||||
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
|
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
@ -397,19 +399,21 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
(destType == FILE_URI || destType == NATIVE_URI) && !this.correctOrientation) {
|
(destType == FILE_URI || destType == NATIVE_URI) && !this.correctOrientation) {
|
||||||
this.callbackContext.success(uri.toString());
|
this.callbackContext.success(uri.toString());
|
||||||
} else {
|
} else {
|
||||||
|
String uriString = uri.toString();
|
||||||
// Get the path to the image. Makes loading so much easier.
|
// Get the path to the image. Makes loading so much easier.
|
||||||
String imagePath = FileHelper.getRealPath(uri, this.cordova);
|
String mimeType = FileHelper.getMimeType(uriString, this.cordova);
|
||||||
String mimeType = FileHelper.getMimeType(imagePath, this.cordova);
|
|
||||||
// Log.d(LOG_TAG, "Real path = " + imagePath);
|
|
||||||
// Log.d(LOG_TAG, "mime type = " + mimeType);
|
|
||||||
// If we don't have a valid image so quit.
|
// If we don't have a valid image so quit.
|
||||||
if (imagePath == null || mimeType == null ||
|
if (!("image/jpeg".equalsIgnoreCase(mimeType) || "image/png".equalsIgnoreCase(mimeType))) {
|
||||||
!(mimeType.equalsIgnoreCase("image/jpeg") || mimeType.equalsIgnoreCase("image/png"))) {
|
|
||||||
Log.d(LOG_TAG, "I either have a null image path or bitmap");
|
Log.d(LOG_TAG, "I either have a null image path or bitmap");
|
||||||
this.failPicture("Unable to retrieve path to picture!");
|
this.failPicture("Unable to retrieve path to picture!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Bitmap bitmap = getScaledBitmap(imagePath);
|
Bitmap bitmap = null;
|
||||||
|
try {
|
||||||
|
bitmap = getScaledBitmap(uriString);
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
if (bitmap == null) {
|
if (bitmap == null) {
|
||||||
Log.d(LOG_TAG, "I either have a null image path or bitmap");
|
Log.d(LOG_TAG, "I either have a null image path or bitmap");
|
||||||
this.failPicture("Unable to create bitmap!");
|
this.failPicture("Unable to create bitmap!");
|
||||||
@ -417,14 +421,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (this.correctOrientation) {
|
if (this.correctOrientation) {
|
||||||
String[] cols = { MediaStore.Images.Media.ORIENTATION };
|
rotate = getImageOrientation(uri);
|
||||||
Cursor cursor = this.cordova.getActivity().getContentResolver().query(intent.getData(),
|
|
||||||
cols, null, null, null);
|
|
||||||
if (cursor != null) {
|
|
||||||
cursor.moveToPosition(0);
|
|
||||||
rotate = cursor.getInt(0);
|
|
||||||
cursor.close();
|
|
||||||
}
|
|
||||||
if (rotate != 0) {
|
if (rotate != 0) {
|
||||||
Matrix matrix = new Matrix();
|
Matrix matrix = new Matrix();
|
||||||
matrix.setRotate(rotate);
|
matrix.setRotate(rotate);
|
||||||
@ -444,15 +441,17 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
try {
|
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
|
||||||
String resizePath = DirectoryManager.getTempDirectoryPath(this.cordova.getActivity()) + "/resize.jpg";
|
String resizePath = DirectoryManager.getTempDirectoryPath(this.cordova.getActivity()) + "/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();
|
ExifHelper exif = new ExifHelper();
|
||||||
try {
|
if (realPath != null && this.encodingType == JPEG) {
|
||||||
if (this.encodingType == JPEG) {
|
try {
|
||||||
exif.createInFile(FileHelper.getRealPath(uri, this.cordova));
|
exif.createInFile(realPath);
|
||||||
exif.readExifData();
|
exif.readExifData();
|
||||||
rotate = exif.getOrientation();
|
rotate = exif.getOrientation();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
OutputStream os = new FileOutputStream(resizePath);
|
OutputStream os = new FileOutputStream(resizePath);
|
||||||
@ -460,7 +459,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
os.close();
|
os.close();
|
||||||
|
|
||||||
// Restore exif data to file
|
// Restore exif data to file
|
||||||
if (this.encodingType == JPEG) {
|
if (realPath != null && this.encodingType == JPEG) {
|
||||||
exif.createOutFile(resizePath);
|
exif.createOutFile(resizePath);
|
||||||
exif.writeExifData();
|
exif.writeExifData();
|
||||||
}
|
}
|
||||||
@ -494,6 +493,19 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int getImageOrientation(Uri uri) {
|
||||||
|
String[] cols = { MediaStore.Images.Media.ORIENTATION };
|
||||||
|
Cursor cursor = cordova.getActivity().getContentResolver().query(uri,
|
||||||
|
cols, null, null, null);
|
||||||
|
int rotate = 0;
|
||||||
|
if (cursor != null) {
|
||||||
|
cursor.moveToPosition(0);
|
||||||
|
rotate = cursor.getInt(0);
|
||||||
|
cursor.close();
|
||||||
|
}
|
||||||
|
return rotate;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Figure out if the bitmap should be rotated. For instance if the picture was taken in
|
* Figure out if the bitmap should be rotated. For instance if the picture was taken in
|
||||||
* portrait mode
|
* portrait mode
|
||||||
@ -564,17 +576,18 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
*
|
*
|
||||||
* @param imagePath
|
* @param imagePath
|
||||||
* @return
|
* @return
|
||||||
|
* @throws IOException
|
||||||
*/
|
*/
|
||||||
private Bitmap getScaledBitmap(String imagePath) {
|
private Bitmap getScaledBitmap(String imageUrl) throws IOException {
|
||||||
// If no new width or height were specified return the original bitmap
|
// If no new width or height were specified return the original bitmap
|
||||||
if (this.targetWidth <= 0 && this.targetHeight <= 0) {
|
if (this.targetWidth <= 0 && this.targetHeight <= 0) {
|
||||||
return BitmapFactory.decodeFile(imagePath);
|
return BitmapFactory.decodeStream(FileHelper.getInputStreamFromUriString(imageUrl, cordova));
|
||||||
}
|
}
|
||||||
|
|
||||||
// figure out the original width and height of the image
|
// figure out the original width and height of the image
|
||||||
BitmapFactory.Options options = new BitmapFactory.Options();
|
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||||
options.inJustDecodeBounds = true;
|
options.inJustDecodeBounds = true;
|
||||||
BitmapFactory.decodeFile(imagePath, options);
|
BitmapFactory.decodeStream(FileHelper.getInputStreamFromUriString(imageUrl, cordova), null, options);
|
||||||
|
|
||||||
//CB-2292: WTF? Why is the width null?
|
//CB-2292: WTF? Why is the width null?
|
||||||
if(options.outWidth == 0 || options.outHeight == 0)
|
if(options.outWidth == 0 || options.outHeight == 0)
|
||||||
@ -588,7 +601,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
// Load in the smallest bitmap possible that is closest to the size we want
|
// Load in the smallest bitmap possible that is closest to the size we want
|
||||||
options.inJustDecodeBounds = false;
|
options.inJustDecodeBounds = false;
|
||||||
options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, this.targetWidth, this.targetHeight);
|
options.inSampleSize = calculateSampleSize(options.outWidth, options.outHeight, this.targetWidth, this.targetHeight);
|
||||||
Bitmap unscaledBitmap = BitmapFactory.decodeFile(imagePath, options);
|
Bitmap unscaledBitmap = BitmapFactory.decodeStream(FileHelper.getInputStreamFromUriString(imageUrl, cordova), null, options);
|
||||||
if (unscaledBitmap == null) {
|
if (unscaledBitmap == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user