add extractThumbnail takePhoto

This commit is contained in:
dmcBig 2017-11-15 17:35:05 +08:00 committed by GitHub
parent f4c94fc454
commit 0add94047a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,6 @@
package com.dmc.mediaPicker; package com.dmc.mediaPickerPlugin;
import android.app.ProgressDialog;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
@ -29,14 +30,19 @@ import java.util.ArrayList;
* This class echoes a string called from JavaScript. * This class echoes a string called from JavaScript.
*/ */
public class MediaPicker extends CordovaPlugin { public class MediaPicker extends CordovaPlugin {
private CallbackContext callback; private CallbackContext callback;
private boolean showBase64=false; private boolean showBase64=false;
private boolean showThumbnail=false; private boolean showThumbnail=false;
private boolean showProgressDialog=true;
private int quality=50;
private int thumbnailW=120;
private int thumbnailH=120;
private ProgressDialog dialog;
private String progressDialogStr="";
@Override @Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
getPublicArgs(args);
if (action.equals("getMedias")) { if (action.equals("getMedias")) {
this.getMedias(args, callbackContext); this.getMedias(args, callbackContext);
return true; return true;
@ -46,6 +52,9 @@ public class MediaPicker extends CordovaPlugin {
}else if(action.equals("photoLibrary")){ }else if(action.equals("photoLibrary")){
this.getMedias(args, callbackContext); this.getMedias(args, callbackContext);
return true; return true;
}else if(action.equals("extractThumbnail")){
this.extractThumbnail(args, callbackContext);
return true;
} }
return false; return false;
} }
@ -67,16 +76,6 @@ public class MediaPicker extends CordovaPlugin {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
try {
showBase64=jsonObject.getBoolean("showBase64");
} catch (Exception e) {
e.printStackTrace();
}
try {
showThumbnail=jsonObject.getBoolean("showThumbnail");
} catch (Exception e) {
e.printStackTrace();
}
try { try {
intent.putExtra(PickerConfig.SELECT_MODE,jsonObject.getInt("selectMode"));//default image and video (Optional) intent.putExtra(PickerConfig.SELECT_MODE,jsonObject.getInt("selectMode"));//default image and video (Optional)
} catch (Exception e) { } catch (Exception e) {
@ -106,47 +105,134 @@ public class MediaPicker extends CordovaPlugin {
this.cordova.startActivityForResult(this,intent,200); this.cordova.startActivityForResult(this,intent,200);
} }
public void getPublicArgs(JSONArray args){
JSONObject jsonObject=new JSONObject();
if (args != null && args.length() > 0) {
try {
jsonObject = args.getJSONObject(0);
} catch (Exception e) {
e.printStackTrace();
}
try {
showBase64 = jsonObject.getBoolean("showBase64");
} catch (Exception e) {
e.printStackTrace();
}
try {
showThumbnail = jsonObject.getBoolean("showThumbnail");
} catch (Exception e) {
e.printStackTrace();
}
try {
progressDialogStr = jsonObject.getString("progressDialogStr");
} catch (Exception e) {
e.printStackTrace();
}
try {
quality = jsonObject.getInt("thumbnailQuality");
} catch (Exception e) {
e.printStackTrace();
}
try {
thumbnailW = jsonObject.getInt("thumbnailW");
} catch (Exception e) {
e.printStackTrace();
}
try {
thumbnailH = jsonObject.getInt("thumbnailH");
} catch (Exception e) {
e.printStackTrace();
}
try {
showProgressDialog = jsonObject.getBoolean("showProgressDialog");
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Override @Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) { public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent); super.onActivityResult(requestCode, resultCode, intent);
try { try {
if(requestCode==200&&resultCode==PickerConfig.RESULT_CODE){ if(requestCode==200&&resultCode==PickerConfig.RESULT_CODE){
ArrayList<Media> select=intent.getParcelableArrayListExtra(PickerConfig.EXTRA_RESULT); final ArrayList<Media> select=intent.getParcelableArrayListExtra(PickerConfig.EXTRA_RESULT);
JSONArray jsonArray=new JSONArray(); final JSONArray jsonArray=new JSONArray();
for(Media media:select){ if(showProgressDialog) {
String path=media.path; dialog = ProgressDialog.show(cordova.getActivity(), "", progressDialogStr, false, true);
JSONObject object=new JSONObject();
if(showThumbnail){
object.put("thumbnailBase64",thumbToBase64(path,media.mediaType));
}
object.put("path",path);
object.put("size",media.size);
object.put("uri",Uri.parse(path));
object.put("exifRotate",getBitmapRotate(path));
object.put("mediaType",media.mediaType==3?"video":"image");
jsonArray.put(object);
} }
this.callback.success(jsonArray); cordova.getThreadPool().execute(new Runnable() {
public void run() {
try {
for(Media media:select){
String path=media.path;
JSONObject object=new JSONObject();
if(showThumbnail){
object.put("thumbnailBase64",extractThumbnail(path,media.mediaType));
}
object.put("path",path);
object.put("size",media.size);
object.put("uri",Uri.parse(path));
object.put("exifRotate",getBitmapRotate(path));
object.put("name",media.name);
object.put("mediaType",media.mediaType==3?"video":"image");
jsonArray.put(object);
}
MediaPicker.this.callback.success(jsonArray);
if(showProgressDialog) {
dialog.dismiss();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public String thumbToBase64(String path,int mediaType) { public void extractThumbnail(JSONArray args, CallbackContext callbackContext){
JSONObject jsonObject=new JSONObject();
if (args != null && args.length() > 0) {
try {
jsonObject = args.getJSONObject(0);
} catch (Exception e) {
e.printStackTrace();
}
String uri= null;
int mediatype= 1;
try {
uri = jsonObject.getString("uri");
mediatype = "video".equals(jsonObject.getString("mediaType"))?3:1;
} catch (JSONException e) {
e.printStackTrace();
}
callbackContext.success(extractThumbnail(uri,mediatype));
}
}
public String extractThumbnail(String path,int mediaType) {
String encodedImage = null;
try {
Bitmap thumbImage; Bitmap thumbImage;
if (mediaType == 3) { if (mediaType == 3) {
thumbImage = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MICRO_KIND); thumbImage = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MICRO_KIND);
} else { } else {
thumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(path), 150, 150); thumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(path), thumbnailW, thumbnailH);
} }
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
thumbImage.compress(Bitmap.CompressFormat.JPEG, 100, baos); thumbImage.compress(Bitmap.CompressFormat.JPEG, quality, baos);
byte[] imageBytes = baos.toByteArray(); byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT); encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage; baos.close();
} catch (IOException e) {
e.printStackTrace();
}
return encodedImage;
} }
public static String fileToBase64(String path,int mediaType) { public static String fileToBase64(String path,int mediaType) {