mirror of
https://github.com/DmcSDK/cordova-plugin-mediaPicker
synced 2026-05-22 00:01:31 +08:00
android support compress image
android support compress image
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
<config-file parent="/*" target="AndroidManifest.xml"/>
|
||||
<config-file parent="/manifest" target="AndroidManifest.xml">
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18"/>
|
||||
</config-file>
|
||||
<framework src="com.dmcBig:mediapicker:+"/>
|
||||
<source-file src="src/android/MediaPicker.java" target-dir="src/com/dmc/mediaPickerPlugin"/>
|
||||
|
||||
+114
-21
@@ -1,6 +1,7 @@
|
||||
package com.dmc.mediaPickerPlugin;
|
||||
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
@@ -23,7 +24,9 @@ import org.json.JSONObject;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
@@ -33,7 +36,8 @@ import java.util.ArrayList;
|
||||
*/
|
||||
public class MediaPicker extends CordovaPlugin {
|
||||
private CallbackContext callback;
|
||||
private int quality=50;
|
||||
private int thumbnailQuality=50;
|
||||
private int quality=100;//default original
|
||||
private int thumbnailW=200;
|
||||
private int thumbnailH=200;
|
||||
@Override
|
||||
@@ -52,6 +56,12 @@ public class MediaPicker extends CordovaPlugin {
|
||||
}else if(action.equals("extractThumbnail")){
|
||||
this.extractThumbnail(args, callbackContext);
|
||||
return true;
|
||||
}else if(action.equals("compressImage")){
|
||||
this.compressImage(args, callbackContext);
|
||||
return true;
|
||||
}else if(action.equals("fileToBlob")){
|
||||
this.fileToBlob(args.getString(0), callbackContext);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -111,7 +121,7 @@ public class MediaPicker extends CordovaPlugin {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
quality = jsonObject.getInt("thumbnailQuality");
|
||||
thumbnailQuality = jsonObject.getInt("thumbnailQuality");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -125,6 +135,11 @@ public class MediaPicker extends CordovaPlugin {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
quality = jsonObject.getInt("quality");
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -142,11 +157,13 @@ public class MediaPicker extends CordovaPlugin {
|
||||
try {
|
||||
int index=0;
|
||||
for(Media media:select){
|
||||
String path=media.path;
|
||||
if(quality<100){
|
||||
media=compressImage(media);
|
||||
}
|
||||
JSONObject object=new JSONObject();
|
||||
object.put("path",path);
|
||||
object.put("path",media.path);
|
||||
object.put("uri",Uri.parse(media.path));
|
||||
object.put("size",media.size);
|
||||
object.put("uri",Uri.parse(path));
|
||||
object.put("name",media.name);
|
||||
object.put("index",index);
|
||||
object.put("mediaType",media.mediaType==3?"video":"image");
|
||||
@@ -174,7 +191,7 @@ public class MediaPicker extends CordovaPlugin {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try {
|
||||
quality = jsonObject.getInt("thumbnailQuality");
|
||||
thumbnailQuality = jsonObject.getInt("thumbnailQuality");
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@@ -182,14 +199,12 @@ public class MediaPicker extends CordovaPlugin {
|
||||
String path =jsonObject.getString("path");
|
||||
jsonObject.put("exifRotate",getBitmapRotate(path));
|
||||
int mediatype = "video".equals(jsonObject.getString("mediaType"))?3:1;
|
||||
String thumbnailBase64=extractThumbnail(path,mediatype,quality);
|
||||
jsonObject.put("thumbnailBase64",thumbnailBase64);
|
||||
jsonObject.put("thumbnailBase64",extractThumbnail(path,mediatype,thumbnailQuality));
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
callbackContext.success(jsonObject);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public String extractThumbnail(String path,int mediaType,int quality) {
|
||||
@@ -212,22 +227,54 @@ public class MediaPicker extends CordovaPlugin {
|
||||
return encodedImage;
|
||||
}
|
||||
|
||||
public static String fileToBase64(String path,int mediaType) {
|
||||
byte[] data = null;
|
||||
try {
|
||||
BufferedInputStream in = new BufferedInputStream(new FileInputStream(path));
|
||||
data = new byte[in.available()];
|
||||
in.read(data);
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return Base64.encodeToString(data, Base64.DEFAULT);
|
||||
|
||||
public Media compressImage(Media media){
|
||||
File file=compressImage(media.path,quality);
|
||||
media.path=file.getPath();
|
||||
media.name=file.getName();
|
||||
media.size=file.length();
|
||||
return media;
|
||||
}
|
||||
|
||||
public void compressImage( JSONArray args, CallbackContext callbackContext){
|
||||
this.callback=callbackContext;
|
||||
try {
|
||||
JSONObject jsonObject = args.getJSONObject(0);
|
||||
String path = jsonObject.getString("path");
|
||||
int quality=jsonObject.getInt("quality");
|
||||
if(quality<100) {
|
||||
File file = compressImage(path, quality);
|
||||
jsonObject.put("path", file.getPath());
|
||||
jsonObject.put("size", file.length());
|
||||
jsonObject.put("name", file.getName());
|
||||
callbackContext.success(jsonObject);
|
||||
}else{
|
||||
callbackContext.success(jsonObject);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
callbackContext.error("compressImage error"+e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public File compressImage(String path,int quality){
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
String compFileName="dmcMediaPickerCompress"+System.currentTimeMillis()+".jpg";
|
||||
File file= new File(cordova.getActivity().getExternalCacheDir(),compFileName);
|
||||
BitmapFactory.decodeFile(path).compress(Bitmap.CompressFormat.JPEG, quality, baos);
|
||||
try {
|
||||
FileOutputStream fos = new FileOutputStream(file);
|
||||
fos.write(baos.toByteArray());
|
||||
fos.flush();
|
||||
fos.close();
|
||||
} catch (Exception e) {
|
||||
MediaPicker.this.callback.error("compressImage error"+e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
public static int getBitmapRotate(String path) {
|
||||
public int getBitmapRotate(String path) {
|
||||
int degree = 0;
|
||||
try {
|
||||
ExifInterface exifInterface = new ExifInterface(path);
|
||||
@@ -249,4 +296,50 @@ public class MediaPicker extends CordovaPlugin {
|
||||
return degree;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public byte[] extractThumbnailByte(String path,int mediaType,int quality) {
|
||||
|
||||
try {
|
||||
Bitmap thumbImage;
|
||||
if (mediaType == 3) {
|
||||
thumbImage = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MINI_KIND);
|
||||
} else {
|
||||
thumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(path), thumbnailW, thumbnailH);
|
||||
}
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
thumbImage.compress(Bitmap.CompressFormat.JPEG, quality, baos);
|
||||
return baos.toByteArray();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String fileToBase64(String path) {
|
||||
byte[] data = null;
|
||||
try {
|
||||
BufferedInputStream in = new BufferedInputStream(new FileInputStream(path));
|
||||
data = new byte[in.available()];
|
||||
in.read(data);
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return Base64.encodeToString(data, Base64.DEFAULT);
|
||||
}
|
||||
|
||||
public void fileToBlob(String path, CallbackContext callbackContext) {
|
||||
byte[] data = null;
|
||||
try {
|
||||
BufferedInputStream in = new BufferedInputStream(new FileInputStream(path));
|
||||
data = new byte[in.available()];
|
||||
in.read(data);
|
||||
in.close();
|
||||
} catch (IOException e) {
|
||||
callbackContext.error("fileToBlob "+e);
|
||||
e.printStackTrace();
|
||||
}
|
||||
callbackContext.success(data);
|
||||
}
|
||||
}
|
||||
|
||||
+20
-11
@@ -1,22 +1,31 @@
|
||||
cordova.define("cordova-plugin-mediapicker-dmcbig.MediaPicker", function(require, exports, module) {
|
||||
var exec = require('cordova/exec');
|
||||
|
||||
|
||||
var MediaPicker = {
|
||||
getMedias:function(arg0, success, error) {
|
||||
exec(success, error, "MediaPicker", "getMedias", [arg0]);
|
||||
},
|
||||
photoLibrary:function(arg0, success, error) {
|
||||
exec(success, error, "MediaPicker", "photoLibrary", [arg0]);
|
||||
},
|
||||
takePhoto:function(arg0, success, error) {
|
||||
exec(success, error, "MediaPicker", "takePhoto", [arg0]);
|
||||
},
|
||||
extractThumbnail:function(arg0, success, error) {
|
||||
exec(success, error, "MediaPicker", "extractThumbnail", [arg0]);
|
||||
},
|
||||
exec(success, error, "MediaPicker", "getMedias", [arg0]);
|
||||
},
|
||||
photoLibrary:function(arg0, success, error) {
|
||||
exec(success, error, "MediaPicker", "photoLibrary", [arg0]);
|
||||
},
|
||||
takePhoto:function(arg0, success, error) {
|
||||
exec(success, error, "MediaPicker", "takePhoto", [arg0]);
|
||||
},
|
||||
extractThumbnail:function(arg0, success, error) {
|
||||
exec(success, error, "MediaPicker", "extractThumbnail", [arg0]);
|
||||
},
|
||||
compressEvent:function(s,i) {
|
||||
cordova.fireDocumentEvent('MediaPicker.CompressVideoEvent', {'status':s,'index':i});
|
||||
},
|
||||
compressImage:function(arg0, success, error) {
|
||||
exec(success, error, "MediaPicker", "compressImage", [arg0]);
|
||||
},
|
||||
fileToBlob:function(arg0, success, error) {
|
||||
exec(success, error, "MediaPicker", "fileToBlob", [arg0]);
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = MediaPicker;
|
||||
|
||||
});
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
<!DOCTYPE html>
|
||||
<!--
|
||||
Licensed to the Apache Software Foundation (ASF) under one
|
||||
or more contributor license agreements. See the NOTICE file
|
||||
distributed with this work for additional information
|
||||
regarding copyright ownership. The ASF licenses this file
|
||||
to you under the Apache License, Version 2.0 (the
|
||||
"License"); you may not use this file except in compliance
|
||||
with the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing,
|
||||
software distributed under the License is distributed on an
|
||||
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
KIND, either express or implied. See the License for the
|
||||
specific language governing permissions and limitations
|
||||
under the License.
|
||||
-->
|
||||
<html>
|
||||
<head>
|
||||
<!--
|
||||
Customize this policy to fit your own app's needs. For more guidance, see:
|
||||
https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy
|
||||
Some notes:
|
||||
* gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
|
||||
* https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
|
||||
* Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
|
||||
* Enable inline JS: add 'unsafe-inline' to default-src
|
||||
-->
|
||||
<meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *; img-src 'self' data: content:;">
|
||||
<meta name="format-detection" content="telephone=no">
|
||||
<meta name="msapplication-tap-highlight" content="no">
|
||||
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
|
||||
<link rel="stylesheet" type="text/css" href="css/index.css">
|
||||
<title>Hello World</title>
|
||||
</head>
|
||||
<body>
|
||||
<div>
|
||||
<img name="imgView" width="100px" height="100px" >
|
||||
<img name="imgView" width="100px" height="100px" >
|
||||
<img name="imgView" width="100px" height="100px" >
|
||||
<img name="imgView" width="100px" height="100px" >
|
||||
<img name="imgView" width="100px" height="100px" >
|
||||
<img name="imgView" width="100px" height="100px" >
|
||||
<img name="imgView" width="100px" height="100px" >
|
||||
<img name="imgView" width="100px" height="100px" >
|
||||
</div>
|
||||
<button id="openBtn">open</button>
|
||||
<button id="uploadBtn">upload</button>
|
||||
<script type="text/javascript" src="cordova.js"></script>
|
||||
<script type="text/javascript" src="index.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,52 @@
|
||||
var imgs = document.getElementsByName('imgView');
|
||||
var args = {
|
||||
'selectMode': 101, //101=picker image and video , 100=image , 102=video
|
||||
'maxSelectCount': 40, //default 40 (Optional)
|
||||
'maxSelectSize': 188743680, //188743680=180M (Optional)
|
||||
};
|
||||
|
||||
document.getElementById('openBtn').onclick = function() {
|
||||
MediaPicker.getMedias(args, function(medias) {
|
||||
//medias [{mediaType: "image", path:'/storage/emulated/0/DCIM/Camera/20170808_145202.jpg', size: 21993}]
|
||||
window.medias = medias;
|
||||
getThumbnail(medias);
|
||||
}, function(e) { console.log(e) })
|
||||
};
|
||||
|
||||
function getThumbnail(medias) {
|
||||
for (var i = 0; i < medias.length; i++) {
|
||||
//medias[i].thumbnailQuality=50; (Optional)
|
||||
//loading(); //show loading ui
|
||||
MediaPicker.extractThumbnail(medias[i], function(data) {
|
||||
imgs[data.index].src = 'data:image/jpeg;base64,' + data.thumbnailBase64;
|
||||
imgs[data.index].setAttribute('style', 'transform:rotate(' + data.exifRotate + 'deg)');
|
||||
}, function(e) { console.log(e) });
|
||||
}
|
||||
}
|
||||
|
||||
document.getElementById('uploadBtn').onclick = function() {
|
||||
//please: cordova plugin add cordova-plugin-file-transfer
|
||||
//see: https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-file-transfer/index.html
|
||||
//use medias[index].path
|
||||
|
||||
//OR
|
||||
//compressImage(); //upload compress img
|
||||
};
|
||||
|
||||
function compressImage() {
|
||||
for (var i = 0; i < medias.length; i++) {
|
||||
// if(medias[i].size>1048576){ medias[i].quality=50; } else {d ataArray[i].quality=100;}
|
||||
medias[i].quality = 30; //when the value is 100,return original image
|
||||
MediaPicker.compressImage(medias[i], function(compressData) {
|
||||
//user compressData.path upload compress img
|
||||
console.log(compressData.path);
|
||||
}, function(e) { console.log(e) });
|
||||
}
|
||||
}
|
||||
|
||||
function loading() {}
|
||||
|
||||
//ios Video transcoding compression to MP4 (use AVAssetExportPresetMediumQuality)
|
||||
document.addEventListener("MediaPicker.CompressVideoEvent", function(data) {
|
||||
alert(data.status + "||" + data.index);
|
||||
}, false);
|
||||
Reference in New Issue
Block a user