mirror of
https://github.com/apache/cordova-plugin-camera.git
synced 2025-01-19 03:42:52 +08:00
Removing commons-codec dependency, this should be ready to go
This commit is contained in:
parent
04cfa1a949
commit
1e02cf281a
@ -20,18 +20,14 @@ package org.apache.cordova.core;
|
|||||||
|
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
|
|
||||||
import org.apache.commons.codec.binary.Base64;
|
|
||||||
import org.apache.cordova.DirectoryManager;
|
|
||||||
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;
|
||||||
import org.apache.cordova.api.DataResource;
|
|
||||||
import org.apache.cordova.api.LOG;
|
import org.apache.cordova.api.LOG;
|
||||||
import org.apache.cordova.api.PluginResult;
|
import org.apache.cordova.api.PluginResult;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
@ -45,11 +41,13 @@ 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;
|
||||||
import android.os.Environment;
|
import android.os.Environment;
|
||||||
import android.provider.MediaStore;
|
import android.provider.MediaStore;
|
||||||
|
import android.util.Base64;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -292,7 +290,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
|
|
||||||
// If sending base64 image back
|
// If sending base64 image back
|
||||||
if (destType == DATA_URL) {
|
if (destType == DATA_URL) {
|
||||||
bitmap = getScaledBitmap(imageUri.toString());
|
bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));
|
||||||
if (bitmap == null) {
|
if (bitmap == null) {
|
||||||
// Try to get the bitmap from intent.
|
// Try to get the bitmap from intent.
|
||||||
bitmap = (Bitmap)intent.getExtras().get("data");
|
bitmap = (Bitmap)intent.getExtras().get("data");
|
||||||
@ -318,9 +316,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
if (this.saveToPhotoAlbum) {
|
if (this.saveToPhotoAlbum) {
|
||||||
Uri inputUri = getUriFromMediaStore();
|
Uri inputUri = getUriFromMediaStore();
|
||||||
//Just because we have a media URI doesn't mean we have a real file, we need to make it
|
//Just because we have a media URI doesn't mean we have a real file, we need to make it
|
||||||
DataResource dataResource = DataResource.initiateNewDataRequestForUri(inputUri, webView.pluginManager, cordova, "CameraLauncher.CameraExitIntent");
|
uri = Uri.fromFile(new File(FileHelper.getRealPath(inputUri, this.cordova)));
|
||||||
File file = dataResource.getRealFile();
|
|
||||||
uri = Uri.fromFile(file);
|
|
||||||
} else {
|
} else {
|
||||||
uri = Uri.fromFile(new File(DirectoryManager.getTempDirectoryPath(this.cordova.getActivity()), System.currentTimeMillis() + ".jpg"));
|
uri = Uri.fromFile(new File(DirectoryManager.getTempDirectoryPath(this.cordova.getActivity()), System.currentTimeMillis() + ".jpg"));
|
||||||
}
|
}
|
||||||
@ -336,15 +332,14 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
|
|
||||||
this.callbackContext.success(uri.toString());
|
this.callbackContext.success(uri.toString());
|
||||||
} else {
|
} else {
|
||||||
bitmap = getScaledBitmap(imageUri.toString());
|
bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));
|
||||||
|
|
||||||
if (rotate != 0 && this.correctOrientation) {
|
if (rotate != 0 && this.correctOrientation) {
|
||||||
bitmap = getRotatedBitmap(rotate, bitmap, exif);
|
bitmap = getRotatedBitmap(rotate, bitmap, exif);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add compressed version of captured image to returned media store Uri
|
// Add compressed version of captured image to returned media store Uri
|
||||||
DataResource dataResource = DataResource.initiateNewDataRequestForUri(uri, webView.pluginManager, cordova, "CameraLauncher.CameraExitIntent");
|
OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri);
|
||||||
OutputStream os = dataResource.getOutputStream();
|
|
||||||
bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
|
bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
|
||||||
os.close();
|
os.close();
|
||||||
|
|
||||||
@ -352,7 +347,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
if (this.encodingType == JPEG) {
|
if (this.encodingType == JPEG) {
|
||||||
String exifPath;
|
String exifPath;
|
||||||
if (this.saveToPhotoAlbum) {
|
if (this.saveToPhotoAlbum) {
|
||||||
exifPath = dataResource.getRealFile().getPath();
|
exifPath = FileHelper.getRealPath(uri, this.cordova);
|
||||||
} else {
|
} else {
|
||||||
exifPath = uri.getPath();
|
exifPath = uri.getPath();
|
||||||
}
|
}
|
||||||
@ -403,9 +398,8 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
this.callbackContext.success(uri.toString());
|
this.callbackContext.success(uri.toString());
|
||||||
} else {
|
} else {
|
||||||
String uriString = uri.toString();
|
String uriString = uri.toString();
|
||||||
DataResource dataResource = DataResource.initiateNewDataRequestForUri(uri, webView.pluginManager, cordova, "CameraLauncher.CameraExitIntent");
|
|
||||||
// Get the path to the image. Makes loading so much easier.
|
// Get the path to the image. Makes loading so much easier.
|
||||||
String mimeType = dataResource.getMimeType();
|
String mimeType = FileHelper.getMimeType(uriString, this.cordova);
|
||||||
// If we don't have a valid image so quit.
|
// If we don't have a valid image so quit.
|
||||||
if (!("image/jpeg".equalsIgnoreCase(mimeType) || "image/png".equalsIgnoreCase(mimeType))) {
|
if (!("image/jpeg".equalsIgnoreCase(mimeType) || "image/png".equalsIgnoreCase(mimeType))) {
|
||||||
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");
|
||||||
@ -446,8 +440,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
// 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).
|
// Some content: URIs do not map to file paths (e.g. picasa).
|
||||||
File realFile = DataResource.initiateNewDataRequestForUri(uri, webView.pluginManager, cordova, "CameraLauncher.CameraExitIntent").getRealFile();
|
String realPath = FileHelper.getRealPath(uri, this.cordova);
|
||||||
String realPath = realFile != null? realFile.getPath() : null;
|
|
||||||
ExifHelper exif = new ExifHelper();
|
ExifHelper exif = new ExifHelper();
|
||||||
if (realPath != null && this.encodingType == JPEG) {
|
if (realPath != null && this.encodingType == JPEG) {
|
||||||
try {
|
try {
|
||||||
@ -541,15 +534,8 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
*/
|
*/
|
||||||
private void writeUncompressedImage(Uri uri) throws FileNotFoundException,
|
private void writeUncompressedImage(Uri uri) throws FileNotFoundException,
|
||||||
IOException {
|
IOException {
|
||||||
DataResource inputDataResource = DataResource.initiateNewDataRequestForUri(imageUri, webView.pluginManager, cordova, "CameraLauncher.writeUncompressedImage");
|
FileInputStream fis = new FileInputStream(FileHelper.stripFileProtocol(imageUri.toString()));
|
||||||
InputStream fis = inputDataResource.getInputStream();
|
OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri);
|
||||||
DataResource outDataResource = DataResource.initiateNewDataRequestForUri(uri, webView.pluginManager, cordova, "CameraLauncher.writeUncompressedImage");
|
|
||||||
OutputStream os = outDataResource.getOutputStream();
|
|
||||||
if(fis == null) {
|
|
||||||
throw new FileNotFoundException("Could not get the input file");
|
|
||||||
} else if(os == null) {
|
|
||||||
throw new FileNotFoundException("Could not get the output file");
|
|
||||||
}
|
|
||||||
byte[] buffer = new byte[4096];
|
byte[] buffer = new byte[4096];
|
||||||
int len;
|
int len;
|
||||||
while ((len = fis.read(buffer)) != -1) {
|
while ((len = fis.read(buffer)) != -1) {
|
||||||
@ -592,15 +578,14 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
*/
|
*/
|
||||||
private Bitmap getScaledBitmap(String imageUrl) throws IOException {
|
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
|
||||||
DataResource dataResource = DataResource.initiateNewDataRequestForUri(imageUrl, webView.pluginManager, cordova, "CameraLauncher.getScaledBitmap");
|
|
||||||
if (this.targetWidth <= 0 && this.targetHeight <= 0) {
|
if (this.targetWidth <= 0 && this.targetHeight <= 0) {
|
||||||
return BitmapFactory.decodeStream(dataResource.getInputStream());
|
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.decodeStream(dataResource.getInputStream(), null, 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)
|
||||||
@ -614,7 +599,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.decodeStream(dataResource.getInputStream(), null, options);
|
Bitmap unscaledBitmap = BitmapFactory.decodeStream(FileHelper.getInputStreamFromUriString(imageUrl, cordova), null, options);
|
||||||
if (unscaledBitmap == null) {
|
if (unscaledBitmap == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -713,11 +698,8 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
bitmap.recycle();
|
bitmap.recycle();
|
||||||
}
|
}
|
||||||
|
|
||||||
DataResource dataResource = DataResource.initiateNewDataRequestForUri(oldImage, webView.pluginManager, cordova, "CameraLauncher.cleanup");
|
|
||||||
File file = dataResource.getRealFile();
|
|
||||||
if(file != null) {
|
|
||||||
// Clean up initial camera-written image file.
|
// Clean up initial camera-written image file.
|
||||||
file.delete();
|
(new File(FileHelper.stripFileProtocol(oldImage.toString()))).delete();
|
||||||
|
|
||||||
checkForDuplicateImage(imageType);
|
checkForDuplicateImage(imageType);
|
||||||
// Scan for the gallery to update pic refs in gallery
|
// Scan for the gallery to update pic refs in gallery
|
||||||
@ -727,7 +709,6 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
|
|
||||||
System.gc();
|
System.gc();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used to find out if we are in a situation where the Camera Intent adds to images
|
* Used to find out if we are in a situation where the Camera Intent adds to images
|
||||||
@ -780,7 +761,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
try {
|
try {
|
||||||
if (bitmap.compress(CompressFormat.JPEG, mQuality, jpeg_data)) {
|
if (bitmap.compress(CompressFormat.JPEG, mQuality, jpeg_data)) {
|
||||||
byte[] code = jpeg_data.toByteArray();
|
byte[] code = jpeg_data.toByteArray();
|
||||||
byte[] output = Base64.encodeBase64(code);
|
byte[] output = Base64.encode(code, Base64.DEFAULT);
|
||||||
String js_out = new String(output);
|
String js_out = new String(output);
|
||||||
this.callbackContext.success(js_out);
|
this.callbackContext.success(js_out);
|
||||||
js_out = null;
|
js_out = null;
|
||||||
|
Loading…
Reference in New Issue
Block a user