mirror of
https://github.com/apache/cordova-plugin-camera.git
synced 2025-01-31 12:42:50 +08:00
CB-9623 Fixes various issues when encodingType set to png
This commit is contained in:
parent
cad9ab0419
commit
dca8bd1943
@ -241,15 +241,30 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
* @return a File object pointing to the temporary picture
|
* @return a File object pointing to the temporary picture
|
||||||
*/
|
*/
|
||||||
private File createCaptureFile(int encodingType) {
|
private File createCaptureFile(int encodingType) {
|
||||||
File photo = null;
|
return createCaptureFile(encodingType, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a file in the applications temporary directory based upon the supplied encoding.
|
||||||
|
*
|
||||||
|
* @param encodingType of the image to be taken
|
||||||
|
* @param fileName or resultant File object.
|
||||||
|
* @return a File object pointing to the temporary picture
|
||||||
|
*/
|
||||||
|
private File createCaptureFile(int encodingType, String fileName) {
|
||||||
|
if (fileName.isEmpty()) {
|
||||||
|
fileName = ".Pic";
|
||||||
|
}
|
||||||
|
|
||||||
if (encodingType == JPEG) {
|
if (encodingType == JPEG) {
|
||||||
photo = new File(getTempDirectoryPath(), ".Pic.jpg");
|
fileName = fileName + ".jpg";
|
||||||
} else if (encodingType == PNG) {
|
} else if (encodingType == PNG) {
|
||||||
photo = new File(getTempDirectoryPath(), ".Pic.png");
|
fileName = fileName + ".png";
|
||||||
} else {
|
} else {
|
||||||
throw new IllegalArgumentException("Invalid Encoding Type: " + encodingType);
|
throw new IllegalArgumentException("Invalid Encoding Type: " + encodingType);
|
||||||
}
|
}
|
||||||
return photo;
|
|
||||||
|
return new File(getTempDirectoryPath(), fileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -334,7 +349,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
cropIntent.putExtra("aspectY", 1);
|
cropIntent.putExtra("aspectY", 1);
|
||||||
}
|
}
|
||||||
// create new file handle to get full resolution crop
|
// create new file handle to get full resolution crop
|
||||||
croppedUri = Uri.fromFile(new File(getTempDirectoryPath(), System.currentTimeMillis() + ".jpg"));
|
croppedUri = Uri.fromFile(createCaptureFile(this.encodingType, System.currentTimeMillis() + ""));
|
||||||
cropIntent.putExtra("output", croppedUri);
|
cropIntent.putExtra("output", croppedUri);
|
||||||
|
|
||||||
// start the activity - we handle returning in onActivityResult
|
// start the activity - we handle returning in onActivityResult
|
||||||
@ -367,24 +382,20 @@ 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
|
||||||
ExifHelper exif = new ExifHelper();
|
ExifHelper exif = new ExifHelper();
|
||||||
String sourcePath;
|
String sourcePath = (this.allowEdit && this.croppedUri != null) ?
|
||||||
try {
|
FileHelper.stripFileProtocol(this.croppedUri.toString()) :
|
||||||
if(allowEdit && croppedUri != null)
|
FileHelper.stripFileProtocol(this.imageUri.toString());
|
||||||
{
|
|
||||||
sourcePath = FileHelper.stripFileProtocol(croppedUri.toString());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
sourcePath = getTempDirectoryPath() + "/.Pic.jpg";
|
|
||||||
}
|
|
||||||
|
|
||||||
//We don't support PNG, so let's not pretend we do
|
if (this.encodingType == JPEG) {
|
||||||
exif.createInFile(sourcePath);
|
try {
|
||||||
exif.readExifData();
|
//We don't support PNG, so let's not pretend we do
|
||||||
rotate = exif.getOrientation();
|
exif.createInFile(sourcePath);
|
||||||
|
exif.readExifData();
|
||||||
|
rotate = exif.getOrientation();
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Bitmap bitmap = null;
|
Bitmap bitmap = null;
|
||||||
@ -392,13 +403,8 @@ 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) {
|
||||||
if(croppedUri != null) {
|
bitmap = getScaledBitmap(sourcePath);
|
||||||
bitmap = getScaledBitmap(FileHelper.stripFileProtocol(croppedUri.toString()));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
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");
|
||||||
@ -415,24 +421,17 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
bitmap = getRotatedBitmap(rotate, bitmap, exif);
|
bitmap = getRotatedBitmap(rotate, bitmap, exif);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.processPicture(bitmap);
|
this.processPicture(bitmap, this.encodingType);
|
||||||
checkForDuplicateImage(DATA_URL);
|
checkForDuplicateImage(DATA_URL);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If sending filename back
|
// If sending filename back
|
||||||
else if (destType == FILE_URI || destType == NATIVE_URI) {
|
else if (destType == FILE_URI || destType == NATIVE_URI) {
|
||||||
uri = Uri.fromFile(new File(getTempDirectoryPath(), System.currentTimeMillis() + ".jpg"));
|
|
||||||
|
|
||||||
if (this.saveToPhotoAlbum) {
|
if (this.saveToPhotoAlbum) {
|
||||||
//Create a URI on the filesystem so that we can write the file.
|
//Create a URI on the filesystem so that we can write the file.
|
||||||
uri = Uri.fromFile(new File(getPicutresPath()));
|
uri = Uri.fromFile(new File(getPicutresPath()));
|
||||||
} else {
|
} else {
|
||||||
uri = Uri.fromFile(new File(getTempDirectoryPath(), System.currentTimeMillis() + ".jpg"));
|
uri = Uri.fromFile(createCaptureFile(this.encodingType, System.currentTimeMillis() + ""));
|
||||||
}
|
|
||||||
|
|
||||||
if (uri == null) {
|
|
||||||
this.failPicture("Error capturing image - no media storage found.");
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If all this is true we shouldn't compress the image.
|
// If all this is true we shouldn't compress the image.
|
||||||
@ -442,12 +441,13 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
|
|
||||||
this.callbackContext.success(uri.toString());
|
this.callbackContext.success(uri.toString());
|
||||||
} else {
|
} else {
|
||||||
if(croppedUri != null) {
|
bitmap = getScaledBitmap(sourcePath);
|
||||||
bitmap = getScaledBitmap(FileHelper.stripFileProtocol(croppedUri.toString()));
|
|
||||||
}
|
// Double-check the bitmap.
|
||||||
else
|
if (bitmap == null) {
|
||||||
{
|
Log.d(LOG_TAG, "I either have a null image path or bitmap");
|
||||||
bitmap = getScaledBitmap(FileHelper.stripFileProtocol(imageUri.toString()));
|
this.failPicture("Unable to create bitmap!");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (rotate != 0 && this.correctOrientation) {
|
if (rotate != 0 && this.correctOrientation) {
|
||||||
@ -456,7 +456,11 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
|
|
||||||
// Add compressed version of captured image to returned media store Uri
|
// Add compressed version of captured image to returned media store Uri
|
||||||
OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri);
|
OutputStream os = this.cordova.getActivity().getContentResolver().openOutputStream(uri);
|
||||||
bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
|
CompressFormat compressFormat = encodingType == JPEG ?
|
||||||
|
CompressFormat.JPEG :
|
||||||
|
CompressFormat.PNG;
|
||||||
|
|
||||||
|
bitmap.compress(compressFormat, this.mQuality, os);
|
||||||
os.close();
|
os.close();
|
||||||
|
|
||||||
// Restore exif data to file
|
// Restore exif data to file
|
||||||
@ -488,7 +492,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
|
|||||||
private String getPicutresPath()
|
private String getPicutresPath()
|
||||||
{
|
{
|
||||||
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
|
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
|
||||||
String imageFileName = "IMG_" + timeStamp + ".jpg";
|
String imageFileName = "IMG_" + timeStamp + (this.encodingType == JPEG ? ".jpg" : ".png");
|
||||||
File storageDir = Environment.getExternalStoragePublicDirectory(
|
File storageDir = Environment.getExternalStoragePublicDirectory(
|
||||||
Environment.DIRECTORY_PICTURES);
|
Environment.DIRECTORY_PICTURES);
|
||||||
String galleryPath = storageDir.getAbsolutePath() + "/" + imageFileName;
|
String galleryPath = storageDir.getAbsolutePath() + "/" + imageFileName;
|
||||||
@ -505,10 +509,15 @@ private void refreshGallery(Uri contentUri)
|
|||||||
|
|
||||||
private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
|
private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
|
||||||
// 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 modifiedPath = getTempDirectoryPath() + "/modified.jpg";
|
String modifiedPath = getTempDirectoryPath() + "/modified." +
|
||||||
|
(this.encodingType == JPEG ? "jpg" : "png");
|
||||||
|
|
||||||
OutputStream os = new FileOutputStream(modifiedPath);
|
OutputStream os = new FileOutputStream(modifiedPath);
|
||||||
bitmap.compress(Bitmap.CompressFormat.JPEG, this.mQuality, os);
|
CompressFormat compressFormat = this.encodingType == JPEG ?
|
||||||
|
CompressFormat.JPEG :
|
||||||
|
CompressFormat.PNG;
|
||||||
|
|
||||||
|
bitmap.compress(compressFormat, this.mQuality, os);
|
||||||
os.close();
|
os.close();
|
||||||
|
|
||||||
// Some content: URIs do not map to file paths (e.g. picasa).
|
// Some content: URIs do not map to file paths (e.g. picasa).
|
||||||
@ -597,7 +606,7 @@ private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
|
|||||||
|
|
||||||
// If sending base64 image back
|
// If sending base64 image back
|
||||||
if (destType == DATA_URL) {
|
if (destType == DATA_URL) {
|
||||||
this.processPicture(bitmap);
|
this.processPicture(bitmap, this.encodingType);
|
||||||
}
|
}
|
||||||
|
|
||||||
// If sending filename back
|
// If sending filename back
|
||||||
@ -673,7 +682,7 @@ private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
|
|||||||
try {
|
try {
|
||||||
if(this.allowEdit)
|
if(this.allowEdit)
|
||||||
{
|
{
|
||||||
Uri tmpFile = Uri.fromFile(new File(getTempDirectoryPath(), ".Pic.jpg"));
|
Uri tmpFile = Uri.fromFile(createCaptureFile(this.encodingType));
|
||||||
performCrop(tmpFile, destType, intent);
|
performCrop(tmpFile, destType, intent);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -1046,10 +1055,14 @@ private String ouputModifiedBitmap(Bitmap bitmap, Uri uri) throws IOException {
|
|||||||
*
|
*
|
||||||
* @param bitmap
|
* @param bitmap
|
||||||
*/
|
*/
|
||||||
public void processPicture(Bitmap bitmap) {
|
public void processPicture(Bitmap bitmap, int encodingType) {
|
||||||
ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream();
|
ByteArrayOutputStream jpeg_data = new ByteArrayOutputStream();
|
||||||
|
CompressFormat compressFormat = encodingType == JPEG ?
|
||||||
|
CompressFormat.JPEG :
|
||||||
|
CompressFormat.PNG;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (bitmap.compress(CompressFormat.JPEG, mQuality, jpeg_data)) {
|
if (bitmap.compress(compressFormat, mQuality, jpeg_data)) {
|
||||||
byte[] code = jpeg_data.toByteArray();
|
byte[] code = jpeg_data.toByteArray();
|
||||||
byte[] output = Base64.encode(code, Base64.NO_WRAP);
|
byte[] output = Base64.encode(code, Base64.NO_WRAP);
|
||||||
String js_out = new String(output);
|
String js_out = new String(output);
|
||||||
|
Loading…
Reference in New Issue
Block a user