// 裁剪图片 private void startImageAction( Uri uri, int outputX, int outputY, int requestCode, boolean isCrop) { Intent intent = null; if (isCrop) { intent = new Intent("com.android.camera.action.CROP"); } else { intent = new Intent(Intent.ACTION_GET_CONTENT, null); } intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); if (requestCode == SeventhConstants.REQUESTCODE_UPLOADAVATAR_GALLERY_CROP) { // 若是相册传来的数据我们需要对url处理,将裁剪后的url保存到心得地方去 File dir = new File(SeventhConstants.MyAvatarDir); if (!dir.exists()) { dir.mkdirs(); } // 原图 File file = new File(dir, new SimpleDateFormat("yyMMddHHmmss").format(new Date()) + ".jpg"); Uri uri2 = Uri.fromFile(file); filePath = file.getAbsolutePath(); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri2); } else { // 用裁剪之后的相机图片覆盖了原图 intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); } intent.putExtra("return-data", true); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, requestCode); }
/** * 启动Aviary图片编辑器 * * @param uri 图片的URI * @param outputFilePath 输出的图片的路径 */ private void startFeather(Uri uri, String outputFilePath) { // Create the intent needed to start feather Intent newIntent = new Intent(this, FeatherActivity.class); // set the source image uri newIntent.setData(uri); // pass the required api_key and secret newIntent.putExtra("API_KEY", API_KEY); // pass the uri of the destination image file (optional) // This will be the same uri you will receive in the onActivityResult newIntent.putExtra("output", Uri.parse("file://" + outputFilePath)); // 返回时data中保存的uri // format of the destination image (optional) newIntent.putExtra(Constants.EXTRA_OUTPUT_FORMAT, Bitmap.CompressFormat.JPEG.name()); // output format quality (optional) newIntent.putExtra(Constants.EXTRA_OUTPUT_QUALITY, 90); newIntent.putExtra(Constants.EXTRA_TOOLS_DISABLE_VIBRATION, true); final DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); int max_size = Math.min(metrics.widthPixels, metrics.heightPixels); max_size = (int) (max_size / 0.8); newIntent.putExtra("max-image-size", max_size); // Enable/disable the default borders for the effects newIntent.putExtra("effect-enable-borders", true); // The session-id key must be 64 char length mSessionId = StringUtils.getSha256(System.currentTimeMillis() + API_KEY); newIntent.putExtra("output-hires-session-id", mSessionId); startActivityForResult(newIntent, ConstantUtil.REQUEST_AVIARY_FEATURE); }
/** 调用图片剪辑程序 */ public static Intent getCropImageIntent( Uri photoUri, int aspectX, int aspectY, int outputX, int outputY) { Intent intent; intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(photoUri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", aspectX); // 宽高比 intent.putExtra("aspectY", aspectY); intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); cropfile = new File(CameraUtil.CROP_FILE_DIR + File.separator + CameraUtil.getPhotoFileName()); try { if (cropfile.exists()) { cropfile.delete(); } cropfile.getParentFile().mkdirs(); cropfile.createNewFile(); } catch (IOException ex) { Log.e("io", ex.getMessage()); } intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(cropfile)); intent.putExtra("return-data", false); // 若为false则表示不返回数据 intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", false); return intent; }
/** * 获取裁切intent * * @param uri * @param outputX * @param outputY * @param aspectX * @param aspectY * @param outputFormat * @return */ public static Intent getCropIntent( Intent intent, Uri uri, int outputX, int outputY, float aspectX, float aspectY, String outputFormat) { if (intent == null) intent = new Intent(); intent.setAction("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); // intent.putExtra( "circleCrop", "of course" ); // circleCrop不为空则进行圆形裁切 intent.putExtra("aspectX", (int) (aspectX + .5)); intent.putExtra("aspectY", (int) (aspectY + .5)); intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); // 黑边 intent.putExtra("scaleUpIfNeeded", true); // 黑边 if (uri != null) { intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); intent.putExtra("return-data", false); // intent.putExtra( "return-data", true ); } else intent.putExtra("return-data", true); if (TextUtils.isEmpty(outputFormat)) intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); else intent.putExtra("outputFormat", outputFormat); intent.putExtra("noFaceDetection", true); return intent; }
/** 获取本地图片,并剪切 */ public void getFromLocal() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); intent.setType("image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 200); intent.putExtra("outputY", 200); intent.putExtra("scale", true); intent.putExtra("return-data", true); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection baseActivity.startActivityForResult(intent, PhotoUpload.PHOTO_ZOOM); }
/** 从相册截小图 */ public void cropSmallImageFromGallery() { Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); intent.setType("image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 200); intent.putExtra("outputY", 200); intent.putExtra("scale", true); intent.putExtra("return-data", true); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, CHOOSE_SMALL_PICTURE); }
/** * 图片剪切功能,需要在活动中监听forresult方法 * * @param paramUri 图片所在资源路径 */ public void startPhotoZoom(Uri paramUri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(paramUri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", 200); intent.putExtra("outputY", 200); intent.putExtra("scale", true); intent.putExtra("return-data", true); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection baseActivity.startActivityForResult(intent, PHOTO_ZOOM); }
private void cropPhoto(Uri data) { Intent intent = new Intent(); intent.setAction("com.android.camera.action.CROP"); intent.setDataAndType(data, "image/*"); // mUri是已经选择的图片Uri intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); // 裁剪框比例 intent.putExtra("aspectY", 1); intent.putExtra("outputX", 150); // 输出图片大小 intent.putExtra("outputY", 150); intent.putExtra("return-data", true); intent.putExtra("scale", true); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection] startActivityForResult(intent, Constants.ACTION_IMAGE_CROP); }
private Intent startCameraZoomIntent(Uri uri, int size) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", size); intent.putExtra("outputY", size); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); intent.putExtra("return-data", true); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection return intent; }
// //截图 private void cropImageUri(Uri uri, int outputX, int outputY, int requestCode) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); intent.putExtra("return-data", false); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, requestCode); }
public class CropParams { public static final String CROP_TYPE = "image/*"; public static final String OUTPUT_FORMAT = Bitmap.CompressFormat.JPEG.toString(); public static final int DEFAULT_ASPECT_W = 720; // 选择框比例 public static final int DEFAULT_ASPECT_H = 400; public static final int DEFAULT_OUTPUT_W = 720; // 输出图片大小 public static final int DEFAULT_OUTPUT_H = 400; public Uri uri; public String type; public String outputFormat; public String crop; public boolean scale; public boolean returnData; public boolean noFaceDetection; public boolean scaleUpIfNeeded; public int aspectX; public int aspectY; public static int outputX; public static int outputY; public boolean isCrop = true; public CropParams() { uri = CropHelper.buildUri(); type = CROP_TYPE; outputFormat = OUTPUT_FORMAT; crop = "true"; scale = true; returnData = false; noFaceDetection = true; scaleUpIfNeeded = true; aspectX = DEFAULT_ASPECT_W; aspectY = DEFAULT_ASPECT_H; outputX = DEFAULT_OUTPUT_W; outputY = DEFAULT_OUTPUT_H; } }
/** * 创建调用系统图片裁剪 * * @return intent */ public static Intent startPhotoZoomIntent(Uri uri, Uri toUri, int size) { // Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // intent.setType("image/*"); // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪 intent.putExtra("crop", "true"); // aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY 是裁剪图片宽高 intent.putExtra("outputX", size); intent.putExtra("outputY", size); intent.putExtra("scale", true); intent.putExtra("return-data", false); intent.putExtra(MediaStore.EXTRA_OUTPUT, toUri); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); return intent; }
/** * 通用的剪切方法 * * @param uri * @param outputX * @param outputY * @param requestCode */ private void cropImageUri(Uri uri, int outputX, int outputY, int requestCode) { File cacheDir = getCacheDir(); if (!cacheDir.exists()) { cacheDir.mkdirs(); } File file = new File(cacheDir, "take_big.png"); Uri imageUri = Uri.parse("file:///" + file.getAbsolutePath()); Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", 2); intent.putExtra("aspectY", 1); intent.putExtra("outputX", outputX); intent.putExtra("outputY", outputY); intent.putExtra("scale", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); intent.putExtra("return-data", false); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, requestCode); }
public class CropConfig { public static final String CROP_TYPE = "image/*"; public static final String OUTPUT_FORMAT = Bitmap.CompressFormat.JPEG.toString(); public static final int DEFAULT_ASPECT = 1; public static final int DEFAULT_OUTPUT = 300; public Uri uri; public String type; public String outputFormat; public String crop; public boolean scale; public boolean returnData; public boolean noFaceDetection; public boolean scaleUpIfNeeded; public int aspectX; public int aspectY; public int outputX; public int outputY; public CropConfig() { uri = CropUtils.buildUri(); type = CROP_TYPE; outputFormat = OUTPUT_FORMAT; crop = "true"; scale = true; returnData = false; noFaceDetection = true; scaleUpIfNeeded = true; aspectX = DEFAULT_ASPECT; aspectY = DEFAULT_ASPECT; outputX = DEFAULT_OUTPUT; outputY = DEFAULT_OUTPUT; } }
/** * 打开系统自带裁减方法 * * @param url */ private void openCropView(String url, Boolean isCamera) { // 获取屏幕宽度 WindowManager wm = (WindowManager) getApplicationContext().getSystemService(Context.WINDOW_SERVICE); int width = wm.getDefaultDisplay().getWidth(); Intent intent = new Intent("com.android.camera.action.CROP"); if (isCamera) { // 相机图片不用另外保存 File vFile = new File(url); ChooseUri = Uri.fromFile(vFile); } else { String tempUrl = Environment.getExternalStorageDirectory() + "/flashtag/" + System.currentTimeMillis() + ".jpg"; File vFile = new File(tempUrl); if (!vFile.exists()) { File vDirPath = vFile.getParentFile(); // new File(vFile.getParent()); vDirPath.mkdirs(); } if (copyFile(url, tempUrl)) { ChooseUri = Uri.fromFile(vFile); } } intent.setDataAndType(ChooseUri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", aspectX); // 裁剪框比例 intent.putExtra("aspectY", aspectY); intent.putExtra("outputX", outputX); // 输出图片大小 intent.putExtra("outputY", outputY); Log.v("dd", Integer.toString(width)); intent.putExtra("scale", true); intent.putExtra("return-data", false); intent.putExtra(MediaStore.EXTRA_OUTPUT, ChooseUri); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, PIC_CROP_BACK); }
protected void showActionList() { final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>(); // get list camera action Intent intentCapture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); List<ResolveInfo> listCapture = getPackageManager().queryIntentActivities(intentCapture, 0); if (listCapture.size() > 1) { for (ResolveInfo res : listCapture) { final CropOption co = new CropOption(); co.title = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo); co.icon = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo); co.appIntent = new Intent(intentCapture); co.requestCode = RequestCode.REQUEST_CAPTURE; co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); cropOptions.add(co); } } else if (listCapture.size() == 1) { final ResolveInfo res = listCapture.get(0); final CropOption co = new CropOption(); co.title = getString(R.string.common_camera); co.icon = getResources().getDrawable(R.drawable.img_btn_bottom_camera); co.appIntent = new Intent(intentCapture); co.requestCode = RequestCode.REQUEST_CAPTURE; co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); cropOptions.add(co); } // get list gallery action Intent intentGallery = new Intent(Intent.ACTION_GET_CONTENT); intentGallery .setType("image/*") // .putExtra("crop", "true") Using Aviary to edit photo .putExtra("scale", false) .putExtra("scaleUpIfNeeded", true) .putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); List<ResolveInfo> listGallery = getPackageManager().queryIntentActivities(intentGallery, PackageManager.MATCH_DEFAULT_ONLY); if (listGallery.size() > 0) { final ResolveInfo res = listGallery.get(0); final CropOption co = new CropOption(); co.title = getString(R.string.common_gallery); co.icon = getResources().getDrawable(R.drawable.img_btn_bottom_gallery); co.appIntent = new Intent(intentGallery); co.requestCode = RequestCode.REQUEST_GALLERY; co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name)); cropOptions.add(co); } CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions); // build dialog to select action final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle(R.string.dialog_choose_action); builder.setAdapter( adapter, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int item) { try { final CropOption crop = cropOptions.get(item); isLoadGalleryOrCamera = true; generationImageTemp(); if (crop.requestCode == RequestCode.REQUEST_GALLERY) { crop.appIntent.putExtra(MediaStore.EXTRA_OUTPUT, mNewImagePath); } else { ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.TITLE, mNewImageName); // store content mNewImageUriCamera = getContentResolver() .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); // set the image file name values crop.appIntent.putExtra(MediaStore.EXTRA_OUTPUT, mNewImageUriCamera); } startActivityForResult(crop.appIntent, crop.requestCode); } catch (Exception ex) { // ko tao duoc file t???m th?? show toast th??ng b??o ng?????i d??ng showCenterToast(R.string.msg_err_camera_not_sdcard); } } }); builder.setOnCancelListener( new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { finish(); } }); AlertDialog alert = builder.create(); alert.show(); }
@Override public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) throws JSONException { mFolderName = args.isNull(10) ? mFolderName : args.getString(10); mGalleryFolder = createFolders(); if (action.equals("show")) { try { Log.i(LOG_TAG, action); this.callbackContext = callbackContext; // make sure an image URI has been provided if (args.isNull(0)) { callbackContext.error("Cannot start aviary, an image URI is required."); return true; } // parameters String source = args.getString(0); // 0 - image URI String outputFormat = args.isNull(1) ? Bitmap.CompressFormat.JPEG.name() : args.getString(1); // 1 - EXTRA_OUTPUT_FORMAT int quality = args.isNull(2) ? 95 : args.getInt(2); // 2 - EXTRA_OUTPUT_QUALITY String[] toolList = new String[] {}; // 3 - EXTRA_TOOLS_LIST if (!args.isNull(3)) { JSONArray toolArray = args.getJSONArray(3); toolList = new String[toolArray.length()]; for (int i = 0; i < toolArray.length(); i++) { toolList[i] = toolArray.getString(i); } } Boolean hideExitUnsaveConfirmation = args.isNull(4) ? false : args.getBoolean(4); // 4 - EXTRA_HIDE_EXIT_UNSAVE_CONFIRMATION Boolean enableEffectsPacks = args.isNull(5) ? false : args.getBoolean(5); // 5 - EXTRA_EFFECTS_ENABLE_EXTERNAL_PACKS Boolean enableFramesPacks = args.isNull(6) ? false : args.getBoolean(6); // 6 - EXTRA_FRAMES_ENABLE_EXTERNAL_PACKS Boolean enableStickersPacks = args.isNull(7) ? false : args.getBoolean(7); // 7 - EXTRA_STICKERS_ENABLE_EXTERNAL_PACKS Boolean disableVibration = args.isNull(8) ? false : args.getBoolean(8); // 8 - EXTRA_TOOLS_DISABLE_VIBRATION Boolean inSaveOnNoChanges = args.isNull(9) ? true : args.getBoolean(9); // 9 - EXTRA_IN_SAVE_ON_NO_CHANGES // get URI Uri uri = Uri.parse(source); // first check the external storage availability if (!isExternalStorageAvilable()) { callbackContext.error("Cannot start aviary, external storage unavailable."); return true; } String mOutputFilePath; // create a temporary file where to store the resulting image File file = getNextFileName(); if (null != file) { mOutputFilePath = file.getAbsolutePath(); } else { callbackContext.error("Cannot start aviary, failed to create a temp file."); return true; } // Create the intent needed to start feather Class<FeatherActivity> clsAviary = FeatherActivity.class; Intent newIntent = new Intent(this.cordova.getActivity(), clsAviary); // set the parameters newIntent.setData(uri); newIntent.putExtra(Constants.EXTRA_OUTPUT, Uri.parse("file://" + mOutputFilePath)); newIntent.putExtra(Constants.EXTRA_OUTPUT_FORMAT, outputFormat); newIntent.putExtra(Constants.EXTRA_OUTPUT_QUALITY, quality); if (toolList.length > 0) { newIntent.putExtra(Constants.EXTRA_TOOLS_LIST, toolList); } newIntent.putExtra( Constants.EXTRA_HIDE_EXIT_UNSAVE_CONFIRMATION, hideExitUnsaveConfirmation); newIntent.putExtra(Constants.EXTRA_EFFECTS_ENABLE_EXTERNAL_PACKS, enableEffectsPacks); newIntent.putExtra(Constants.EXTRA_FRAMES_ENABLE_EXTERNAL_PACKS, enableFramesPacks); newIntent.putExtra(Constants.EXTRA_STICKERS_ENABLE_EXTERNAL_PACKS, enableStickersPacks); // http://developers.aviary.com/docs/android/intent-parameters#EXTRA_MAX_IMAGE_SIZE // newIntent.putExtra(Constants.EXTRA_MAX_IMAGE_SIZE, 1000); // since a minor bug exists that when explicitly setting this to false disables vibration so // only set if true if (disableVibration) { newIntent.putExtra(Constants.EXTRA_TOOLS_DISABLE_VIBRATION, disableVibration); } newIntent.putExtra(Constants.EXTRA_IN_SAVE_ON_NO_CHANGES, inSaveOnNoChanges); cordova.getActivity().startActivityForResult(newIntent, ACTION_REQUEST_FEATHER); cordova.setActivityResultCallback(this); return true; } catch (Exception ex) { Log.e(LOG_TAG, ex.toString()); callbackContext.error("Unknown error occured showing aviary."); } } else if (action.equals("prepareForShow")) { // nothing to do on Android callbackContext.success(); } return false; }